How to combine multiple lines into one line in VIM?

eg:

I want to combine such text

CATEGORIES = ['Books',
'Business',
'Education',
'Entertainment',
'Finance',
'Games',
"Health and fitness",
'Life style',
'The medicine',
'Music',
'Navigation',
'News',
'The photo',
'Performance',
'Directory',
"Social networks",
'Sport',
'Travel',
'Utilities',
'Weather',
'All',]

at

CATEGORIES = ['Books', 'Business', 'Education', 'Entertainment', 'Finance', 'Games', 'Health and Fitness', 'Lifestyle', 'Medicine', 'Music', 'Navigation' News "," Photography "," Performance "," Link "," Social Networks "," Sports "," Travel "," Utilities "," Weather "," Everything ",]

+57
vim
Jul 05 2018-11-11T00:
source share
6 answers

In command mode:

[range]j[lines] 

EG: here you want to make the entire buffer:

 %j 

If you just want to make 10 lines from the current cursor position:

 j10 

If you do not want to replace newlines with spaces! after j.

 %j! j!10 

And for uberfancy:

 5j20 

Goes to line 5 and joins the next 20 lines.

+94
Jul 05 2018-11-11T00:
source share
— -

The most intuitive approach would be to use the vim shift + v visual linear mode. All you have to do is select the content you want to combine into one line, then press shift + j .

+27
07 Oct
source share

Use the J (uppercase) key. He will join you

Check this thread for more information on joining options and see the Help page.

+9
Jul 05 '11 at 2:28
source share

In this specific example, the following commands will work:

 :1, 21 j 

or

 :%s/\n/ /g 
+6
Jul 05 '11 at 2:40
source share

Or combine everything: from the opening square bracket to the closing square bracket (provided that there are a lot of them in your file) and leaving the other lines unchanged

 :g/\[/,/\]/j 

it's quick and easy.

+2
Nov 19 '14 at 15:53
source share
 :g/\[/,/\]/j 

Or

 /^CATEGORIES :v//-1j 

And if you have:

 edit "Komputer" ala ala next edit "FortiGate" ala ala next :g/edit/,/next/j 
+2
Mar 02 '18 at 22:06
source share



All Articles