Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vim set color for listchars tabs and spaces

Tags:

vim

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
like image 211
Hello-World Avatar asked Jun 15 '14 17:06

Hello-World


3 Answers

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.

like image 197
romainl Avatar answered Nov 04 '22 21:11

romainl


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/
like image 9
Kent Avatar answered Nov 04 '22 19:11

Kent


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.

like image 8
Ingo Karkat Avatar answered Nov 04 '22 20:11

Ingo Karkat