Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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?

like image 589
nelstrom Avatar asked Sep 23 '09 17:09

nelstrom


People also ask

How to highlight on vim?

Press 1 to highlight the current visually selected text, or the current word (if nothing is selected). Highlight group hl1 is used. Press 2 for highlight hl2 , 3 for highlight hl3 , etc. Press 0 to remove all highlights from the current visually selected text, or the current word.

Why are some words highlighted in vim?

Most filetypes (like python) in Vim come with a syntax that defines highlight groups (see them via :highlight ). A colorscheme then provides combinations of foreground / background color and/or formatting like bold and italic, for terminals, color terminals, and/or GVIM.

Where is vim syntax?

vim comes bundled with Vim and is available in /usr/share/vim/vim72/syntax/python. vim , if an alternative version is instead loaded from ~/. vim/syntax/python.

How to change syntax color in vim?

You can change color schemes at anytime in vi by typing colorscheme followed by a space and the name of the color scheme. For more color schemes, you can browse this library on the vim website. You can enable or disable colors by simply typing "syntax on" or "syntax off" in vi.


2 Answers

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
like image 163
Rook Avatar answered Oct 12 '22 23:10

Rook


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.

like image 44
DrAl Avatar answered Oct 12 '22 23:10

DrAl