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

后端 未结 4 1201
粉色の甜心
粉色の甜心 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:09

    There is this function that was floating around the web when I was doing the same thing:

    function! SynStack()
      if !exists("*synstack")
        return
      endif
      echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
    endfunc
    
    0 讨论(0)
  • 2020-12-15 06:21

    Here's a mapping that will show the hierarchy of the synstack() and also show the highlight links. press gm to use it.

    function! SynStack ()
        for i1 in synstack(line("."), col("."))
            let i2 = synIDtrans(i1)
            let n1 = synIDattr(i1, "name")
            let n2 = synIDattr(i2, "name")
            echo n1 "->" n2
        endfor
    endfunction
    map gm :call SynStack()<CR>
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-12-15 06:26

    Try this:

    " diagnostics {{{
    if has('balloon_eval')
        nnoremap <F12>           : setl beval!<CR>
        set bexpr=InspectSynHL()
    endif
    fun! InspectSynHL()
        let l:synNames = []
        let l:idx = 0
        for id in synstack(v:beval_lnum, v:beval_col)
            call add(l:synNames, printf('%s%s', repeat(' ', idx), synIDattr(id, 'name')))
            let l:idx+=1
        endfor
        return join(l:synNames, "\n")
    endfun
    "}}}
    
    0 讨论(0)
提交回复
热议问题