Question

I'm defining the following syntax rules in a vimscript:

hi MyBold term=bold   gui=bold
hi MyRed  ctermfg=red guifg=#ff0000

And later on, I want to apply both highlights to the same character. So basically, I tried the following:

syn match MyRed  /\%1c\%1l./
syn match MyBold /\%1c\%1l./

but then the second match replaces the first match. So, I further read the help on syntax match, and also the answer to that question and I tried playing with the syn match parameters:

syn match MyRed  /\%1c\%1l./ contains=CONTAINED
syn match MyBold /\%1c\%1l./ contained

but it still behaves the exact same way.

So, how could I use two syntax matches to change independently the background and the foreground of the same character?

Was it helpful?

Solution

You can't. For each character, Vim only uses one particular highlight group, determined by the last, "most inner" non-transparent syntax group match.

To work around this, define a combined highlight group and corresponding :syntax commands.

:hi MyBoldRed cterm=bold ctermfg=red gui=bold guifg=#ff0000

Some syntaxes (e.g. $VIMRUNTIME/syntax/html.vim, and various Wiki formats) use that for combining bold, italic and underline, but it gets tedious and repetitive after that. But remember that Vim is not meant for WYSIWYG; syntax highlighting just provides some hints.

Edit: There's a patch proposed that would add a combine modifier for :syntax commands; it's in Bram's Todo list.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top