Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I wrap text to some length in Vim?

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?

like image 907
Léo Léopold Hertz 준영 Avatar asked May 05 '09 07:05

Léo Léopold Hertz 준영


People also ask

How do you wrap text in Vim?

If you want to wrap lines in a specific area, move the cursor to the text you want to format and type gq followed by the range. For example, gqq wraps the current line and gqip wraps the current paragraph.

How do I turn on word wrap in Vim?

Command to toggle word wrap in Vim While :set wrap will turn on word wrap in Vim and :set nowrap will turn off word wrapping, you can also use both commands with the ! (bang) symbol to toggle word wrap.

What is Nowrap in Vim?

To wrap lines visually, i.e. the line is still one line of text, but Vim displays it on multiple lines. Use :set nowrap. To display long lines as just one line (i.e. you have to scroll horizontally to see the entire line).

How do you wrap text in gedit?

In the gedit Text Editor menu click Edit, and then click Preferences. A popup named gedit Preferences opens up. In the same popup on the first Tab named View, under the sub-heading Text Wrapping click the check box named Enable text wrapping, to toggle the text wrap feature on or off.


2 Answers

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.

like image 86
sleske Avatar answered Sep 21 '22 11:09

sleske


Once you set 'textwidth', you can select text with visual mode and press gq to wrap it nicely (you can also use Q on some older/legacy configurations).

A few useful tips:

gqq (wrap the current line) gq} (wrap this 'paragraph', i.e. until the next blank line) :h gq 
like image 20
Vlad Dogaru Avatar answered Sep 19 '22 11:09

Vlad Dogaru