3

I am playing around with vim syntax, matching expressions like name.subname regardless of their layout. Here is a match pattern I came upon which does exactly what I want:

enter image description here

Now, I wish that subname would be the only colored part of my matches. This is a job for \zs, right? Here is the result:

enter image description here

Why is this so? Why does \zs break my pattern? How can I get all subnames in the first section colored and not the ones in the second section?

4

Well, here is my explanation: there are restrictions on multi-line patterns for syntax highlighting: \zs cannot be used if it makes it jump to a line which is not where the pattern started:

:helph syn-multi-line

And here is a neat workaround with the keyword contains

highlight link SubGroup Error
syntax match SubGroup '\<subname\>' contained
syntax match Constant '\<name\>[ \n]*\.[ \n]*\<subname\>' contains=SubGroup

enter image description here

Also, here is another, maybe more efficient one: only highlight the end of the pattern:

syntax match Error "\<name\>[ \n]*\.[ \n]*\<subname\>"he=-6

it seems to randomly color or not the intermediate . though.

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.