开发者

How to create a mapping for Insert mode but not for the autocomplete submode in Vim?

开发者 https://www.devze.com 2023-03-25 03:23 出处:网络
I have these insert mode mappings in my .vimrc file: imap <C-e> <C-o>A imap <C-a> <C-o>I

I have these insert mode mappings in my .vimrc file:

imap <C-e> <C-o>A
imap <C-a> <C-o>I

They make Ctrl开发者_如何学JAVA-A and Ctrl-E move the cursor to the start and end of the line without leaving insert mode, a la emacs keybindings.

However, I just realized that the Ctrl-E mapping introduces a conflict with the autocompletion submode. The documentation in :help complete_CTRL-E states:

When completion is active, you can use CTRL-E to stop it and go back to the originally typed text.

Thus, my Ctrl-E mapping interferes with this. Is there a way that I can make Ctrl-E jump to the end of the line only if auto-completion is not active?


There is no proper way to test whether the Ctrl+X-completion mode is active or not. However, the following two workarounds are possible.

1. If one uses the popup menu to choose from the list of available completions (especially in the case of menuone set in the completeopt option), an acceptable solution might be the mapping

inoremap <expr> <c-e> pumvisible() ? "\<c-e>" : "\<c-o>A"

2. A general solution can be based on a side effect: In the completion submode, it is disallowed to enter Insert mode recursively (see :helpgrep Note: While completion), so if an attempt to do so fails, we can suppose that we are in the midst of a completion:

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

精彩评论

暂无评论...
验证码 换一张
取 消