How can I merge multiple lines into one line in 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', ]

Solution 1:

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.

Solution 2:

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.