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

泪湿孤枕 提交于 2019-11-27 01:20:28

问题


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 word or symbol belongs. In TextMate, I would place the caret on the word/symbol in question, then hit ctrl-shift-p and a tool tip would appear saying something like:

text.html.basic
meta.tag.structure.any.html
string.quoted.double.html

From this information, it is really straightforward to edit a TextMate color theme to apply (or remove) formatting to the text in question.

In Vim, if I want to change formatting for a certain word or symbol, I'm not sure where to start. Is there anything equivalent to TextMate's ctrl-shift-p?


回答1:


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



回答2:


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.




回答3:


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.



来源:https://stackoverflow.com/questions/1467438/find-out-to-which-highlight-group-a-particular-keyword-symbol-belongs-in-vim

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!