7

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

3 Answers 3

17

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.

1
  • formatoptions+=a is very useful, thanks!
    – jonescb
    Mar 27, 2011 at 20:18
4

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.

1

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .