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 trail:x (replace x with the character you want to use for a trailing space), which can be useful as well.)

  • 4
    Note: I like to use set listchars=tab:!·,trail:·, IMHO · is a lot clearer than a - ... You need a fairly recent Vim version for this to work btw (not sure which exactly, I think 7.3.x)... – Martin Tournoij Feb 5 '15 at 15:07
  • 5
    I like using the unicode character as it is slightly smaller than >. Currently I see trailing white space by marking the eol (though I may try the above). My settings are: set listchars=eol:¬,tab:▸\ and – Martin York Feb 6 '15 at 21:43

The basic answer is :set list, which causes tabs to display as ^I.

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 of g++ to appear under the first p of cpp (assuming tabstop=8).

Therefore, I suggest that in your .vimrc you put something like :set listchars=tab:␉· (To be clear, that's Unicode character U+2409 SYMBOL FOR HORIZONTAL TABULATION followed by U+00B7 MIDDLE DOT.)

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 listchars:

:set listchars=eol:⏎,tab:␉·,trail:␠,nbsp:⎵

These characters are distinctive enough that they rarely appear literally in document text.

To type these special characters into your vimrc, type Ctrl-vu23CE while in Insert Mode, as explained in :help utf-8-typing.

  • 2
    See also set lcs=tab:├─ – sch Feb 5 '15 at 15:33

Highlight tabs

Temporary Tab highlighting

For occasional use, one can simply highlight all tabs in a document using the following search / command:

/\t

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/

More highlighting

For more of these useful highlighting commands —including combined with trailing whitespace and making this permanent— see the Vim wiki.

  • 3
    You can also use :match Error /\t/ to highlight tabs (or any other pattern for that matter) independently from the search register and hlsearch (type :match to clear it). – Martin Tournoij Aug 25 '16 at 22:56
  • 1
    @Carpetsmoker One can find a whole lot more of these useful highlighting commands (including combined with trailing whitespace!) on the Vim wiki about highlighting unwanted spaces. – Serge Stroobandt Aug 26 '16 at 19:05

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.

(Image taken from the link above)

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.

  • 2
    I believe this plugin is intended for spaces indenting though? – Jake Byman Dec 11 '17 at 18:02

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

:set invlist 

to toggle between regular display and list display.

For example:

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

You may want to show tabs differently in a regular terminal and gvim.

set list!
if has('gui_running')
    set listchars=tab:▶\ ,trail:·,extends:\#,nbsp:.
else
    set listchars=tab:>.,trail:.,extends:\#,nbsp:.
endif

(Adding for completeness, in case others find it useful).

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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