Vim Tips Wiki
(easier commands to type than gq$)
Tag: sourceedit
(Add code tags)
Tag: sourceedit
(One intermediate revision by one other user not shown)
Line 11: Line 11:
 
|category2=Usage
 
|category2=Usage
 
}}
 
}}
You can set the text width using <code>:set textwidth=n</code> (or <code>:set tw=n</code>) where n is a positive integer, for example:
+
You can set the text width for automatic word wrapping using <code>:set textwidth=''n''</code> (or <code>:set tw=''n''</code>) where ''n'' is a positive integer, for example:
 
<pre>
 
<pre>
 
:set tw=79
 
:set tw=79
 
</pre>
 
</pre>
  +
Also make sure that you have the "t" formatoption set!
 
 
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.
  +
  +
Wrapping text using textwidth requires "t" in formatoptions, which it is by default. The following commands display the current setting then add "t" if needed. The abbreviation fo is used instead of formatoptions.
 
<pre>
 
<pre>
:set formatoptions+=t
+
:set fo?
  +
:set fo+=t
 
</pre>
 
</pre>
   
  +
If a line is already longer than textwidth when insert mode is started, the line may not be wrapped even if text is added to the line. A long line is not wrapped when text is added if formatoptions contains "l". If needed, "l" can be removed so long lines will be wrapped:
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.
 
  +
<pre>
  +
:set fo-=l
  +
</pre>
   
The textwidth option can be unset using:
+
To stop automatic wrapping, unset textwidth using:
 
<pre>
 
<pre>
 
:set tw=0
 
:set tw=0
 
</pre>
 
</pre>
   
If you want to apply this to specific areas; bring the cursor at the beginning of the text you want to format and type <code>gq</code>. Now specify the range, say <code>$</code> to format a large line.
+
If you want to wrap lines in a specific area, move the cursor to the text you want to format and type <code>gq</code> followed by the range. For example, <code>gqq</code> wraps the current line and <code>gqip</code> wraps the current paragraph.
  +
 
The following 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.
 
<pre>
 
<pre>
 
:set wm=2
 
:set wm=2
 
</pre>
 
</pre>
   
 
The above methods 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:
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 <code>gqq</code> or <code>gqip</code> 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>
 
<pre>
 
:set wrap linebreak nolist
 
:set wrap linebreak nolist
 
</pre>
 
</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.
+
Note that this may lead to a bunch of screen lines being taken up by only a single "real" line, so commands like <code>j</code> and <code>k</code> which move on real lines will skip over a lot of screen lines. You can use <code>gj</code> and <code>gk</code> to move by screen lines.
   
 
==References==
 
==References==
Line 50: Line 54:
   
 
==Comments==
 
==Comments==
This doesn't work in vim7.3. I get no text wrapping with these options:
 
 
:set
 
--- Options ---
 
autoindent hidden scroll=42 tabstop=4
 
background=dark history=50 shiftwidth=4 textwidth=78
 
comments=:# hlsearch showcmd ttyfast
 
commentstring=#%s ignorecase showmatch ttymouse=xterm2
 
define=[^A-Za-z_] incsearch smartcase viminfo='20,"50
 
expandtab modified smartindent visualbell
 
filetype=perl pastetoggle=<F11> softtabstop=4 t_Sb=^[[4%dm
 
helplang=en ruler syntax=perl t_Sf=^[[3%dm
 
backspace=indent,eol,start
 
errorformat=%f:%l:%m
 
fileencoding=utf-8
 
fileencodings=ucs-bom,utf-8,latin1
 
formatoptions=bctloq
 
guicursor=n-v-c:block,o:hor50,i-ci:hor15,r-cr:hor30,sm:block,a:blinkon0
 
include=\<\(use\|require\)\>
 
includeexpr=substitute(substitute(v:fname,'::','/','g'),'$','.pm','')
 
indentexpr=GetPerlIndent()
 
indentkeys=0{,0},:,0#,!^F,o,O,e,0=,0),0],0=or,0=and
 
isfname=@,48-57,/,.,-,_,+,,,#,$,%,~,=,:
 
keywordprg=perldoc -f
 
----
 
This feature has worked for many years in every version of Vim since it was introduced. What were you expecting to happen? What happened instead? With the settings above, you should see the text automatically reflow whenever you type text that goes beyond the 78th column. But just setting these options won't automatically wrap anything until you type. And when it does wrap, it will do so by inserting newlines. --[[User:Fritzophrenic|Fritzophrenic]] ([[User talk:Fritzophrenic|talk]]) 18:11, July 10, 2015 (UTC)
 
----
 
The problem turned out to be that I had to remove the 'l' (lowercase L) from formatoptions before word-wrapping would work. -- 14:51, July 13, 2015 (UTC)
 

Revision as of 20:05, 17 January 2016

Tip 850 Printable Monobook Previous Next

created 2005 · complexity basic · author mmj · version 6.0


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

:set tw=79

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.

Wrapping text using textwidth requires "t" in formatoptions, which it is by default. The following commands display the current setting then add "t" if needed. The abbreviation fo is used instead of formatoptions.

:set fo?
:set fo+=t

If a line is already longer than textwidth when insert mode is started, the line may not be wrapped even if text is added to the line. A long line is not wrapped when text is added if formatoptions contains "l". If needed, "l" can be removed so long lines will be wrapped:

:set fo-=l

To stop automatic wrapping, unset textwidth using:

:set tw=0

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.

The following 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.

:set wm=2

The above methods 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