Reputation: 37224
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 ...
Upvotes: 215
Views: 104503
Reputation: 4518
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.
Upvotes: 2
Reputation: 119
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.
Upvotes: -1
Reputation: 1
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
Upvotes: 0
Reputation: 7
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.
Upvotes: -2
Reputation: 6784
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
Upvotes: 317
Reputation: 1020
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
Upvotes: 33
Reputation: 37159
As a quick and nasty, maybe try the following map:
map q 080lwbels<CR><ESC>
which says:
Then hitting q and CR will break the line up into chunks on the word boundary.
Upvotes: 5
Reputation: 2606
To split long lines in the complete document without removing already present line breaks, use:
:set formatoptions+=w
:set tw=80
gggqG
Upvotes: 7
Reputation: 73580
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>
Upvotes: 14
Reputation: 6703
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
Upvotes: 4
Reputation:
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
Upvotes: 101
Reputation: 5491
This is not really related to VIM, but you could use the fmt program as in
$ fmt myfile
Upvotes: 20