How do I find the syntax highlighting group applied to the character under the cursor?
Answer
:echo synIDattr(synID(line('.'),col('.'),1),'name')
Explanation
When customizing a colorscheme or debugging unexpected colors, you need to know exactly which highlight group is coloring the text under your cursor. This command reveals the name of the resolved syntax group at the cursor position, which you can then override with :highlight.
How it works
line('.')andcol('.')return the current cursor row and columnsynID(row, col, 1)returns the numeric ID of the syntax group at that position (the1means "use the resolved group after any transparent groups are followed")synIDattr(id, 'name')converts the numeric ID to a human-readable group name
Example
Place the cursor on a keyword like function in a JavaScript file, then run:
:echo synIDattr(synID(line('.'),col('.'),1),'name')
Output:
Statement
Now you know to customize it with:
:highlight Statement ctermfg=cyan guifg=#00FFFF
Tips
- Map it for quick access:
nnoremap <F10> :echo synIDattr(synID(line('.'),col('.'),1),'name')<CR> - To see the full stack of groups at the cursor (including inherited ones), use:
:echo map(synstack(line('.'),col('.')), 'synIDattr(v:val,"name")') - In Neovim, the built-in
:Inspectcommand shows the same information with richer output - Use
synIDattr(id,'fg')orsynIDattr(id,'bg')to retrieve the actual foreground/background color values