Hello-World
Hello-World

Reputation: 9545

vim set color for listchars tabs and spaces

Using the concept of the code below in VIM How do I make the tabs a light grey background and a the spaces a blue background

" part of ~/.vimrc
" highlight tabs and trailing spaces
set listchars=tab:>-,trail:-
set list

Upvotes: 44

Views: 26123

Answers (4)

DimeCadmium
DimeCadmium

Reputation: 340

I've had this in my vimrc for a long time, probably from elsewhere on StackOverflow; it highlights extraneous whitespace as red:

 highlight ExtraWhitespace ctermbg=red guibg=red
 match ExtraWhitespace /\s\+$\| \+\ze\t\|[^\t]\zs\t\+/

To turn this into what the question specifically asks for, highlighting whitespace at the end gray:

 highlight SpecialKey ctermbg=blue guibg=blue
 highlight ExtraWhitespace ctermbg=lightgray guibg=lightgray
 match ExtraWhitespace /\s\+$/

Upvotes: 1

Ingo Karkat
Ingo Karkat

Reputation: 172540

You can't, at least not in an easy way that is 100% correct.

As @romainl has said, Vim only offers one highlight group for all listchar types representing characters, viz. SpecialKey.

The workaround implementation of @Kent has several shortcomings:

  • It only works for the first / current window; you'd need :autocmds to define it for new windows, too.
  • It does not consider whether 'list' is actually set; hooking into its changes isn't directly possible.
  • It clobbers the :match slots; this could be prevented via matchadd(), but its use is more complex.

So, think hard whether that feature is really worth it.

Upvotes: 8

romainl
romainl

Reputation: 196536

If you use those options in your ~/.vimrc you most certainly have read :help 'list' and :help 'listchars' but from your question it's not really clear if you spotted the last two lines of :help 'listchars':

The "NonText" highlighting will be used for "eol", "extends" and
"precedes".  "SpecialKey" for "nbsp", "tab" and "trail".

Tabs and trailing spaces use the same highlight group, though, so you won't be able to give them different background colors with those highlight groups. But you don't really need different colors, here, because you already set them to be represented by different characters.

Upvotes: 45

Kent
Kent

Reputation: 195039

this is one way:

first create two hi-groups:

:hi GroupA ctermbg=blue ctermfg=.. guibg=...
:hi GroupB ctermbg=gray ctermfg=.. guibg=..

then

:match GroupA / \+$/
:2match GroupB /\t/

Upvotes: 11

Related Questions