I have a language which uses statement like


I can do the following in the corresponding synthax file

syn keyword LangKeys WHILE DO END_WHILE
syn region  LangBlock start="WHILE" end="END_WHILE" fold transparent

which allows me to highlight correctly the different keywords as well as defining a given region.

But now, I would like to be sure that the DO is only highlighted in the context of the loop. But I don't want to affect anything else. I tried

syn keyword LangKeys WHILE END_WHILE
syn keyword InsideKeys DO contained
syn region LangBlock start="WHILE" end="END_WHILE" fold transparent contained=InsideKeys

also without the transparent option. I tried a few variations of that. But it does not produce the expected result.

How can I get the highlight to be applied only when the keywords are within a given context?

Alternatively, is there a better way to handle that?

  • Which language is this? Perhaps there's already a syntax file floating around somewhere? Nov 9, 2015 at 13:48
  • In this case, it is the Structure Control Language (SCL) for Siemens S7 PLC. But I have met a similar problem, for example for modelica. Nov 9, 2015 at 13:55

2 Answers 2


The difference isn't so large, but the following seems to be working

syn keyword LangKeys   contained WHILE END_WHILE
syn keyword InsideKeys contained DO
syn region  LangBlock  fold transparent 
        \ matchgroup=LangKeys 
        \ start="\<WHILE\_s" end="\<END_WHILE\>" 
        \ contains=InsideKeys
hi def link LangKeys   Statement
hi def link InsideKeys Statement

That way, a DO inside the WHILE group will be highlighted but not separated.

  • The keyword LangKeys part should not be required.
    – Vitor
    Nov 11, 2015 at 15:22
  • @Vitor, why not? I use them as matchgroup, and in my tests, without it, you would have DO highlighted, but WHILE and ENd_WHILE would take on the transparent feature... Nov 12, 2015 at 6:11
syn region LangBlock matchgroup=LangKeys start="\<WHILE\>" end="\<END_WHILE\>" fold transparent contains=InsideKeys
syn keyword InsideKeys DO contained

Things that I changed in the syntax code you provided:

  1. The \< \> are required to only match words, such that WHILE is not found inside END_WHILE.
  2. The matchgroup argument sets the syntax group name to be used in the match start/end words.
  3. Remove the keywords for WHILE and END_WHILE, because the highlight is done by the respective syn-match command.

Obviously, both LangKeys and InsideKeys syntax groups need to be defined using the highlight command with something like the following:

highlight link LangKeys   Statement
highlight link InsideKeys Statement

For more information please check:

:help syn-match
:help hi-link
  • You have LangKeys in your matchgroup, where do you define them? Nov 12, 2015 at 12:35
  • The original question does not include highlight commands and it is said that it works, so I assume that there is no doubt regarding that part.
    – Vitor
    Nov 12, 2015 at 12:56
  • I don't think you understand what I want to say. Indeed the question does not specify highlighting. Nevertheless, in your example, you have syn region LangBlock matchgroup=LangKeys .... I might be mistaken, but I think that this calls for another syn line with LangKeys defined. Doesn't it? Nov 12, 2015 at 13:01
  • It doesn't and I explain that in my answer in the second point.
    – Vitor
    Nov 12, 2015 at 13:04
  • No, you don't really explain it. At least not clearly enough. So you could have matchgroup=FooBar instead, it would just work the same? Nov 12, 2015 at 13:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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