5

I'm trying to tweak my highlighting. Vim highlighting is pretty complex, as I understand, lots of rules run to determine the category of a bit of text, then those rules are mapped and possibly mapped again through to a final category, which is then mapped to a treatment dependent on the environment (term/cterm/gui)!

As I'm on cterm there are more combinations than colours, so you get duplicates and it's not always obvious which highlighting category ("group"?) has been applied where.

I would like to put my cursor over a highlighted word and run a command to see what highlight rules matched and how that got mapped to a final treatment.

Is that a feature? Is it possible?

artfulrobot
  • 20,637
  • 11
  • 55
  • 81
  • possible duplicate of [Find out to which highlight-group a particular keyword/symbol belongs in vim](http://stackoverflow.com/questions/1467438/find-out-to-which-highlight-group-a-particular-keyword-symbol-belongs-in-vim) – glts Mar 13 '15 at 13:21

2 Answers2

4

Create such mapping:

nm <silent> <F1> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name")
    \ . '> trans<' . synIDattr(synID(line("."),col("."),0),"name")
    \ . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name")
    \ . ">"<CR>

And use F1 on any token to see the highlighting.

svlasov
  • 9,923
  • 2
  • 38
  • 39
  • That's amazing! vimscript is like voodoo to me - no idea! So it outputs `hi` (name of highlighting group), and `trans` and `lo`? – artfulrobot Mar 13 '15 at 11:40
  • Yes, doing it on `while` keyword in C displays `hi trans lo` – svlasov Mar 13 '15 at 11:43
  • What do trans and lo mean? – artfulrobot Mar 13 '15 at 11:51
  • 2
    `trans` represents transparent item highlighting, and `lo` is for inherited highlighting. Usually they are the same, but not always. Read more `:h transparent` – svlasov Mar 13 '15 at 12:17
  • Any reason it doesn't echoes the `Search` highlight group when hlsearch is on? @svlasov – Moshe Jun 07 '21 at 15:04
  • @Moshe `Search` is a _highlight group_, while `synID*` functions work for _syntax groups_ only, defined with `:syntax` commands. Syntax and highlight groups are not the same thing. – svlasov Jun 07 '21 at 21:56
0

To find out which syntax group causes highlighting, :syn list shows all active groups, but it's easier when you install the SyntaxAttr.vim - Show syntax highlighting attributes of character under cursor plugin. I find this plugin indispensable for troubleshooting and developing syntax plugins! (Basically, it's an advanced version of @svlasov's answer.)

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324