204

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 12

302

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
8
  • 9
    So for your case, the gq command would be <ESC> (get out of Insert/Replace/etc mode), then gq80l Aug 13, 2009 at 14:46
  • 6
    This is the opposite of Ctrl+j (combines multiple lines into one). Mar 31, 2011 at 3:56
  • 11
    just for the next person who finds this, gq only wont split a line of solid text, it needs whitespace to do its thing
    – stringy05
    Aug 28, 2014 at 22:48
  • Just a note: To get auto line break you must set :set fo+=t.
    – KevinO
    Mar 5, 2016 at 14:13
  • Related to the comment by @stringy05 - if you need to split a line of solid text with no whitespace, Michael Anderson's answer works well (requires GNU fold)
    – Aaron D
    Jul 26, 2017 at 10:25
97

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
0
28

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
2
  • 1
    This is the only solution that worked for a long base64 string.
    – jviotti
    Aug 9, 2017 at 4:24
  • I wish I could upvote this more than once. ;-)
    – kbulgrien
    Mar 31, 2022 at 21:43
19

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

$ fmt myfile
2
  • 41
    :%!fmt % " Can make it vim related :) Aug 13, 2009 at 14:19
  • 2
    Useful for vi, though I'd prefer gqq on vim.
    – WhyNotHugo
    Aug 20, 2014 at 15:52
9

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>

2
  • This is the answer that works for long strings not separated by whitespace. On macOS you may need to use gfold instead of fold, though.
    – Aaron D
    Jul 26, 2017 at 10:23
  • on mac os, I had to do !fold -w 80
    – user674669
    Nov 10, 2017 at 2:54
6

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

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

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
4

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.

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

0

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

-1

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.

-2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.