How to make tabulation look different than whitespace in vim (highlighted for example).
That would be useful for code in Python.
How to make tabulation look different than whitespace in vim (highlighted for example).
That would be useful for code in Python.
Use the list
and listchars
options, something like this:
:set list
:set listchars=tab:>-
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).
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 ...
.
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
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.