201

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 ...
Jordan Parmer
  • 36,042
  • 30
  • 97
  • 119

12 Answers12

297

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
he_the_great
  • 6,554
  • 2
  • 30
  • 28
  • 9
    So for your case, the `gq` command would be `` (get out of Insert/Replace/etc mode), then `gq80l` – MidnightLightning Aug 13 '09 at 14:46
  • 6
    This is the opposite of Ctrl+j (combines multiple lines into one). – Yzmir Ramirez Mar 31 '11 at 03: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 '14 at 22:48
  • Just a note: To get _auto line break_ you must set `:set fo+=t`. – KevinO Mar 05 '16 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 '17 at 10:25
  • you should add corner cases when it doesn't work – outoftime Jun 15 '21 at 16:18
  • @YzmirRamirez Just saying it should be Shift+j... – PROgram52bc Jul 15 '22 at 12:38
  • I got a similar but additional question: I want to add a trailing \`\\` with the auto formatting, and if any space character is at the end, let the vim not removing it (so the space character would be the second last charter like this: ` \\`). It is possible? (am having trouble with the escape in this comment) – jimmymcheung Nov 20 '22 at 09:30
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
26

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
smokedice
  • 900
  • 2
  • 10
  • 25
19

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

$ fmt myfile
Wernsey
  • 5,411
  • 22
  • 38
8

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>

Michael Anderson
  • 70,661
  • 7
  • 134
  • 187
6

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

:set formatoptions+=w
:set tw=80
gggqG
Olivier Faucheux
  • 2,520
  • 3
  • 29
  • 37
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.

Brad Koch
  • 19,267
  • 19
  • 110
  • 137
Rob Wells
  • 36,220
  • 13
  • 81
  • 146
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
John Rix
  • 6,271
  • 5
  • 40
  • 46
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

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

kbulgrien
  • 4,384
  • 2
  • 26
  • 43
-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.

Vivek Jain
  • 2,730
  • 6
  • 12
  • 27
andrew_ysk
  • 119
  • 5
-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.

D. Ben Knoble
  • 4,273
  • 1
  • 20
  • 38
808
  • 7
  • 2