1

I have the following

syn match specialNumber /1/         contained
syn match normalNumber /\d/         contained

syn match line /^\d$/               contains=specialNumber,normalNumber

hi def link specialNumber           Special 
hi def link normalNumber            Normal 

I expect this to highlight a line with 1 as Special, and a line with any other digit with Normal. But this is not the case. A line with 1 is always highlighted with Normal. What can I do to make the code match my expectation?

1 Answer 1

1

As usual, read help pages. In this particular case, it is documented under :h :syn-priority.

The order matters, and the last match has a preference, not the first one.

BTW. Here I would rather choose 1 and 2-9 patterns, so that the collision would never arise.

5
  • I already tried the "usual" and it does not work.
    – xvf99680
    Commented Jul 16, 2023 at 9:48
  • This example is a simplification of the problem to understand how to solve it. My actual normalNumber is more complicated and involves many \d's
    – xvf99680
    Commented Jul 16, 2023 at 9:49
  • @xvf99680 1) Yes, I guess you did and failed. And that's really a pity. You could, for example, open :h syntax.txt then type /priority etc.etc. Saying "it does not work" is a very poor excuse. 2) As I said, you need to take the declaration order into account. Sometimes, it's inevitable, but sometimes you can fiddle with your patterns to avoid that (at least, partially).
    – Matt
    Commented Jul 16, 2023 at 10:33
  • 1
    I miss understood you comment. I thought you were talking about the order of groups in contains. You deserve an apology. Sorry and thanks.
    – xvf99680
    Commented Jul 16, 2023 at 10:36
  • @xvf99680 The priority rules for the global scope and for a group scope are the same. contains and contained keywords only define which matches are tried and which aren't.
    – Matt
    Commented Jul 16, 2023 at 11:27

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.