When I work on files in python, I sometimes mistake tabs for spaces and vice versa. Is there a way I can get Vim to display a character for a tab instead of a tab?
Yes, use listchars:
set list set listchars=tab:>-
If you put these two lines in your .vimrc, tabs will be shown as
> for the start position and
- through the rest of the tab.
(Sidenote: listchars can also show trailing spaces with
x with the character you want to use for a trailing space), which can be useful as well.)
The basic answer is
:set list, which causes tabs to display as
However, I recommend going beyond that. Just
:set list is problematic in that it fails to preserve visual alignment on screen. For example:
%.o: %.cpp ^Ig++ -c $<
doesn't look good, especially when you expect the
g++ to appear under the first
As explained in
:help listchars, a two-character setting for
tab:xy causes visual alignment to be preserved:
tab:xy Two characters to be used to show a tab. The first char is used once. The second char is repeated to fill the space that the tab normally occupies. "tab:>-" will show a tab that takes four spaces as ">---". When omitted, a tab is show as ^I.
The example above would appear on screen as:
%.o: %.cpp ␉·······g++ -c $<
Personally, I go all-out with my
eol, I use U+23CE RETURN SYMBOL
trail, I use U+2420 SYMBOL FOR SPACE
nbsp, I use U+23B5 BOTTOM SQUARE BRACKET
These characters are distinctive enough that they rarely appear literally in document text.
To type these special characters into your
vimrc, type Ctrl-vu
23CE while in Insert Mode, as explained in
Temporary Tab highlighting
For occasional use, one can simply highlight all tabs in a document using the following search
To remove the highlighting, simply type
:noh which is short for
:nohlsearch —no high lighting.
Permanent Tab highlighting
" Highlight tabs as errors. " https://vi.stackexchange.com/a/9353/3168 match Error /\t/
For more of these useful highlighting commands —including combined with trailing whitespace and making this permanent— see the Vim wiki.
I personally like the use of the plugin indentLine, which displays a vertical line at indentation levels.
It is of great help especially in python, where the indentation is important.
Among other things, it provides a toggle command, that can be mapped to a key combination, so that you can turn it off, when you don't need it.
Easiest method is to do
:set list, which will show tabs as
^I and end of line as
I like to use a mapping that calls
to toggle between regular display and list display.
:nmap <leader>l :set invlist<cr>
This allows me to quickly check if there are tab vs space problems and then go back to a regular display.
I combined @john-om approach with this character Unicode Character 'ZERO WIDTH SPACE' (U+200B) and indentLine plugin. So, if I use spaces, plugin show nice formated indents, if I use tabs, pipes are the only visible mark and that is what I want. I can also toggle tab's marks.
" indicate tabs :nmap <leader>l :set invlist<cr> set list listchars=tab:❘⠀,trail:·,extends:»,precedes:«,nbsp:×
This can be easily achieved using
listchars by specifying the tab as:
,tab:\ \ ┊,
By providing two spaces in front of your
vim will assume that is the repeating character. No need for yet another plugin.
Here's an example of what it might look like using the following complete
set listchars=eol:↓,tab:\ \ ┊,trail:●,extends:…,precedes:…,space:·