为插入模式创建映射,但不为自动完成子模式创建映射

nel*_*rom 14 vim

我在我的vimrc中有这些插入模式映射:

imap <C-e> <C-o>A
imap <C-a> <C-o>I
Run Code Online (Sandbox Code Playgroud)

他们制作ctrl-a并将ctrl-e光标移动到行的开头和结尾,而不会留下插入模式, emacs键绑定.

我刚刚意识到<C-e>映射引入了与自动完成子模式的冲突.CTRL-E的文档说明:

When completion is active you can use CTRL-E to stop it and go back to the
originally typed text.
Run Code Online (Sandbox Code Playgroud)

我的<C-e>映射干扰了这一点.有没有办法ctrl-e只有在自动完成不活动时才可以跳到行尾?

ib.*_*ib. 15

没有设计方法来测试 Ctrl+ X-completion模式是否有效.如果使用弹出菜单从可用完成列表中进行选择,则可接受的解决方案可能是映射

inoremap <c-e> <c-r>=pumvisible() ? "\<c-e>" : "\<c-o>A"<cr>
Run Code Online (Sandbox Code Playgroud)

(特别是在选项中menuone设置的情况下completeopt.)

一般解决方案可以基于副作用:在完成子模式中,不允许递归地进入插入模式(请参阅参考资料 :helpgrep Note: While completion).

inoremap <c-e> <c-r>=InsCtrlE()<cr>
function! InsCtrlE()
    try
        norm! i
        return "\<c-o>A"
    catch
        return "\<c-e>"
    endtry
endfunction
Run Code Online (Sandbox Code Playgroud)

  • 或者作为一个很好的表达式映射:`inoremap <expr> <ce> pumvisible()?"\ <ce>":"\ <co> A"` (2认同)