How can I wrap text to some length in Vim?

Léo Léopold Hertz 준영 picture Léo Léopold Hertz 준영 · May 5, 2009 · Viewed 52.6k times · Source

Let's speak of relative measures. My Vim looks like:

aaaaaaaaaaaaa 
bbbbbbbbbbbbb 
ccccccccccccc 
etc

I would like it to be smaller:

aaaaa
aaaaa
bbbbb
bbbbb
ccccc
ccccc
etc

How can I get it? And how can I manage setting the length of such a block?

Answer

sleske picture sleske · May 5, 2009

You can actually do two things:

  1. Let vim format (i.e.change) your text to have shorter lines, by inserting linebreaks
  2. Leave lines as they are, but display them wrapped

Which do you want?

Option 1 would be achieved by setting textwidth (for example :set textwidth=30 (from Swaarop's answer)). Then you can reformat your text by highlighting it (in visual mode) and typing gq. (textwidth can be abbreviated as tw, thus :set tw=30.)

Option 2 can be toggled by running :set wrap / :set nowrap. This will wrap lines which are too long for the window.

Both are independent.