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

Vim

Vim Problem Overview


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 ...

Vim Solutions


Solution 1 - Vim

Vim does this very easy (break lines at word boundaries).

gq{motion} % format the line that {motion} moves over
{Visual}gq % format the visually selected area
gqq        % format the current line
...

I'd suggest you check out :help gq and :help gw.

Also setting textwidth (tw) will give you auto line break when exceeded during typing. It is used in gq too, though if disabled gq breaks on window size or 79 depending on which comes first.

:set tw=80

By setting format options to include text width vim will automatically break at the tw setting.

:set fo+=t

Solution 2 - Vim

First set your vim so that it understands that you want 80 characters:

:set tw=80

then, hilight the line:

V

and make vim reformat it:

gq

Solution 3 - Vim

This is not really related to VIM, but you could use the fmt program as in

$ fmt myfile

Solution 4 - Vim

For solid lines of text highlight the area using v in normal mode, then press

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

This will add a newline at the end of every 80th character.

:s/       replaces within the current select
\v        uses regular expressions
(.{80})   selects 80 characters & placed them into group one
\1\r      replaces group one with group one and a newline

Solution 5 - Vim

If you're on *nix you probably have fold available.

Select the region you want using v, then you can break on spaces at width 80 using:

!fold --spaces --width=80

This is esentially the same as using gq.

However, if you just want to break at character 80 and not be restricted to whitespaces you can use:

!fold --width=80

If you want it with one keystroke just set a mapping - I've used

vmap <f1> !fold --width=80<CR>

Solution 6 - Vim

To split long lines in the complete document without removing already present line breaks, use:

:set formatoptions+=w
:set tw=80
gggqG

Solution 7 - Vim

As a quick and nasty, maybe try the following map:

map q 080lwbels<CR><ESC>

which says:

  • start a 0th position of line,
  • move to 80th char to the right,
  • go to beginning of next word,
  • go back to previous word,
  • go to end of current word,
  • go one char right, and
  • substitute a CR for that char.

Then hitting q and CR will break the line up into chunks on the word boundary.

Solution 8 - Vim

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

Solution 9 - Vim

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

Solution 10 - Vim

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.

Solution 11 - Vim

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.

Solution 12 - Vim

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJordan ParmerView Question on Stackoverflow
Solution 1 - Vimhe_the_greatView Answer on Stackoverflow
Solution 2 - Vimuser80168View Answer on Stackoverflow
Solution 3 - VimWernseyView Answer on Stackoverflow
Solution 4 - VimsmokediceView Answer on Stackoverflow
Solution 5 - VimMichael AndersonView Answer on Stackoverflow
Solution 6 - VimOlivier FaucheuxView Answer on Stackoverflow
Solution 7 - VimRob WellsView Answer on Stackoverflow
Solution 8 - VimJohn RixView Answer on Stackoverflow
Solution 9 - Vimuser14080458View Answer on Stackoverflow
Solution 10 - VimkbulgrienView Answer on Stackoverflow
Solution 11 - Vimandrew_yskView Answer on Stackoverflow
Solution 12 - Vim808View Answer on Stackoverflow