20

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?

nelstrom
  • 18,802
  • 13
  • 54
  • 70

3 Answers3

23

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
Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Rook
  • 60,248
  • 49
  • 165
  • 242
  • I just made one change: mapping the command to ctrl-shift-p for the sake of consistency. `nmap :call SynStack()` – nelstrom Sep 23 '09 at 21:28
20

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.

DrAl
  • 70,428
  • 10
  • 106
  • 108
4

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.

rampion
  • 87,131
  • 49
  • 199
  • 315
  • The help docs for Vim are outstanding, but I couldn't have found the information on synID without your help. Thanks. Also, the tip about overriding syntax files by putting them in the `after` directory is much appreciated. – nelstrom Sep 23 '09 at 21:19
  • @nelstrom - by way of "help vimfiles" you can see which directories have preferences over which (the \after part). – Rook Sep 23 '09 at 21:47
  • @nelstrom - a quick way to see what might be a relevant help topic is to use the CTRL-D keystroke to see possible completions when typing `:help syn` - it'll show you all the help topics that match the string "syn", which is how I found `synID()`. – rampion Sep 24 '09 at 14:01
  • How to revert "Once you've decided what changes you want to make, put them in `~/.vim/after/syntax/.vim`." ? Please. If I comment that changes, then the highlight is `cleared`, instead of just revert to keep the **original plugin color-scheme**. – Xopi García Dec 28 '20 at 20:45