Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Word Wrap in Vim (preserving indentation)

Tags:

vim

word-wrap

I was just looking at this post which describes how to wrap entire words in vim. The accepted solution was this:

:set formatoptions=l :set lbr 

Which takes this text (tabs are shown as \t):

 *Inside of window                        *Outside of window |---------------------------------------|     |\t\tthis is a like of text that will wr|ap here                             |\t\tcan you see the wrap               | |                                       | |---------------------------------------| 

This accomplishes a behavior like this (tabs are shown as \t):

 *Inside of window                        *Outside of window |---------------------------------------|     |\t\tthis is a like of text that will   | |wrap here                              | |\t\tcan you see the wrap               | |                                       | |---------------------------------------| 

I would however like to redefine this function. I would like the wrapped line to have the same number of tabs in front of it that the line above has plus one. Ie:

 *Inside of window                        *Outside of window |---------------------------------------|     |\t\tthis is a like of text that will   | |\t\t\twrap here                        | |\t\tcan you see the wrap               | |                                       | |---------------------------------------| 

Any ideas?

like image 434
sixtyfootersdude Avatar asked May 13 '10 15:05

sixtyfootersdude


People also ask

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

What is Nowrap in Vim?

To wrap lines visually, i.e. the line is still one line of text, but Vim displays it on multiple lines. Use :set nowrap. To display long lines as just one line (i.e. you have to scroll horizontally to see the entire line).


2 Answers

This did not work when the question was originally asked, but as of June 25, 2014, this will work. (Assuming you update your vim to a version newer than that date)

Add to your .vimrc:

" Indents word-wrapped lines as much as the 'parent' line set breakindent " Ensures word-wrap does not split words set formatoptions=l set lbr 

And that's it!

--

Some people (myself included) share a single .vimrc across multiple computers. In that case, it's important to have this line be robust (to avoid annoying error messages). This is a little better:

if has("patch-7.4.354")     " Indents word-wrapped lines as much as the 'parent' line     set breakindent     " Ensures word-wrap does not split words     set formatoptions=l     set lbr endif 

This way, if you're on an earlier version of vim, you don't get an error message.

like image 189
JoshuaD Avatar answered Sep 28 '22 02:09

JoshuaD


The breakindent patch has what you're looking for. I successfully applied it using instructions found in this thread:

Patch Vim with the breakindent patch on OS X with Homebrew

Specifically, echristopherson's Homebrew formula.

I know this thread is old but it's popular on google and I came across it multiple times when trying to find a solution.

EDIT: This patch is now included with vim as patch 7.4.338. See: https://retracile.net/blog/2014/07/18/18.00

On Yosemite (Mac OS X), I used snowbound's command with hombrew:

brew install macvim --with-features=huge --override-system-vim --HEAD  
like image 38
Exit42 Avatar answered Sep 28 '22 04:09

Exit42