2009-09-23 66 views
18

我從TextMate來到Vim,並且我想自定義我的vim colorscheme。如果我能夠找出哪些特定詞彙或符號屬於哪個高亮組合,這將非常有幫助。在TextMate的,我會放在有問題的話/符號插入符號,然後按Ctrl-Shift-P鍵和工具提示似乎說是這樣的:找出vim中某個關鍵字/符號屬於哪一個高亮組合

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

從這些信息中,實在是簡單的編輯一個TextMate顏色主題,用於將格式應用於(或移除)相關文本。

在Vim中,如果我想改變某個單詞或符號的格式,我不確定從哪裏開始。有什麼相當於TextMate的ctrl-shift-p?

回答

22

我不確定我是否理解正確,但是你在找這個嗎?

" 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

現貨 - 謝謝! – nelstrom 2009-09-23 21:14:43

+0

我只做了一個改變:爲了一致性,將命令映射到ctrl-shift-p。 'nmap :致電 SynStack()' – nelstrom 2009-09-23 21:28:36

3

UPDATE:從:help synID()(見例子):

 
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") 

據我所知,你能做的最好的是:syntax,這將給你加載的所有語法上的列表當前文件。我不知道任何可以對當前緩衝區進行合理解析的東西。

請注意,:syntax只是定義了語法項目,它是:highlight命令的用法,它給出了語法項目的顏色。

一旦你決定要做什麼改變,把它們放在~/.vim/after/syntax/<filetype>.vim。這些將在加載默認語法文件後應用您的更改。

+0

Vim的幫助文檔非常出色,但沒有您的幫助,我無法在synID上找到相關信息。謝謝。 此外,通過將語法文件放在'after'目錄中來重寫語法文件的提示非常感謝。 – nelstrom 2009-09-23 21:19:48

+0

@nelstrom - 通過「幫助vimfiles」的方式,你可以看到哪些目錄有哪些偏好(後面的部分)。 – Rook 2009-09-23 21:47:47

+0

@nelstrom - 快速查看可能是相關幫助主題的方法是在鍵入':help syn'時使用CTRL-D按鍵查看可能的完成項 - 它將向您顯示所有與字符串「同步「,這是我如何找到'synID()'。 – rampion 2009-09-24 14:01:02

12

另一種方式來獲得大量有關高亮顯示的信息:

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> 

如果我搬過來在C文件中的註釋,然後按F3,我得到:

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

這表明它位於高亮組cCommentStart中,該組與Comment相連並以綠色着色(#00ff00)。這是(修改)從here,請參閱該頁面以獲取更多信息。

+0

這太棒了。我發現這比簡單的'hi'標識符更有用。 – mybuddymichael 2011-10-03 16:43:12

相關問題