Find out to which highlight-group a particular keyword/symbol belongs in vim

后端 未结 3 1337
南方客
南方客 2020-12-08 15:34

I am coming to Vim from TextMate, and I would like to customise my vim colorscheme. It would be really helpful if I could find out to which highlight-group(s) any particular

相关标签:
3条回答
  • 2020-12-08 16:09

    Another way to get lots of information about the highlighting:

    map <F3> :echo "hi<" . synIDattr(synID(line("."),col("."),1),"name") . '> trans<' . synIDattr(synID(line("."),col("."),0),"name") . "> lo<" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"name") . ">" . " FG:" . synIDattr(synIDtrans(synID(line("."),col("."),1)),"fg#")<CR>
    

    If I move over a comment in a C file and press F3, I get:

    hi<cCommentStart> trans<cCommentStart> lo<Comment> FG:#00ff00
    

    which shows that it is in the highlight group cCommentStart, which is linked to Comment and coloured in green (#00ff00). This is (modified) from here, see that page for more information.

    0 讨论(0)
  • 2020-12-08 16:10

    I'm not sure I understood right, but are you looking for this ?

    " adds to statusline
    set laststatus=2
    set statusline+=%{synIDattr(synID(line('.'),col('.'),1),'name')}
    
    " a little more informative version of the above
    nmap <Leader>sI :call <SID>SynStack()<CR>
    
    function! <SID>SynStack()
        if !exists("*synstack")
            return
        endif
        echo map(synstack(line('.'), col('.')), 'synIDattr(v:val, "name")')
    endfunc
    
    0 讨论(0)
  • 2020-12-08 16:24

    UPDATE: From :help synID() (see the example):

    synID({line}, {col}, {trans})                           *synID()*
                    The result is a Number, which is the syntax ID at the position
                    {line} and {col} in the current window.
                    The syntax ID can be used with |synIDattr()| and
                    |synIDtrans()| to obtain syntax information about text.
                    {col} is 1 for the leftmost column, {line} is 1 for the first
                    line.
                    When {trans} is non-zero, transparent items are reduced to the
                    item that they reveal.  This is useful when wanting to know
                    the effective color.  When {trans} is zero, the transparent
                    item is returned.  This is useful when wanting to know which
                    syntax item is effective (e.g. inside parens).
                    Warning: This function can be very slow.  Best speed is
                    obtained by going through the file in forward direction.
    
                    Example (echoes the name of the syntax item under the cursor):  
                            :echo synIDattr(synID(line("."), col("."), 1), "name")
    

    As far as I know, the best you can do is :syntax, which will give you a listing of all the syntax loaded for the current file. I don't know of anything that will give the syntatical parsing of the current buffer.

    Note that :syntax just defines the syntax items, it's uses of the :highlight command that give the coloring for a syntax item.

    Once you've decided what changes you want to make, put them in ~/.vim/after/syntax/<filetype>.vim. These will apply your changes after the default syntax files are loaded.

    0 讨论(0)
提交回复
热议问题