Vim 中的自动换行(保留缩进)

发布于 2024-09-01 14:02:01 字数 1322 浏览 8 评论 0原文

我只是在看这篇文章,它描述了如何在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               |
|                                       |
|---------------------------------------|

有什么想法吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

じее 2024-09-08 14:02:01

最初提出问题时这不起作用,但从 2014 年 6 月 25 日起,这将起作用。 (假设您将 vim 更新到比该日期更新的版本)

添加到您的 .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

就是这样!

--

有些人(包括我自己)在多台计算机上共享一个 .vimrc。在这种情况下,使该行保持健壮非常重要(以避免烦人的错误消息)。这样好一点:

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

这样,如果您使用的是早期版本的 vim,就不会收到错误消息。

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.

凉宸 2024-09-08 14:02:01

breakindent 补丁 可以满足您的需求。我使用此线程中的说明成功应用了它:

在 OS X 上使用 Breakindent 补丁修补 Vim与 Homebrew

具体来说,是 echristopherson 的 Homebrew 公式。

我知道这个线程很旧,但它在谷歌上很受欢迎,我在尝试寻找解决方案时多次遇到它。

编辑:此补丁现在作为补丁 7.4.338 包含在 vim 中。请参阅:https://retracile.net/blog/2014/07/18/18.00< /a>

在 Yosemite (Mac OS X) 上,我将 Snowbound 的命令与 hombrew 一起使用:

brew install macvim --with-features=huge --override-system-vim --HEAD 

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 
只想待在家 2024-09-08 14:02:01

您将得到的最好的结果是 showbreak 选项,它将在每个换行的前面放置一个固定字符串(我使用 set showbreak=...)。

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=...).

长发绾君心 2024-09-08 14:02:01

我同意“表演突破”是最佳选择的答案。 Showbreak 通常不允许您将非打印字符(例如,空格或制表符)放入 showbreak 字符串中,因此通常使用时,它只会在左边距中提供一个指示符,即没有真正的缩进。这不太好,因为我认为OP的主要目标是给换行线一个缩进,防止它们混乱左边距区域并看起来像自己的线。

因此,使用 showbreak 添加(丑陋的)缩进的一种方法是仅使用大量字符,例如“:set showbreak=>--------------->”。这会导致如下所示的结果:

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

更好的替代方案可能是使用不间断空格字符(假设您的 Vim 实例启用了 unicode),每个字符都可以使用 ctrl-v 键序列输入到 showbreak 字符串中,160。这样您就可以输入左侧空白的显示中断字符串,并且看起来是真正的缩进。例如,“:set showbreak=. . . . . . . . . . >>”其中每个“.”命令中实际上是按 ctrl-V,160 输入的不间断空格字符。这样,您最终会得到一个缩进良好的换行,如下所示:

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

您仍然无法根据前一行的缩进来改变缩进级别,但至少您可以在没有大量缩进的情况下获得换行的干净缩进。窗口左侧边缘的视觉混乱。如果换行的缩进小于实际行开头的缩进,仍然可能会造成混乱,但这也许可以通过使 showbreak“缩进”相当大(即大于代码中常见的任何缩进)来避免)但仍然足够小,可以为文本的清晰换行提供足够的空间。对于许多用途,我认为 40 或 50 个空格的显示中断缩进就可以很好地做到这一点。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文