70

In Sublime Text 2, you get vertical guides where your indentation is, like this:

Sublime Text screenshot

but Vim doesn't:

vim screenshot

Is there a way to show lines like that in Vim?

By the way, my indentation is 2 spaces.

:set tabstop=2 softtabstop=2 shiftwidth=2 expandtab

6 Answers 6

57

Unified answer:

If you are using tabs

I'd try using listchars:

:set listchars=tab:\|\ 
:set list

note: there is a space after the last \ above.

This method is good because it uses Vim's built-in support for showing this kind of thing.

Example output:

|       if (1) {
|       |       func();
|       }

screenshot of text sample above

However, this doesn't work for leading spaces. As pointed out by @Josh Petrie, there is a patch to work for spaces, but then you'd have to recompile Vim, which is beyond the scope of this answer.

If you are using spaces, and don't mind using a plugin

Vaibhav Sagar points out the plugin indentLine performs this functionality.

Install in the normal way (see installing Plugins) and it seems to magically just work without further input. If you don't mind the ASCII bars, you don't need to do anything with your fonts. Example output of your example after setting your indentation settings and running :IndentLinesReset

45   ¦ var move = function(direction) {
46   ¦ ¦ if(!initialized) { initialize(); }
47 
48   ¦ ¦ if(shiftBoard(direction)) {
49   ¦ ¦ ¦ setTimeout(addRandomPiece, 100);
50   ¦ ¦ }
51   ¦ };

screenshot of example with spaces

The plugin requires Vim 7.3 or later, and makes cleaver use of that version's Conceal feature. Instead of actually hiding text, it replaces text with bars in the view, but not the actual buffer. The result is you see the bars but they don't get saved into the file (since they are actually spaces)

Caveat

Both of these methods assume "properly" indented code. They don't place the indentation lines at the level of indent when a "block" is opened (which sublime appears to do) but instead places lines at regular intervals based on your indentation settings. Additionally, it only shows the bars on lines where there is actually space (which is why the line in the vim output under the first if has no bars, making a visual gap)

9
  • 1
    Actually, I'm using spaces to indent instead of tabs. I've updated my question.
    – aharris88
    Commented Feb 9, 2015 at 22:23
  • 1
    There is a patch to make listchars work for non-trailing spaces, which you can dig up on the mailing list. It has not, however, been accepted into the official distribution :(
    – user72
    Commented Feb 9, 2015 at 23:52
  • 1
    Updated answer to include information from Vaibhav Sagar and Josh Petrie. I tried out the plugin Vaibhav mentioned and show the results in the edited answer. I think I'm going to start using this plugin myself. It's not fantastic, but in general seems to work pretty well.
    – John O'M.
    Commented Feb 10, 2015 at 2:11
  • 1
    Is it possible to get the guides to appear on blank lines like they do in sublime?
    – James
    Commented Feb 10, 2015 at 22:53
  • 3
    indentLine works alright for python, json, viml, c, c++, etc. But unfortunately it doesn't work with vim-javascript, so I vim-indent-guides instead.
    – Sassan
    Commented Sep 1, 2016 at 17:32
55

Another option I use, which isn't exactly what you asked for, but accomplishes the same purpose of making it easier to see what is in the same column, is to set the following in your vimrc:

set cursorcolumn
set cursorline

cursorcolumn illustration

This has the trade off that you have to move your cursor to the column you want to see, but you get less visual clutter. Usually when I care about indentation levels, I'm keeping my place with the cursor anyway. My way also isn't as awkward-looking in languages like python without closing braces.

1
  • 4
    Python vi users will die for this feature
    – KRoy
    Commented Feb 24, 2018 at 0:48
22

For the sake of completeness, there is also the Indent Guides plugin, which shows the indentation levels by alternating the colors of white spaces. It requires version 7.2 or later. Since it uses the tabstop and shiftwidth variables, it will probably work for your indentation settings right away.

Imgur

Once the plugin is installed, you can simply use:IndentGuidesEnable or :IndentGuidesDisable. If you are using terminal vim, note that, according to the manual,

At the moment Terminal Vim only has basic support. This means is that colors won't be automatically calculated based on your colorscheme. Instead, some preset colors are used depending on whether background is set to dark or light.

In order to change the colors of the guides for the screenshot, I used the following

let g:indent_guides_auto_colors=0

so the colors are not recalculated when buffers are swapped, and then set the colors using

hi IndentGuidesEven ctermbg=238
hi IndentGuidesOdd ctermbg=236

As with the other solutions presented, it only shows the indent guides on lines were there are spaces.

1
  • 2
    add let g:indent_guides_enable_on_vim_startup=1 to my .vimrc to enable this on startup.
    – inye
    Commented Apr 6, 2017 at 13:24
10

I've found the indentLine plugin to be the best for this. You do need a font with the appropriate symbols though, I find Consolas on Windows and Menlo on Mac OS X work well. As is pointed out in the unified answer above, it works by (ab)using vim's conceal feature to put vertical bars (from the font used) in the right place. I'm pretty happy with this solution but the disadvantage to it is that my vertical bars are part of the box-drawing glyphs and they are center-aligned instead of left-aligned like in other editors. I've gotten used to it but it can be fixed by manually patching your font or using a font like Fantasque Sans Mono (note how the screenshots in the linked github repo use indentLine).

1
  • Which glyph is the patched vertical line? Can you paste it here? Thanks.
    – Timtro
    Commented Mar 7, 2018 at 16:02
5

The indentLine plugin tends to be fairly slow on non-small files, observable when clicking enter as it lags by a second or so.

I've found vim-indentguides to be a faster and visually similar plugin, that also visually distinguishes tab indents versus space indents.

4

Try the most recent updated of Goneovim, not the current release 0.4.6.
It's not pure vim but a neovim gui, and it does not gain much attention. The indent guides feature of goneovim is native, and works much better than vim/neovim plugins. enter image description here

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.