1

I built the following highlight definitions (thanks to an answer in this question):

" #a6e22e
syntax match tagFunctionDefinition "\v\<"
syntax match tagFunctionDefinition "\v\>"
syntax match tagFunctionDefinition "\v/"
hi def link tagFunctionDefinition Function

" #f6266e
syntax match tagOperator "\v\<\/?\zs\w+\ze\/?>"
hi def link tagOperator Operator

The first is to highlight the < > and / in a file. The second to hightlight characters between < >s.

For some reason the first hightlight definition is neutralizing the second (the second works if I comment out the first).

What could be the cause?

(I tried swapping their positions, but does nothing.)

Community
  • 1
  • 1
alexchenco
  • 53,565
  • 76
  • 241
  • 413

1 Answers1

1

Syntax highlighting is a bit different. In particular, the \zs / \ze parts obscure matches at those positions, even when they are not part of the (highlighted) match. That's what you're seeing. One solution would be to fall back to the (slower) lookahead / lookbehind, but I think a different solution is more appropriate here.

As it stands, your tagFunctionDefinition would also match standalone < or > characters; that's probably not what you want. To match them only inside a complete tag, make those syntax definitions contained:

syntax match tagFunctionDefinition "\v\<" contained
syntax match tagFunctionDefinition "\v\>" contained
syntax match tagFunctionDefinition "\v/" contained
hi def link tagFunctionDefinition Function

Then, drop the match-limiting \zs and \ze, and contains above definitions in there:

syntax match tagOperator "\v\</?\w+/?\>" contains=tagFunctionDefinition
hi def link tagOperator Operator

Notes

  • You don't need to escape the literal / here (as your chosen delimiter is "); only in a / search.
  • If your syntax has similarities to HTML / XML, have a look at the corresponding $VIMRUNTIME/syntax/html.vim syntax scripts for inspiration.
  • What's your fixation about \v? In this particular example, they make the patterns even more complex!
Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • Wow, thanks. You've helped me a lot with Vim. Are you a Vim developer? Or you've used it since kindergarten? – alexchenco Feb 19 '15 at 15:49
  • I took it from Learning VimScript the Hard Way: http://learnvimscriptthehardway.stevelosh.com/chapters/46.html I think using `\v` makes the regex look more like the regex in other languages like Perl and JavaScript? – alexchenco Feb 19 '15 at 15:54
  • @alexchenco: well ironically enough, I followed the talk Larry Wall (Perl designer) gave at fosdem and they said they were redesigning the regex language because it was too complicated ;). – Willem Van Onsem Feb 19 '15 at 16:04
  • @alexchenco: Thanks; glad I could help. I use Vim since 2002, well after graduating from university. More [here](http://ingo-karkat.de/software%20development/contributions.html#vim). – Ingo Karkat Feb 19 '15 at 17:23