Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I find out the "syntax group" of the current character? [duplicate]

Tags:

vim

Sometimes when using vim I'd like to know why the syntax highlighting is coloring some text in a certain way. I'd like to be able to put my cursor on this text and enter some command to find out what syntax group it belongs to.

For example, I noticed that the first 50 characters of the first line in a gitcommit buffer (ie: a git commit message) was being highlighted in an odd color. I'd like to know what syntax group is being used for these characters so I can adjust my (custom) color scheme.

like image 783
Laurence Gonsalves Avatar asked Sep 13 '25 04:09

Laurence Gonsalves


1 Answers

The following command will output both the name of the syntax group, and the translated syntax group of the character the cursor is on:

:let s = synID(line('.'), col('.'), 1) | echo synIDattr(s, 'name') . ' -> ' . synIDattr(synIDtrans(s), 'name')

To make this more convenient it can be wrapped up in a custom command, function, or key binding.

How this works:

  • line('.') and col('.') return the current position
  • synID(...) returns a numeric syntax ID
  • synIDtrans(s) translates s by following highlight links
  • synIDattr(s, 'name') returns the name corresponding to the numeric syntax ID

This will output something like:

gitcommitSummary -> Statement
like image 132
Laurence Gonsalves Avatar answered Sep 16 '25 05:09

Laurence Gonsalves