2012-03-17 78 views
2

我試圖讓vim讓我通過tab鍵自動填充彈出列表。它適用於標籤,但不適用於s-tab(shift-tab)。 看起來像shift-tab某種程度上取消了應用C-P之前的自動完成菜單vim omnicomplete與shift-tab不工作?

任何人有任何想法?

function InsertTabWrapper(direction) 
    if pumvisible() 
    if "forward" == a:direction 
     return "\<C-N>" 
    else 
     return "\<C-P>" 
    endif 
    endif 
    let col = col('.') - 1 
    if !col || getline('.')[col - 1] !~ '\k' 
    return "\<tab>" 
    else 
    return "\<c-x>\<c-o>" 
    endif 
endfunction 

inoremap <tab> <c-r>=InsertTabWrapper("forward")<cr> 
inoremap <s-tab> <c-r>InsertTabWrapper("backward")<cr> 

回答

5

<c-r><s-tab>映射後錯過了等號 「=」。

不過,我建議做這樣的:

function! InsertTabWrapper() 
    if pumvisible() 
    return "\<c-n>" 
    endif 
    let col = col('.') - 1 
    if !col || getline('.')[col - 1] !~ '\k' 
    return "\<tab>" 
    else 
    return "\<c-x>\<c-o>" 
    endif 
endfunction 
inoremap <expr><tab> InsertTabWrapper() 
inoremap <expr><s-tab> pumvisible()?"\<c-p>":"\<c-d>" 
  1. 使用<expr>映射。這是更好看,更清晰的(很多人不知道<c-r>=事情
  2. 映射<s-tab>這樣,你可以在插入模式做取消縮進
+3

好像有人用你的代碼留下深刻印象:。HTTP:/ /moviecode.tumblr.com/post/76605632708/the-intro-sequence-of-the-anime-time-of-eve – MOnsDaR 2014-02-14 08:53:44

+2

OMG!我在RSS feed上看到帖子,但並沒有意識到這是來自我的回答,儘管。 – tungd 2014-02-14 12:32:12