Stop Vim wrapping lines in the middle of a word

Vim

Vim Problem Overview


After doing :set wrap, Vim wraps lines longer than the window.

But is it possible to have Vim wrap to a new line on blank spaces only, not half-way through a word?

Vim Solutions


Solution 1 - Vim

:help wrap

> This option changes how text is displayed. It doesn't change the text > in the buffer, see 'textwidth' for that. > When on, lines longer than the width of the window will wrap and > displaying continues on the next line. When off lines will not wrap > and only part of long lines will be displayed. When the cursor is > moved to a part that is not shown, the screen will scroll > horizontally. > The line will be broken in the middle of a word if necessary. See > 'linebreak' to get the break at a word boundary.

:help linebreak

> If on Vim will wrap long lines at a character in 'breakat' rather > than at the last character that fits on the screen.

:help breakat

> 'breakat' 'brk' string (default " ^I!@*-+;:,./?")

So, :set linebreak and it should work out of box. Or you can restrict breakat to just break on spaces, instead of spaces+punctuation.

Solution 2 - Vim

Use

:set linebreak

Or 'lbr' for short. It will break lines on characters included in your 'breakat' option, which includes a space by default.

Solution 3 - Vim

With vim open, press esc and enter

:set lbr

Solution 4 - Vim

The following will do a line wrap without breaking any words and preserve the shorter lines.

:set formatoptions+=w
:set tw=80
gggqG

To try and format the current paragraph try the follwoing:

:nnoremap Q gqip

Solution 5 - Vim

The following commands work for me, and I am using Red Hat 7.x at work, and Cygwin 3.1.4 at home. The exclamation point acts like a not operator.

:set wrap
:set wrap!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDan WalmsleyView Question on Stackoverflow
Solution 1 - VimCat Plus PlusView Answer on Stackoverflow
Solution 2 - VimsidyllView Answer on Stackoverflow
Solution 3 - VimbluesmanView Answer on Stackoverflow
Solution 4 - VimStrykerView Answer on Stackoverflow
Solution 5 - Vimuser127440View Answer on Stackoverflow