I can define different highlight groups for different documents.

For example,

  • in a general text document, I would define highlight groups as Header1, Header2, Header3, and Normal Text, with different colors for each.

  • in a code example document, I would define highlight group as ClassName, FunctionName, VariableName, and KeyWord etc, with different colors for each.

Is it possible to make both in the same document? Some paragraphs are for general context, some are for code example.

The difficult is in different context, the way to notice the highlight groups are different. For example, in code example context, I would notice abc as def pattern, and take abc as VariableName highlight group, as as Keyword, and def as ClassName group. However, in general text content, as is a frequently appearing word. I find hard to separate them.

Is it possible to have a context mode for highlight groups? For example a document starts as Normal Text mode, where a bunch of highlight group recognition rules apply, then it can be toggled to Code Example mode where another set applies?

  • I think your question boils down to writing a syntax file that is aware of multiple syntaxes, is that correct?
    – D. Ben Knoble
    Commented Oct 6, 2023 at 20:53

1 Answer 1


It is possible.

The following answer and answer will gives you some hints.

You'll have to clarify how to déterminé which syntax must be applies where.

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.