Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

vim

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-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?

like image 808
nelstrom Avatar asked Aug 03 '11 11:08

nelstrom


1 Answers

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
like image 122
ib. Avatar answered Oct 09 '22 03:10

ib.