views:

1052

answers:

5

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 ...
+1  A: 

G'day,

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.

HTH

'Avahappy,

Rob Wells
A: 

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

$ fmt myfile
iWerner
:%!fmt % " Can make it vim related :)
maksymko
+8  A: 

Vim does this very easily.

gq{motion}
{Visual}gq
gqq
...

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
he_the_great
So for your case, the `gq` command would be `<ESC>` (get out of Insert/Replace/etc mode), then `gq80l`
MidnightLightning
+1  A: 
:setl tw=80 fo=t
<ESC>
gqgq

(Note: doesn't take care to preserve any previous settings.)

hobbs
+2  A: 

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
depesz