vimtricks.wiki Concise Vim tricks, one at a time.

How do I join two lines together in Vim?

Answer

J

Explanation

The J command joins the current line with the line below it, replacing the newline with a space. It is one of the most commonly used editing commands for merging lines.

How it works

  • J removes the newline at the end of the current line
  • Leading whitespace on the next line is stripped
  • A single space is inserted between the joined content

Example

Given the text:

Hello
world

With the cursor on the first line, pressing J results in:

Hello world

Tips

  • Use gJ to join lines without inserting a space between them
  • Use 3J to join the current line with the next 2 lines (3 lines total)
  • In visual mode, select multiple lines and press J to join them all
  • If the current line ends with a period, Vim inserts two spaces after the join (controlled by the joinspaces option — use :set nojoinspaces to disable this behavior)

Next

How do I edit multiple lines at once using multiple cursors in Vim?