Vim 中可以显示缩进指南吗?

发布于 2024-08-19 16:45:46 字数 289 浏览 8 评论 0 原文

我是 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?

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

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

发布评论

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

评论(7

戏剧牡丹亭 2024-08-26 16:45:46

这个答案有点晚了,而且也是一个无耻的插件。:)

无论如何,试试我的 Indent-Guides.vim 插件。它的创建是为了解决我对 vim 中缺乏缩进指南的渴望。我厌倦了等待别人来建造它,所以我就自己做了。

特点:

  • 可以检测制表符和空格缩进样式。
  • 自动检查您的配色方案并选择合适的颜色(仅限 gVim)。
  • 将使用交替颜色突出显示缩进级别。
  • 对 gVim 的全面支持以及对 Terminal Vim 的基本支持。
  • 似乎可以在 Windows gVim 7.3 上运行(尽管尚未进行任何广泛的测试)。
  • 缩进指南的可定制尺寸,例如。瘦导板(仅限软标签)。
  • 可定制的开始缩进级别。

以下是该插件运行中的一些屏幕截图: 将您的将鼠标移至此处并单击

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:

  • Can detect both tab and space indent styles.
  • Automatically inspects your colorscheme and picks appropriate colors (gVim only).
  • Will highlight indent levels with alternating colors.
  • Full support for gVim and basic support for Terminal Vim.
  • Seems to work on Windows gVim 7.3 (haven't done any extensive tests though).
  • Customizable size for indent guides, eg. skinny guides (soft-tabs only).
  • Customizable start indent level.

Here’s a few screenshots of the plugin in action: put your mouse here and click.

dawn曙光 2024-08-26 16:45:46

如果您使用空格缩进代码,您可以尝试我的插件: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)

英雄似剑 2024-08-26 16:45:46

您可以使用制表符来显示缩进指南并在保存文件之前删除制表符:

" use 4 spaces for tabs
set tabstop=4 softtabstop=4 shiftwidth=4

" display indentation guides
set list listchars=tab:❘-,trail:·,extends:»,precedes:«,nbsp:×

" convert spaces to tabs when reading file
autocmd! bufreadpost * set noexpandtab | retab! 4

" convert tabs to spaces before writing file
autocmd! bufwritepre * set expandtab | retab! 4

" convert spaces to tabs after writing file (to show guides again)
autocmd! bufwritepost * set noexpandtab | retab! 4

You might use tabs to display indentation guides and remove tabs before saving file:

" use 4 spaces for tabs
set tabstop=4 softtabstop=4 shiftwidth=4

" display indentation guides
set list listchars=tab:❘-,trail:·,extends:»,precedes:«,nbsp:×

" convert spaces to tabs when reading file
autocmd! bufreadpost * set noexpandtab | retab! 4

" convert tabs to spaces before writing file
autocmd! bufwritepre * set expandtab | retab! 4

" convert spaces to tabs after writing file (to show guides again)
autocmd! bufwritepost * set noexpandtab | retab! 4
渔村楼浪 2024-08-26 16:45:46

也许最有效的解决方案是“绘制”缩进
使用匹配突出显示的指南。要了解它有何用处,
看一下下面的示例:

:match Search /\%(\_^\s*\)\@<=\%(\%1v\|\%5v\|\%9v\)\s/

它突出显示 - 使用“搜索”突出显示组;任何其他组
当然可以使用——第一、第五、第九(可以是
续)前面的空格字符占用的虚拟列
除了行首的空白之外什么都没有。因此,这个
为四个空格的缩进(最多为三个)生成突出显示
层次深。

唯一剩下的就是推广这个想法的过程
生成与上面类似的模式,根据
当前缓冲区的 textwidthshiftwidth 设置(按顺序)
处理更深的缩进级别并使用实际的缩进宽度。
该任务可以简单地自动化,如下面的函数所示。

function! ToggleIndentGuides()
    if exists('b:indent_guides')
        call matchdelete(b:indent_guides)
        unlet b:indent_guides
    else
        let pos = range(1, &l:textwidth, &l:shiftwidth)
        call map(pos, '"\\%" . v:val . "v"')
        let pat = '\%(\_^\s*\)\@<=\%(' . join(pos, '\|') . '\)\s'
        let b:indent_guides = matchadd('CursorLine', pat)
    endif
endfunction

每当当前缓冲区中需要缩进指南时,它
可以通过:调用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:

:match Search /\%(\_^\s*\)\@<=\%(\%1v\|\%5v\|\%9v\)\s/

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 and shiftwidth settings, in order
to handle deeper indent levels and use the actual indentation width.
This task can be simply automated as shown in the function below.

function! ToggleIndentGuides()
    if exists('b:indent_guides')
        call matchdelete(b:indent_guides)
        unlet b:indent_guides
    else
        let pos = range(1, &l:textwidth, &l:shiftwidth)
        call map(pos, '"\\%" . v:val . "v"')
        let pat = '\%(\_^\s*\)\@<=\%(' . join(pos, '\|') . '\)\s'
        let b:indent_guides = matchadd('CursorLine', pat)
    endif
endfunction

Whenever indentation guides are necessary in the current buffer, it
can be switched on via :call ToggleIndentGuides(). Of course, one
can 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, which
implements a slightly extended version of the above approach.

守护在此方 2024-08-26 16:45:46

以下命令将配置 Vim 显示点来指示
键入时的缩进级别。当这些点神奇地消失时
光标离开该行:

:set list listchars=tab:»-,trail:·,extends:»,precedes:«

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:

:set list listchars=tab:»-,trail:·,extends:»,precedes:«
花心好男孩 2024-08-26 16:45:46

试试这个 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.

痕至 2024-08-26 16:45:46

使用 Indent-Guides.vim 插件,并在需要时切换使用 ig。有时它可能会很烦人:)

use the Indent-Guides.vim plugin, and toggle use ig whenever you need it. Sometimes it could be annoying though :)

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