1

Often, insert-mode completion can take some time (e.g. <C-x><C-l> on a common prefix in your project). <C-c> interrupts this. I'd like to map another key to also have this interrupt behaviour, like so:

inoremap <C-k> <C-c>

However, it seems that on <C-k>, the mapping only gets processed after the completion finishes, contrary to <C-c>.

How can I make my mapping also interrupt insert mode completion?

Edit:

To clarify, the problem is not about detecting whether we are in completion-mode. This can be achieved with pumvisible() or complete_info(). The problem is that before the popup menu appears, that is, when vim is (slowly) scanning a completion, the mappings don't trigger until scanning completes, unlike <C-c> (defeating the purpose of mapping something to <C-c>), which immediately interrupts the scanning without waiting for scanning to finish.

1 Answer 1

0

A simple insert mode mapping can't work because triggering completion puts you in a special "submode", note the quotes, where your mappings won't work and that doesn't have its own prefixed *[nore]map commands.

Moreover, :help i_ctrl-k is a very useful command that you should probably not override.

The solution can be found under :help ins-completion-menu:

There are no special mappings for when the popup menu is visible.  However,
you can use an Insert mode mapping that checks the pumvisible() function to
do something different.  Example:
    :inoremap <Down> <C-R>=pumvisible() ? "\<lt>C-N>" : "\<lt>Down>"<CR>

In your case:

inoremap <C-k> <C-R>=pumvisible() ? "\<lt>C-c>" : "\<lt>C-k>"<CR>

which:

  • does <C-c> when you press <C-k> while the popup menu is visible,
  • does <C-k> otherwise, so there is no loss in functionality.
2
  • In fact, I read that part of the manual and attempted that previously. Unfortunately, it does not work, and the completion is not interrupted. I have also tried things like checking for complete_info(['mode']).mode, which didn't work either, which is why I ended up trying the most direct remap. The problem with the remappings is that none of them interrupts the completion, like <C-c>. You can see that if you try to initiate a very slow completion. Logically, if even the simplest remap to <C-c> doesn't work, it would follow that a more complex solution like yours doesn't work either. Commented Aug 2 at 10:07
  • 1
    Yeah you are right and I'm afraid I misread your question. The popup menu is not there yet because the completion list is currently being built, therefore pumvisible() is not true-ish yet. In that context, <C-c> is the global Ctrl-C used to interrupt long commands and I don't think it can be "re"mapped easily.
    – romainl
    Commented Aug 2 at 11:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.