Is it possible to display Indentation guides in Vim?

隐身守侯 提交于 2019-11-27 06:10:29

NOTE: 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.

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
Yggdroot

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)

This will show dots to indicate your indentation level as you type. The dots magically disappear as you leave the line.

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

E.g:

def test():
....print('indentation level 1')

Pretty cool huh?

Probably the most effective solution would be to "draw" indentation guides using match-highlighting. To understand how it helps, look at this example:

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

It highlights (using Search highlighting group, it's possible to use any other, of course) the first, the fifth, the ninth (and it can be continued) virtual column occupied by space symbol preceding nothing but whitespace from the beginning of line. So, this produce four-space indentation highlighting for three levels in deep.

To generalize this idea it remains only to generate pattern like that mentioned above according to the current buffer's textwidth and shiftwidth (to handle deeper indent levels and proper indent width). This task could 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

Every time you need indentation guides in a buffer, it can be switched on by :call ToggleIndentGuides(). Of course, you can change highlighting group (or create a special one for use in indentation guides only), setup a handy mapping1 for that and/or call it from autocmd for some file types.


1 Indentation guides highlighting configuration from my .vimrc file: https://gist.github.com/734422

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!