Skip to content

Join lines in Vim

In Vim, press J in Normal mode to join the line below onto the current one, with a space between them. Use gJ to join without adding or removing spaces. For a range, use :join, for example :1,10join.

Remove line breaksUpdated 3 min read

The three join commands

J joins lines and tidies the space between them. Vim's help says it removes the indent of the next line and inserts one space where the line end was. It adds no space if the line already ends in white space or the next line starts with a ).

gJ joins lines exactly as they are. It doesn't insert or remove any spaces, so indents are kept and words touch if there was no space.

:join (short form :j) does the same as J, but on a range of lines from the command line. Add ! to make it behave like gJ.

All three delete the line ends between the lines. You can undo them with u as usual.

Join two lines

  1. Put the cursor anywhere on the first line.
  2. Press J.

Join several lines

  • With a count: 4J joins the current line and the three below it, four lines in all. Vim's help says J always joins at least two lines, and a count bigger than the lines left is reduced to what's there.
  • In Visual mode: press V, move down to select the lines, then press J (or gJ).
  • With a range: :5,12join joins lines 5 to 12. :.,+3join joins the current line and the next three.

Join a whole paragraph

Use the paragraph text object. In Normal mode, type vipJ:

  1. v starts Visual mode.
  2. ip selects the inner paragraph: the lines around the cursor, up to the blank lines.
  3. J joins them.

This is the Vim way to fix a paragraph pasted from a PDF, where every line ends with a hard break.

Join every line in the file

Type :%join and press Enter. % is the range for the whole file, so every line becomes one. Use :%join! to keep the spacing exactly as it is.

To join the lines inside each paragraph but keep the blank lines between paragraphs, combine :global with :join:

:g/./,/^$/join

For every line that has text, this joins from that line to the next blank line. Try it on a copy first; on a file with no blank line at the end, the last paragraph may need joining by hand.

Two spaces after a full stop

If the joinspaces option is on, J and :join put two spaces after a ., ! or ? when joining. Turn it off for one space:

:set nojoinspaces

Add that line to your vimrc to make it permanent.

Without Vim

If the text isn't in a file you're editing in Vim, such as a paragraph from a PDF or an email, paste it into the box below instead. It opens set up like :%join: every line joined into one, with a single space between them and indents trimmed. Choose Keep paragraphs to join only the lines inside each paragraph, like vipJ on every paragraph at once.

Remove line breaks

Open the full tool

Common problems

J added a space you didn't want. Undo with u and use gJ instead.

The joined text has double spaces. The lines had trailing spaces before you joined them, or joinspaces is on. Remove trailing spaces first with :%s/\s\+$//.

:join did nothing. A range with the same start and end, like :5,5join, does nothing, according to Vim's help. Give it at least two lines.

J fails on the last line. There's no line below to join. Move up one line first.

Sources

Questions

How do I join lines in Vim?

In Normal mode, put the cursor on the first line and press J. The next line is joined onto it with a space. Use a count, like 4J, to join four lines.

How do I join lines in Vim without a space?

Use gJ instead of J, or :join! with an exclamation mark. Neither inserts nor removes any spaces.

How do I merge two lines?

Put the cursor anywhere on the first of the two lines and press J.

How do I start a new line in Vim?

Press o to open a new line below the cursor, or O for one above, and start typing. In Insert mode, Enter breaks the line where the cursor is.