In Sublime Text 2, you get vertical guides where your indentation is, like this:
but Vim doesn't:
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
Unified answer:
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();
| }
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.
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 ¦ };
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)
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)
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
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.
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.
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.
let g:indent_guides_enable_on_vim_startup=1
to my .vimrc to enable this on startup.
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).
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.
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.