Vim syntax files ... trying to understand "contains"

I am trying to fix a new vim syntax file for the specific user format that I am using. I can understand most of them, but the keyword “contains” is causing me problems.

Is there anyone here who could give me an explanation of what he is doing (I read the help → did not quite understand) as if he were explaining this tree.

+8
vim
Nov 30 '09 at 10:30
source share
1 answer

In general, you can use only one way to highlight syntax in one place. Therefore, to use C-like syntax as an example, if you define a region that should start with the opening bracket '{' and end with a closing bracket '}', the syntax highlighting for that region will be the same.

contains= allows you to configure other syntax highlighting groups that should be contained in the external group. To follow the previous example, you can select an int even if it is in an external region. Then you might have something like:

 syn keyword Keyword int syn region BraceBlock start='{' end='}' contains=Keyword 

Quite often, you need to add items later in your keyword list. There are several ways to do this. First, you can use contains=ALL or contains=ALLBUT,Error so that everything is in the region. Secondly, you can use containedin to insert something into the contents of another area:

 syn region BraceBlock start='{' end='}' syn keyword Keyword int containedin=BraceBlock 

Thirdly, you can define everything that is “contained” as valid in this group:

 syn region BraceBlock start='{' end='}' contains=CONTAINED syn keyword Keyword int contained 

Finally, you can use clusters that make it easy to decide what happens:

 syn region BraceBlock start='{' end='}' contains=@MyCluster syn keyword Keyword int syn cluster MyCluster contains=Keyword syn keyword Conditional if else syn cluster MyCluster add=Conditional " Now conditionals and keywords can appear in a BraceBlock 

Not knowing exactly what you want to understand, I'm not sure what else to say - what are you trying to achieve and what causes problems?

+21
Nov 30 '09 at 11:03
source share



All Articles