Is vim's syntax highlighting limited by regexes or can you, for example, highlight [ words_and_maybe_keywords... [ words_and_maybe_keywords... ] words_and_maybe_keywords... ] all the same (e.g., green) all based on this region being demarcated by some outermost [/] pair?

IOW, can it handle regexes + depth?

If this is supported, how can I highlight such a nestable region?

  • 1
    Yes; help syntax, probably especially regions and contained
    – D. Ben Knoble
    Commented Feb 28, 2021 at 17:38
  • @D.BenKnoble Thanks. That's encouraging. But shouldn't then there be syntax files utilizing that ability? I've tried looking at the syntax files for pascal and matlab, both of which, I'm told, have nestable comments, but the comments, when they have nested, aren't highlighted correctly for either language. :/ (I've added a pascal example to the question). Commented Mar 1, 2021 at 22:38
  • Well, it works fine in other langs; perhaps those syntax rules are broken? Not even SE’s highlighter works too well on that example
    – D. Ben Knoble
    Commented Mar 1, 2021 at 22:39
  • @D.BenKnoble I take it back contains=pascalTodo,pascalSpaceError,pascalComment does make it work, I was invoking it wrong. Thanks for the help, D. Ben Knoble! Commented Mar 1, 2021 at 22:42
  • 1
    np; add an answer if you’ve solved your problem. And, for the future, maybe start with the extra details :)
    – D. Ben Knoble
    Commented Mar 1, 2021 at 22:43

1 Answer 1


I've tried looking at a syntax file which might be using the feature: pascal.vim, because pascal, as I've learned, has nested comment sections. Unfortunately, the highlighting for the comment section is wrong when there's nesting in the comments.

Example hello_world.pas:

program HelloWorld(output);
    Write('Hello, World!')
    {No ";" is required after the last statement of a block -
    { nested comment }
        adding one adds a "null statement" to the program, which is ignored by the compiler.}

This compiles and runs, but the comment section with nesting is highlighted wrong, even here on the StackOverflow.

I found the relevant line in the syntax file:

syn region pascalComment    start="(\*\|{"  end="\*)\|}" contains=pascalTodo,pascalSpaceError

and just naively added ,pascalComment to the contains attribute` and it did make vim highlight the comment region correctly.

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.