tags:

views:

271

answers:

6

How to make tabulation look different than whitespace in vim (highlighted for example).

That would be useful for code in Python.

+5  A: 

Use the list and listchars options, something like this:

:set list
:set listchars=tab:>-
Greg Hewgill
+13  A: 

I use something like this:

set list listchars=tab:»·,trail:·,precedes:…,extends:…,nbsp:‗

Requires Vim7 and I'm not sure how well this is going to show up in a browser, because it uses some funky Unicode characters. It's good to use some oddball characters so that you can distinguish a tab from something you may have typed yourself.

In addition to showing tabs, showing spaces at the end of lines is useful so you know to remove them (they are annoying).

Brian Carper
wow I was looking for this a couple weeks back, but didn't really find anything (I didn't really search extensively)... thanks :)
Terence Honles
can you tell us how to enter those characters into your .vimrc?
glenn jackman
Copy/paste is easiest, or `Ctrl-V u####` where #### is a 4-digit unicode code point.
Brian Carper
+3  A: 

If you do the following:

:set list

then all TAB characters will appear as ^I and all trailing spaces will appear as $.

Using listchars, you can control what characters to use for any whitespace. So,

:set listchars=tab:...

in conjunction with :set list makes TABs visible as ....

ars
+2  A: 

Also, when cutting and pasting text around, it's useful to disable the display of tabs and spaces. You can do that with

:set list!

And you enable it again with repeating the command.

+7  A: 

Many others have mentioned the 'listchars' and 'list' options, but just to add another interesting alternative:

if &expandtab == 0
    execute 'syn match MixedIndentationError display "^\([\t]*\)\@<=\( \{'.&ts.'}\)\+"'
else
    execute 'syn match MixedIndentationError display "^\(\( \{' . &ts . '}\)*\)\@<=\t\+"'
endif
hi link MixedIndentationError Error

This will look at the current setting of 'expandtab' (i.e. whether you've got hard tabs or spaces pretending to be tabs) and will highlight anything that would look like correct indentation but be of the wrong form. These are designed to work by looking at the tab stops, so tabs used for indentation followed by spaces used for simple alignment (not a multiple of 'tabstop') won't be highlighted as erroneous.

Simpler options are available: if you just want to highlight any tabs in the wrong file in bright red (or whatever your Error colour is), you could do:

syn match TabShouldNotBeThereError display "\t"
hi link TabShouldNotBeThereError Error

or if you want spaces at the start of a line to be considered an error, you could do:

syn match SpacesUsedForIndentationError display "^ +"
hi link SpacesUsedForIndentationError Error

Just a few more thoughts to add to the mix... more information here:

:help 'expandtab'
:help 'tabstop'
:help 'listchars'
:help 'list'
:help :exe
:help let-option
:help :hi-link
:help :syn-match
:help :syn-display
Al
+2  A: 

glenn jackman asked how to enter the characters (I'm assuming he means characters like "»").

Brian Carper suggests a method using the character's Unicode index number. Since many of these distinctive-looking characters are digraphs [ :help digraphs ], you can also use the CNTL-k shortcut, which is generally easier to remember.

So, for example, you can generate the "»" in Insert mode by typing CNTL-k and the ">" character twice.

Godfrey Parke