2012-03-17 94 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