0

We can get highlight group information by the following command

:hi[ghlight] {group-name}

But is it possible to get this information by given keyword? Something like this :

syn match Integer '\d\+'
hi def link Integer Constant

:hi '3'
Integer xxx links to Constant
2

If I understand your question correctly, it's not possible to do exactly what you're asking. Vim needs the items to be in their correct context in order to determine what highlighting applies to them.

However, you can ask Vim what syntax items are currently being applied. See the example at the end of :help synID():

:echo synIDattr(synID(line("."), col("."), 1), "name")

This will output the name of the syntax item under the cursor. So instead of typing :hi '3', you'd find a 3 in your current, syntax-highlighted buffer (or add one), place the cursor on it, and run the command.

To obtain the highlighting information, you can slot the above into a :highlight command:

execute 'hi' synIDattr(synID(line("."), col("."), 1), "name")

If your purpose is debugging syntax highlighting, then you will also find the example in :help synstack() useful. I have a little wrapper command in my .vimrc containing that code so I can check syntax items quickly:

function! s:syntax_query() abort
  for id in synstack(line("."), col("."))
    echo synIDattr(id, "name")
  endfor
endfunction
command! SyntaxQuery call s:syntax_query()

You can modify the echo line similarly, if you want this also to display highlighting info:

execute 'hi' synIDattr(id, "name")
  • Thank you, it's really helpful. You guessed it, I was looking for a way to debug syntax highlighting, but I didn't explain it well. – Firmin Martin May 3 '18 at 0:23

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.