Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Stop Vim wrapping lines in the middle of a word

Tags:

vim

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?

like image 381
Dan Walmsley Avatar asked Mar 13 '12 21:03

Dan Walmsley


People also ask

How do I stop text wrapping 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.

How do I turn on line wrap 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.

What does set wrap do in Vim?

If you want to make Vim wrap long lines to fit in the window, you first have to enable :set wrap . By default Vim will break lines at exactly the width of the window, which causes some words to be split across two lines. To prevent this from happening, you can enable :set linebreak .


4 Answers

: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.

like image 110
Cat Plus Plus Avatar answered Oct 23 '22 13:10

Cat Plus Plus


Use

:set linebreak

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

like image 45
sidyll Avatar answered Oct 23 '22 14:10

sidyll


With vim open, press esc and enter

:set lbr

like image 4
bluesman Avatar answered Oct 23 '22 15:10

bluesman


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
like image 2
Stryker Avatar answered Oct 23 '22 13:10

Stryker