I am trying to highlight only the name of pointer without '->' I tried with syn region but all the pointer and component are highlighted when i use match:

syn match PTR1 /\<[0-9A-Za-z_]\{1,\}[-][>]\>/

this will produce : ptr->inPtr-> value = 1

with region it highlight the first -> and doesn't for the rest.

syn region ptr1 start=/[0-9A-Za-z_]\{1,\}[-][>]/ skip=/[-][>]/ end=/[0-9A-Za-z_]\{1,\}/

this will produce : ptr-> inptr->value=1

How could I correct it to have only the names highlighted for example : ptr->inPtr->value = 1


I think you want to add a positive look-ahead for the -> syntax (you could also use \ze or \zs, but that tends to cause problems with syntax because the text is consumed; with a look-ahead/behind, that is not the case):

:syntax match PTR1 /\<[0-9A-Za-z_]\+\(->\)\@=\>/

Note that I've swapped \{1,\} for \+ and used \(->\): the group is necessary with \@=, which is a multi that follows an atom; we don't need to character-class - or > because they do not have special meanings by default.

Simplifying even more, \w is equivalent to [0-9A-Za-z_] (though you might want to use \k instead for 'iskeyword' characters). I also don't think the word boundaries are necessary.

:syntax match PTR1 /\w\+\(->\)\@=/

Lastly it's may be a good idea to include containedin=cBlock to prevent it from matching in the wrong places and make sure it matches in the right ones, though a few simple tests didn't reveal any issues without it.

  • Hi, it is working thank you. However I woul'd like to learn how to fish, where can I get documentations to understand for instance \@=?. Let say I want to change color for **Function_colored**(arg1, ..., argN); here only function name have been colored.
    – lilington
    Aug 19 at 6:23
  • @lilington start with :help syntax, which has nothing to do with \@=, and I would recommend peeking at the existing C syntax file, and using one of the various ways to show syntax groups at a given position (useful debugging). The rest is patterns: syntax match uses the same pattern syntax as a search. So :help pattern, or more specifically in this case :help /\@=.
    – D. Ben Knoble
    Aug 19 at 10:43
  • Yeah thanks, I thought so, I was hopping for an easier (lazier solution). I will do this than. Thank you again
    – lilington
    Aug 20 at 6:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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