Vim 中的字包装(保留缩进)

我刚刚在看 这篇文章,它描述了如何在 vim 中包装整个单词。公认的解决办法是:

:set formatoptions=l
:set lbr

它接受这个文本(制表符显示为 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               |
|                                       |
|---------------------------------------|

这实现了类似下面这样的行为(选项卡显示为 t) :

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

然而,我想重新定义这个函数。我希望包装线前面有相同数量的制表符,上面的行加一。即:

 *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               |
|                                       |
|---------------------------------------|

有什么想法吗?

13438 次浏览

The best you're going to get is the showbreak option which will put a fixed string in front of each wrapped line (I use set showbreak=...).

I agree with the answer that says 'showbreak' is the best option. Showbreak does not typically allow you to put nonprinting characters (e.g., spaces or tabs) into the showbreak string, so as typically used it will just give you an indicator along left margin, i.e., no real indent. This isn't great, since main goal of OP, I think, is to give wrapped lines an indent keep them from cluttering left margin area and looking like lines of their own.

So one way to add an (ugly) indent using showbreak is to just use a lot of characters, .e.g, ":set showbreak=>--------------->". This results in something that looks like this:

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

A better alternative might be to make use of nonbreaking space characters (assuming your instance of Vim is unicode enabled), each of which can be entered into the showbreak string using the key sequence of ctrl-v,160. That way you can enter a showbreak string that is blank at the left side and appear to be a true indent. E.g., ":set showbreak=. . . . . . . . . . >>" where each '.' in the command is actually a nonbreaking space character entered by pressing ctrl-V,160. That way you end up with a wrap that is nicely indented, like this:

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

You still don't have any ability to vary the level of indent according to indent of previous line, but at least you get clean indent of wrapped lines without lots of visual clutter along left margin of window. There could still be confusion if indent of a wrapped line is less than that of the beginning of an actual line, but this could perhaps be avoided by making the showbreak "indent" quite large (i.e., greater than any indent commonly found in your code) but still small enough that it provides enough space for legible wrapping of the text. For many uses I think a showbreak indent of 40 or 50 spaces would do this pretty well.

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

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.