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
Jremoves 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
gJto join lines without inserting a space between them - Use
3Jto join the current line with the next 2 lines (3 lines total) - In visual mode, select multiple lines and press
Jto join them all - If the current line ends with a period, Vim inserts two spaces after the join (controlled by the
joinspacesoption — use:set nojoinspacesto disable this behavior)