Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Smart Wrap in Vim

Tags:

vim

word-wrap

I have been wondering if Vim has the capability to smart wrap lines of code, so that it keeps the same indentation as the line that it is indenting. I have noticed it on some other text editor, such as e-text editor, and found that it helped me to comprehend what I'm looking at easier.

For example rather than

<p>     <a href="http://www.example.com">         This is a bogus link, used to demonstrate an example     </a> </p> 

it would appear as

<p>     <a href="somelink">         This is a bogus link, used to demonstrate         an example     </a> </p> 
like image 423
Sasha Avatar asked Jul 30 '09 02:07

Sasha


People also ask

How do you wrap text 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.

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


2 Answers

This feature has been implemented on June 25, 2014 as patch 7.4.338. There followed a few patches refining the feature, last one being 7.4.354, so that's the version you'll want.

:help breakindent :help breakindentopt 

Excerpts from vim help below:

'breakindent'     'bri'   boolean (default off)                           local to window                           {not in Vi}                           {not available when compiled without the |+linebreak|                           feature}         Every wrapped line will continue visually indented (same amount of         space as the beginning of that line), thus preserving horizontal blocks         of text.  'breakindentopt' 'briopt' string (default empty)                           local to window                           {not in Vi}                           {not available when compiled without the |+linebreak|                           feature}         Settings for 'breakindent'. It can consist of the following optional         items and must be seperated by a comma:                   min:{n}     Minimum text width that will be kept after                               applying 'breakindent', even if the resulting                               text should normally be narrower. This prevents                               text indented almost to the right window border                               occupying lot of vertical space when broken.                   shift:{n}   After applying 'breakindent', wrapped line                               beginning will be shift by given number of                               characters. It permits dynamic French paragraph                               indentation (negative) or emphasizing the line                               continuation (positive).                   sbr         Display the 'showbreak' value before applying the                                additional indent.         The default value for min is 20 and shift is 0. 

Also relevant to this is the showbreak setting, this will suffix your shift amount with character(s) you specify.

Example configuration

" enable indentation set breakindent  " ident by an additional 2 characters on wrapped lines, when line >= 40 characters, put 'showbreak' at start of line set breakindentopt=shift:2,min:40,sbr  " append '>>' to indent set showbreak=>>    

Note on behaviour

If you don't specify the sbr option, any showbreak any characters put appended to the indentation. Removing sbr from the above example causes an effective indent of 4 characters; with that setting, if you just want to use showbreak without additional indentation, specify shift:0.

You can also give a negative shift, which would have the effect of dragging showbreak characters, and wrapped text, back into any available indent space.

When specifying a min value, the shifted amount will be squashed if you terminal width is narrower, but showbreak characters are always preserved.

like image 155
Dominykas Mostauskis Avatar answered Sep 19 '22 06:09

Dominykas Mostauskis


There is a patch for this, but it's been lingering for years and last time I checked did not apply cleanly. See the "Correctly indent wrapped lines" entry in http://groups.google.com/group/vim_dev/web/vim-patches -- I really wish this would get in the mainline.

Update: that link seems to have bitrotted. Here is a more up to date version of the patch.

Update 2: it has been merged upstream (as of 7.4.345), so now you only have to :set breakindent.

like image 33
ergosys Avatar answered Sep 20 '22 06:09

ergosys