42

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

Sublime Text screenshot

but in vim I obviously don'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
39

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)

  • Actually, I'm using spaces to indent instead of tabs. I've updated my question. – aharris88 Feb 9 '15 at 22:23
  • Oh. Well then, that will require more research. – John O'M. Feb 9 '15 at 22:25
  • 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 Feb 9 '15 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. Feb 10 '15 at 2:11
  • 1
    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 Sep 1 '16 at 17:32
19

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.

  • Python vi users will die for this feature – shuva Feb 24 '18 at 0:48
18

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.

  • add let g:indent_guides_enable_on_vim_startup=1 to my .vimrc to enable this on startup. – inye Apr 6 '17 at 13:24
9

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

  • 4
    Please take a look at our discussion of plugin-link-only answers and consider updating your answer with more information. – user72 Feb 10 '15 at 0:26
  • My mistake, I've updated my answer. – Vaibhav Sagar Feb 10 '15 at 9:52
  • Which glyph is the patched vertical line? Can you paste it here? Thanks. – Timtro Mar 7 '18 at 16:02
2

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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