What's the best way to do word wrapping for a document in vim?
I'd like have each line wrap at 80 characters, not just visually wrap but enter a newline.

I got this kind of working by using

:set wrap
:set linebreak

But if I go back and insert text on a previous line, it doesn't adjust the rest of the paragraph. It takes the overflow text and puts it on a newline, but the line that comes after doesn't get moved up to fill in the gap.

So if I edit a previous line, my paragraph can look like this

A line that is 80 characters long
short line
A line that is 80 characters lone

I want it to be like this:

A line that is 80 characters long
short line A line that is 80
characters long

wrap and linebreak control the display of text, I think you'll find they don't actually insert newlines in the file. To get vim to insert newlines in the file as you type, set textwidth to the desired width (e.g. 80).

That will still not automatically reflow subsequent lines when you insert more text. I usually do that manually with gq}, but I just discovered that set formatoptions+=a will tell vim to do it automatically. See the help for auto-format.

| improve this answer | |
  • formatoptions+=a is very useful, thanks! – jonescb Mar 27 '11 at 20:18

The gq} wraps a paragraph to textwidth. Be sure to set tw=80 first. Many distros map that to Q. So you may also be able to also use Q} instead.

| improve this answer | |

I use par for formatting, it can even word wrap with an existing prefix, in the context of emails for example.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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