How can I merge multiple lines into one line in Vim?

The most intuitive approach would be to use Vim visual line mode, Shift + v. All you have to do is select the content you want to merge to one line, and then press Shift + j.


In command mode:

[range]j[lines]

For example: here you want to do the whole buffer:

%j

If you just wanted to do 10 lines from the current cursor position:

j10

If you don’t want to replace the new lines with spaces, use ! after j.

%j!
j!10

And for the uberfancy:

5j20

It would go to line 5, and join the next 20 lines.

Tags:

Vim