Consider the text:
First we highlight the regular expression
:syntax match Special /foobar/
Next we highlight the regular expression
:syntax match Error /foobarbaz/
This match begins in the same position as our
Special group, but it was defined later, and so it takes precedence. (See `:help :syn-priority.)
Now let's clear that (
:syntax clear Error), and instead try starting our highlighting at
bar by using
\zs to set the start of the match.
:syntax match Error /foo\zsbarbaz/
Okay, that didn't work, but perhaps it's because by altering the start of the match, this match now begins inside our earlier group. If that's the case, we can fix it with the
:syntax match Error /foo\zsbarbaz/ containedin=Special
That seems to have worked!
Now let's try starting our highlighting from the
:syntax match Error /foobar\zsbaz/ containedin=Special
Huh. Now that we've moved the start of our
Error match past the end of the
Special match, it no longer works. (Note that I'm not using the
contained argument, so even if the
Error group starts outside of the
Special group, it should still match: removing the
containedin argument also has no effect.)
The sole mention of
syntax.txt doesn't appear to be relevant (it regards multi-line patterns), and
:help \zs also doesn't shed any light on this.
What's going on?
N.B. For this particular example, I could use an
hs syntax pattern offset to achieve the desired highlighting, and with more complicated regular expressions I could use
\@<= look-behind. But
\zs is recommended over
\@<= for performance reasons, and in any case, I'd still like to understand how the feature works.