Vim Tips Wiki
(Change <tt> to <code>, perhaps also minor tweak.)
(adding a note about the formatoption)
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=850
 
|id=850
Line 15: Line 14:
 
<pre>
 
<pre>
 
:set tw=79
 
:set tw=79
  +
</pre>
  +
Also make sure that you have the "t" formatoption set!
  +
<pre>
  +
:set formatoptions+=t
 
</pre>
 
</pre>
   
Line 29: Line 32:
 
</pre>
 
</pre>
   
sets a wrap margin of 2 characters from the right window border. A system-dependent EOL character is inserted and the line wraps as you type. This option may be useful in some situations, but probably is not what you are looking for.
+
sets a wrap margin of 2 characters from the right window border. A [[File format|system-dependent EOL character]] is inserted and the line wraps as you type. This option may be useful in some situations, but probably is not what you are looking for.
  +
  +
Use
  +
<pre>
  +
gq
  +
</pre>
  +
  +
on a line or paragraph that is too long, and vim will wrap it automatically at your assigned textwidth.
  +
  +
The above methods will do a "hard" wrap of your text, by inserting newline characters. An alternative method is a "soft" wrap which does not change the text but simply displays it on multiple lines. This can be achieved with:
  +
  +
<pre>
  +
:set wrap linebreak nolist
  +
</pre>
  +
  +
Note that this may lead to a bunch of screen lines being taken up by only a single "real" line, so commands like j and k which move on real lines will skip over a lot of screen lines. You can use gj and gk to move by screen lines.
   
 
==References==
 
==References==

Revision as of 14:54, 3 December 2013

Tip 850 Printable Monobook Previous Next

created 2005 · complexity basic · author mmj · version 6.0


You can set the text width using :set textwidth=n (or :set tw=n) where n is a positive integer, for example:

:set tw=79

Also make sure that you have the "t" formatoption set!

:set formatoptions+=t

That will automatically wrap text as close to 79 characters as white space allows without exceeding the 79 character limit. This option wraps at word boundaries.

The textwidth option can be unset using:

:set tw=0

If you want to apply this to specific areas; bring the cursor at the beginning of the text you want to format and type gq. Now specify the range, say $ to format a large line.

:set wm=2

sets a wrap margin of 2 characters from the right window border. A system-dependent EOL character is inserted and the line wraps as you type. This option may be useful in some situations, but probably is not what you are looking for.

Use

gq

on a line or paragraph that is too long, and vim will wrap it automatically at your assigned textwidth.

The above methods will do a "hard" wrap of your text, by inserting newline characters. An alternative method is a "soft" wrap which does not change the text but simply displays it on multiple lines. This can be achieved with:

:set wrap linebreak nolist

Note that this may lead to a bunch of screen lines being taken up by only a single "real" line, so commands like j and k which move on real lines will skip over a lot of screen lines. You can use gj and gk to move by screen lines.

References

Comments