Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

People also ask

How do I split one line into multiple lines in Vim?

The easiest way I've found to split lines in Vim is the normal mode command gq (type both letters in quick succession in normal or visual mode). In visual mode, it will split whatever is selected, in normal mode, you follow gq with a motion. For example, gql will split one line to the currently set width.

How do I go down multiple lines in Vim?

In normal mode or in insert mode, press Alt-j to move the current line down, or press Alt-k to move the current line up. After visually selecting a block of lines (for example, by pressing V then moving the cursor down), press Alt-j to move the whole block down, or press Alt-k to move the block up.


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

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

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

$ fmt myfile

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