Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Whats 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
share|improve this question
add comment

3 Answers

up vote 13 down vote accepted

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.

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

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.

share|improve this answer
add comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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