Miroslav Popov
Miroslav Popov

Reputation: 3383

Vim - How to highlight a part of a pattern

I want to highlight an object in Vim C#.

I do the following in cs.vim:

syn match csObject  "[A-Z][_a-zA-Z0-9]*\.[A-Z]"

And highlight it in my color theme

hi csObject     guifg=#ff0000

However it paints also the . and the first letter from the next "word".

enter image description here

How to highlight only the match before the dot?

EDIT Thanks to the @romainl answer I found out that \zs sets the start of a match and \za sets the end of a match.

That's allowed me to make the match properly:

syn match csObject         "[ \t\(\!]\zs[A-Z][_a-zA-Z0-9]\{-}\ze\.[A-Z]"

Upvotes: 5

Views: 514

Answers (1)

romainl
romainl

Reputation: 196466

You only need one tiny modification to your regular expression to solve your issue.

Your pattern actually covers Application, the ., and the following uppercase letter. What you should do is use \ze to mark the end of the "useful" part of your pattern:

syn match csObject "[A-Z][_a-zA-Z0-9]*\ze\.[A-Z]"

Also, I would use \{-} instead of the too greedy *.

Upvotes: 5

Related Questions