Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

For example, I want to merge such text:

CATEGORIES = ['Books',         'Business',         'Education',         'Entertainment',         'Finance',         'Games',         'Healthcare & Fitness',         'Lifestyle',         'Medical',         'Music',         'Navigation',         'News',         'Photography',         'Productivity',         'Reference',         'Social Networking',         'Sports',         'Travel',         'Utilities',         'Weather',         'All',  ] 

into

CATEGORIES = ['Books', 'Business', 'Education', 'Entertainment', 'Finance', 'Games', 'Healthcare & Fitness', 'Lifestyle', 'Medical', 'Music', 'Navigation', 'News', 'Photography', 'Productivity', 'Reference', 'Social Networking', 'Sports', 'Travel', 'Utilities', 'Weather', 'All', ] 
like image 419
northtree Avatar asked Jul 05 '11 02:07

northtree


People also ask

How do I combine multiple lines in one?

The paste command can merge lines from multiple input files. By default, it merges lines in a way that entries in the first column belong to the first file, those in the second column are for the second file, and so on. The -s option can let it merge lines row-wise.

How do you copy and paste multiple lines in Vim?

Press v to select characters, or uppercase V to select whole lines, or Ctrl-v to select rectangular blocks (use Ctrl-q if Ctrl-v is mapped to paste). Move the cursor to the end of what you want to cut. Press d to cut (or y to copy). Move to where you would like to paste.

Which command joins next line to current line?

Joins consecutive lines. The J (join) command joins a specified number of lines together as one line. Number of consecutive lines, starting with the current line, to be joined to the current line.


1 Answers

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.

like image 120
TWK Avatar answered Oct 07 '22 13:10

TWK