7

I'm trying to tune the highlighting in my vim and I'm trying to set some syntax groups via the match command and highlight them in my vimrc file, because those groups are generic and do not belong to any particular language syntax file.

But my approach works only for a single highlighting, as soon as I add another instance of a highlight/match pair the previously declared ones do not work, only the last one works until I add another highlight/match.

This is the code (the relevant part) of my vimrc. This example works:

hi NewLineWin ctermfg=248 guifg=#999999
match NewLineWin /\r\n/

And this doesn't (only the last pair of hi/match does):

hi NewLineWin ctermfg=248 guifg=#999999
match NewLineWin /\r\n/
hi WhiteSpaceChar ctermfg=252 guifg=#999999
match WhiteSpaceChar / /

Can anyone help me with this, how to make it work?

8

From :help :match

Highlighting matches using :match are limited to three
matches (aside from :match, :2match and :3match are
available). matchadd() does not have this limitation and in
addition makes it possible to prioritize matches.

So you can use:

hi NewLineWin ctermfg=248 guifg=#999999
match NewLineWin /\r\n/
hi WhiteSpaceChar ctermfg=252 guifg=#999999
2match WhiteSpaceChar / /

Or:

hi NewLineWin ctermfg=248 guifg=#999999
match NewLineWin /\r\n/
hi WhiteSpaceChar ctermfg=252 guifg=#999999
call matchadd("WhiteSpaceChar", " ")

matchadd() also allows further flexibility, such as deleting or prioritising matches. See :help matchadd() for more details.

Alternatively, look into syntax highlighting: :help syntax

3

What about using the

:sy[ntax] match {group-name} [{options}] [excludenl] {pattern} [{options}]

:hi NewLineWin ctermfg=248 guifg=#999999
:syntax match NewLineWin /\r\n/
:hi WhiteSpaceChar ctermfg=252 guifg=#999999
:syntax match WhiteSpaceChar / /

You can use any number of such match...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.