Search code examples
vimsyntax-highlightingvim-syntax-highlighting

How do I mark text bold inside an already matched region in VIM syntax highlighting?


Given something like:

This is \f random text \b and more \f* and then some more.

  • I want to make \f up until \f* grey
  • I want the "tags" as bold (so they are grey but also become bold)

I can't get the priorities right so both the bolding and the colourisation occur.

syn match footnoteEnd /\\f\*/
syn match footnoteStart /\\f/
syn match footnoteBTag /\\f/
syn region footnoteInfo start=/\\f/ end=/\\f\*/

hi def footnoteInfo guifg=grey
hi def footnoteStart gui=bold
hi def footnoteBTag gui=bold
hi def footnoteEnd gui=bold

Any help would be much appreciated.


Solution

  • You could try this:

    syn match footnoteDelimiter /\\f\*\?/ contained
    syn match footnoteBTag /\\b/ contained
    syn region footnoteInfo start=/\\f/ end=/\\f\*/ contains=footnoteDelimiter,footnoteBTag keepend
    
    hi def footnoteInfo guifg=grey
    hi def footnoteBTag gui=bold guifg=grey
    hi def footnoteDelimiter gui=bold guifg=grey
    

    Explanation:

    • When Vim reaches the end of the region, it matches footnoteStart again before footnoteEnd and then * never gets bold. That's why I suggest using footnoteDelimiter instead.

    • The contained argument was added to footnoteDelimiter and footnoteBTag because they occur into a region.

    • contains=footnoteDelimiter,footnoteBTag specifies which syntax groups are allowed into the region.

    • keepend makes the matching of an end pattern of the outer region also end any contained item.