How to get group name of highlighting under cursor in vim?

后端 未结 4 1200
粉色の甜心
粉色の甜心 2020-12-15 05:28

I usually customize existing colorscheme to meet my needs.

If I could get the syntax group name under cursor, it would help me a lot, just like Firebu

4条回答
  •  天涯浪人
    2020-12-15 06:24

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

    function! SynGroup()                                                            
        let l:s = synID(line('.'), col('.'), 1)                                       
        echo synIDattr(l:s, 'name') . ' -> ' . synIDattr(synIDtrans(l:s), 'name')
    endfun
    

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

    How this works:

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

    This will echo something like:

    vimMapModKey -> Special
    

提交回复
热议问题