Search code examples
vimword-wrap

Word Wrap in Vim (preserving indentation)


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?


Solution

  • 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