2

The following syntax rules:

" #f6266e
syntax match tagOperator "\v\<\/?\w+\s*[^>]*\>" contains=tagFunction,tagFunctionDefinition
hi def link tagOperator Operator

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

produce this:

enter image description here

As you can see the html attributes names are highlighted as the html tags (each, onsubmit). So I added a syntax to fix that (turn them to blue):

" #66d9ef
syn keyword tagFunction var this
syn match tagFunction "/\v\<\/?\w+\s+\w+\/?>" contained
hi def link tagFunction Define

But nothing happens, the color stay the same. I even used contains and contained so I'm not sure what's the problem. Is the tagFunction regex wrong?

2
  • What are you trying to match with the regex? Commented Feb 20, 2015 at 14:41
  • @JLILIAman the HTML attributes names in my example code each, onsubmit. Commented Feb 20, 2015 at 15:08

1 Answer 1

2

Your pattern starts with a / (and also escapes the second / as \/, which isn't necessary); you probably forgot to remove those when copying from the search history:

syn match tagFunction "\v\<\/?\w+\s+\w+/?>" contained

You can easily verifying your patterns by first yanking the pattern yi", then assigning that to the search register :let @/ = @@. The n command will then jump to the next match and (with :set hlsearch) highlight all matches.

Further critique

Note that your tagOperator pattern is more general and includes (because of the [^>]* part) any tagFunction part. This ambiguity is dangerous, as now the correct matching depends on the ordering of the two :syntax commands. In this case, I think you can just make a tagAttribute contained in tagOperator, and define it roughly like this:

:syn match tagAttribute "\w\+={[^}]*}" contained
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks man. You've become my Vim hero. By the way I tried doing: tagAttribute "\w\+\s\+" to only highlight each, onsubmit. But the other words are also highlighted (e.g. item). I noticed you stopped using zs and ze after I included the contains. How can I do it to only highlight the attribute's names?
Can there be multiple attributes (foo={bar} hi={ho})? Then, it's best to anchor the pattern to before the =: tagAttribute "\w\+\ze=". Your attempt will match (within tagOperator) all words followed by whitespace.
ha, you're right. I wrote something similar. Thanks again.
Yeah, what a coincidence. It shows you're on the right way!
Yeah. Wow, I've learned a lot about regexs from you, more than in college. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.