2

I would like to highlight overlong lines in Vim (like here: https://stackoverflow.com/a/235970/1329844) as well as trailing whitespace (like here: https://stackoverflow.com/a/4617156/1329844). However, whenever I use both highlights, only the last one is applied.

I have the following code in my .vimrc:

highlight OverLength ctermbg=0 ctermfg=197
match OverLength /\%>80v.\+/
highlight ExtraWhitespace ctermbg=0
match ExtraWhitespace /\s\+$/

When I open a file, only trailing whitespace is highlighted. If I exchange the order of the two highlight/match pairs, only overlength lines are highlighted. What do I need to change so that both patterns are matched and highlighted?

Community
  • 1
  • 1

2 Answers2

3

The :match command can only have one active pattern. If both of your highlights used the same colors, you could combine the patterns with \|. Here, you have to use one of the two alternative commands: either :2match or :3match, or you can use the (newer) matchadd() function, where you can specify arbitrary numbers (> 3) as the (last) {id} argument.

:call matchadd('OverLength', '\%>80v.\+', 10, 4)
:call matchadd('ExtraWhitespace', '\s\+$', 10, 5)
Ingo Karkat
  • 154,018
  • 15
  • 205
  • 275
2

I think, Ingos solution is to be prefered, but nevertheless, you can use this:

:match MyCustomHighlight /\%(\s\+$\)\|\(\%>30v.\+\)/
:highlight MyCustomHighlight ctermbg=0 ctermfg=197
Christian Brabandt
  • 7,355
  • 22
  • 30