2

I'm trying to improve the cmake-syntax-highlighting for vim.

I'm unable to have vim highlight a keyword which is inside a text which is already matching a region. I'm talking about variables:

For example CMAKE_SOURCE_DIR is a built-in variable which is listed in a syn keyword cmakeVariable-list.

When accessing values of a variable in cmake you use ${...}. Highlighting this is done with a region:

syn region cmakeVariableValue start=/\${/ end=/}/
        \ contained oneline contains=CONTAINED,cmakeTodo,cmakeVariable

Both highlightings work, except when mixed together. Keywords listed in cmakeVariable are not highlighted specially when being in a ${}-region.

How can I tell vim to apply special highlighting in a region when a keyword is used?

2

You need to use the contained argument in your keyword definition. Here's a random snippet that works for me:

syntax keyword toBeContained contained foo bar
syntax region fooRegion start="{" end="}" contains=toBeContained

hi toBeContained ctermfg=51 guifg=cyan
hi fooRegion ctermfg=30 guifg=darkcyan

Note: This will make keyword toBeContained not work when it's not in a region that contains it. So you might need to make two definitions, one with contained and one without.

I recommend this great tutorial on creating syntax files, as well as :help :syn-keyword for more information.

  • Works as described. Now I just need to find out how to use it for my cmake-syntax-highlighting file ;-) – Patrick B. May 1 '17 at 13:40
  • It works, it was a question of ordering the regions correctly. – Patrick B. May 1 '17 at 15:09

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.