Superpose two vim syntax matches on the same character [duplicate]

浪尽此生 提交于 2019-12-11 03:15:14

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/15974439/superpose-two-vim-syntax-matches-on-the-same-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!