Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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]"
like image 985
Miroslav Popov Avatar asked Sep 13 '15 09:09

Miroslav Popov


People also ask

What is syntax highlighting in Vim?

Syntax highlighting enables Vim to show parts of the text in another font or color. Those parts can be specific keywords or text matching a pattern. Vim doesn't parse the whole file (to keep it fast), so the highlighting has its limitations.

How do I highlight the current word in Vim?

To automatically highlight the current word, type z/ .


1 Answers

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 *.

like image 164
romainl Avatar answered Oct 16 '22 23:10

romainl