我是 Vim 的长期用户(三四年),最近开始处理一些深度嵌套的代码。此代码使用空格缩进,而不是制表符。我想要一些干净且不分散注意力的缩进指示,以帮助在我查看多个层次的深度内容时跟踪我所在的代码块。
:set list
只显示制表符和结束行字符。我找到了一个插件(目前似乎无法挖掘它),它将以逐渐变暗的颜色突出显示每个缩进级别,但这在视觉上没有吸引力。理想情况下,我希望在每个压痕级别看到细垂直线。许多新奇的编辑器都有这个功能,但我还不愿意放弃 Vim。
有谁知道如何实现这一点?
I'm a longtime Vim user (3 or 4 years) who has recently started dealing with some deeply nested code. This code is indented with spaces, not tabs. I would like some clean and non-distracting indication of indentation to help with keeping track of which block of code I'm in when I'm looking at something many levels deep.
:set list
only displays tab and endline characters. I have found one plugin (can't seem to dig it up at the moment) that will highlight each indentation level in progressively darker colors but this is visually unappealing. Ideally I would like to see thin vertical lines at each indentation level. Many new-fangled editors have this functionality but I'm not willing to give up on Vim just yet.
Does anyone know how this can be achieved?
发布评论
评论(7)
这个答案有点晚了,而且也是一个无耻的插件。:)
无论如何,试试我的 Indent-Guides.vim 插件。它的创建是为了解决我对 vim 中缺乏缩进指南的渴望。我厌倦了等待别人来建造它,所以我就自己做了。
特点:
以下是该插件运行中的一些屏幕截图: 将您的将鼠标移至此处并单击。
This answer is a bit late to the party and also a shameless plug. :)
Regardless, try my Indent-Guides.vim plugin. It was created to scratch my own itch regarding the lack of indent guides in vim. I got fed-up waiting for someone else to come along and build it, so I just did it myself.
Features:
Here’s a few screenshots of the plugin in action: put your mouse here and click.
如果您使用空格缩进代码,您可以尝试我的插件:https://github.com /Yggdroot/indentLine,它显示细垂直线,但不显示上述答案提到的粗垂直线。如果使用制表符缩进代码,只需
:set list lcs=tab:\|\ (这里是一个空格)
If you indent code with spaces, you can try my plugin: https://github.com/Yggdroot/indentLine, it displays thin vertical lines but not thick vertical lines as the above answers mentions. If you indent code with tab, just
:set list lcs=tab:\|\ (here is a space)
您可以使用制表符来显示缩进指南并在保存文件之前删除制表符:
You might use tabs to display indentation guides and remove tabs before saving file:
也许最有效的解决方案是“绘制”缩进
使用匹配突出显示的指南。要了解它有何用处,
看一下下面的示例:
它突出显示 - 使用“搜索”突出显示组;任何其他组
当然可以使用——第一、第五、第九(可以是
续)前面的空格字符占用的虚拟列
除了行首的空白之外什么都没有。因此,这个
为四个空格的缩进(最多为三个)生成突出显示
层次深。
唯一剩下的就是推广这个想法的过程
生成与上面类似的模式,根据
当前缓冲区的
textwidth
和shiftwidth
设置(按顺序)处理更深的缩进级别并使用实际的缩进宽度。
该任务可以简单地自动化,如下面的函数所示。
每当当前缓冲区中需要缩进指南时,它
可以通过
:调用ToggleIndentGuides()
打开。当然,一可以更改突出显示组(甚至可以创建一个专用的突出显示组)
与缩进指南一起使用),为此设置一个方便的映射,以及
对于某些文件类型,从
autocmd
调用它。有关示例,请参阅突出显示配置的缩进指南
来自我的
.vimrc
文件 https://gist.github.com/734422,其中实现上述方法的稍微扩展版本。
Probably the most effective solution would be to “draw” indentation
guides using match-highlighting. To understand how it may be useful,
take a look at the following example:
It highlights—using the Search highlighting group; any other group
can, of course, be used—the first, the fifth, and the ninth (it can be
continued) virtual columns occupied by the space character preceding
nothing but whitespace from the beginning of line. Hence, this
produces highlighting for four-space indentation that is at most three
levels deep.
The only thing remaining to generalize this idea is a procedure
generating a pattern similar to the one above, according to the
current buffer’s
textwidth
andshiftwidth
settings, in orderto handle deeper indent levels and use the actual indentation width.
This task can be simply automated as shown in the function below.
Whenever indentation guides are necessary in the current buffer, it
can be switched on via
:call ToggleIndentGuides()
. Of course, onecan change the highlighting group (or even create a dedicated one for
using with indentation guides), setup a handy mapping for that, and
call it from an
autocmd
for some file types.For an example, see the indentation guides highlighting configuration
from my
.vimrc
file at https://gist.github.com/734422, whichimplements a slightly extended version of the above approach.
以下命令将配置 Vim 显示点来指示
键入时的缩进级别。当这些点神奇地消失时
光标离开该行:
The following command will configure Vim to show dots to indicate the
indentation level as you type. The dots magically disappear when the
cursor leaves the line:
试试这个 VIM 插件 BlockHL
它对每个连续级别的缩进进行不同的颜色编码。
编辑:您使用什么语言?该插件适用于 C 风格语言。
Try out this VIM plugin BlockHL
It color codes the indentation of each successive level differently.
EDIT:What lanaguge are you using? This plugin is for C-style languages.
使用 Indent-Guides.vim 插件,并在需要时切换使用 ig。有时它可能会很烦人:)
use the Indent-Guides.vim plugin, and toggle use ig whenever you need it. Sometimes it could be annoying though :)