How can I retrieve the syntax id under the cursor, in all cases?

Indeed, the synID() function doesn't take into account highlightings due to the :match* commands or the matchadd() function.

Example, in a C source file:

void myfunc(char c)
{ }

Let's say I run in Vim:

:call matchadd('SpecialKey', '\<myfunc\>')

Then, when the cursor is on the word char:

:echo synID(line('.'), col('.'), 1)

But when the cursor is on the word myfunc:

:echo synID(line('.'), col('.'), 1)
  • 2
    That is not possible, because matches are totally different then syntax items. To retrieve the active matches, use the getmatches() function. Feb 13 '17 at 6:24
  • @ChristianBrabandt : but getmatches() gives only infos about the match definition, not about the actual matches
    – yolenoyer
    Feb 13 '17 at 19:31
  • What do you mean with about the actual matches? I fail to see what is missing Feb 13 '17 at 19:38
  • I mean: with infos given by getmatches(), it's impossible to retrieve the syntax group which is under the cursor. To follow the example in my question., when i'm on the word myfunc, i need to retrieve the name SpecialKey, or its id (which is 1 on my vim)
    – yolenoyer
    Feb 13 '17 at 19:59
  • And by the return value of getmatches() you know, whether it would match myfunc and you know to which highlighting group it belongs. I still don't see the problem Feb 13 '17 at 20:23

getmatches() provides the match info including the pattern and match id. Run the patterns on the current line and check if cursor position is included in the match locations.

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.