In VIM, how do I break one really long line into multiple lines?

Viewed 83130

Say I have a super long line in the VIM editor (say around 300+ characters). How would I break that up into multiple lines so that the word boundaries roughly break at 80 characters?

Example:

This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line This is a really long line

to

This is a really long line 
This is a really long line
This is a really long line
This is a really long line
This is a really long line
This is a ...
12 Answers

I needed to reformat an entire file rather than one line. As Wernsey points out, I could have used 'fmt', but the following sequence in vim did the trick also (borrowing from the various answers here):

<ESC>
:setl tw=80 fo=t
1GVGgq

fmt also works quite well in VIM, and will change something like this:

md5: A55B4EEB6FC24B2377A31A37C490D236 | sha1: BB4E344C5F271BF8B76B3FDC626A26627E97F453 | sha256: 7A386ADBBF9CE26E892F044128F21C70B13695CE7931456C12868776BC680582 | sha512: DECB7B5B66FA5A272FDAB56CD4B6639CA216B30418E050C16A3821FE2FBF9B90C3DC35671AED44B0AE8C5471FCD6393D4955237E1497DF2CA2B427615FEE7B32

To a more favorable HASH:

|

It will put the type of hash, hash number, and pipe all on their own respective lines successively...

Just visually select the text you need, then:

!fmt -1

This is a really long line This is a really long line This is a really long line

smokedice posted a very, very helpful recipe!

:s/\v(.{80})/\1\r/g

It can break lines at any pattern by replacing .{80} with a different pattern. For example, everywhere the word line appears, replace following spaces with a line break:

:s/\v(line)[ ]*/\1/r/g

Move the break before or after \1 by \r placement.

 :s/\v(This)/\r\1/g

Omit "\1" if you want to replace the pattern with the line break with or without something else:

 :s/\v(long line )/short line\r/g

As this is an s/// replacement, it is useful with :g/select/s/find/replace/g where select is a pattern that selects lines to edit, find is the pattern to break lines at, and replace is the actual break.

:g/^This.*This/s/\v(This)/\1\r/g 

Seriously, the answer is awesome.

I manually break up the long line at place. I think where the main point is by pressing "r" (normal mode) then press .

This will make delete a character where cursor is. So remember to do it at the space before the word you want to make a new line, else you will have to insert the missing character.

I just don't know how to break the line and shift it down 2 line space so that there will be space between the 2 lines.

I manually inserted '\' (and then CR / tab to format) in each LONGLINE after the last whitespace but before the 80 column. That is to say:

1 this is a long, long, line

now looks like

1 this is a long, \
        long line

and compiles normally.

Related