When editing a text file, if you want word wrapping, but only want line breaks inserted when you explicitly press the Enter key:
:set wrap :set linebreak :set nolist " list disables linebreak
This will get Vim to wrap existing text as desired.
wrap tells Vim to word wrap visually (as opposed to changing the text in the buffer), and
linebreak tells Vim to only wrap at a character in the
breakat option (by default, this includes "
^I!@*-+;:,./?" (note the inclusion of " " and that
^I is the control character for Tab)).
In addition, you will need to prevent Vim from automatically inserting line breaks in newly entered text. The easiest way to do this is:
:set textwidth=0 :set wrapmargin=0
If you want to keep your existing 'textwidth' settings for most lines in your file, but not have Vim automatically reformat when typing on existing lines, you can do this with:
To make settings permanent, without the need to type this every time you use Vim, just add the commands to your vimrc.
- :help 'wrap'
- :help 'linebreak'
- :help ins-textwidth
- :help 'showbreak' to show the difference between soft and hard wrapped lines
I'm not certain whether 'wrapmargin' is always turned off when it is set equal to zero, but it seems to work. Is this always the case?
--Fritzophrenic 17:47, June 15, 2010 (UTC)
- I do not use wrapmargin but surely its meaning implies that it only does something when set to a value above zero. I think it's pretty safe to assume the tip is ok. JohnBeckett 11:32, June 16, 2010 (UTC)
This doesn't work. With the settings saved, I restarted vim and created a new file. If I hold down the "a" key and randomly press the space bar. Eventually vim still inserts line feed characters into the text. This is not a visual wrap, but the actual '\n' character. Which shouldn't happen. --October 2, 2012
It can be useful to do a word wrap without line breaks only when editing text (not programming code), using LaTeX for example. I've added these lines in my .vimrc to reach this :
au BufRead,BufNewFile *.txt,*.tex set wrap linebreak nolist textwidth=0 wrapmargin=0
--Eduardoconto 20:48, February 8, 2013 (UTC)