I would like to make my Vim completion menu a little more IDE-like, and I was able to achieve this using the bindings suggested here. However, the <Esc> binding causes a bit of a problem:

inoremap <expr> <Esc> pumvisible() ? "\<C-e>" : "\<Esc>"

The page warns:

Word to the wise, mapping over can cause an issue in terminal vim where arrow keys will print letters A through D along with a newline rather than moving. To avoid the issue, simply omit the mapping over .

Indeed! And scrolling in insert mode does some crazy $#!^ that I don't even know how to describe. The culprit seems to be that the up arrow is secretly bound to ^[OA, and rebinding <Esc> rebinds the ^[ part. I thought the whole point of inoremap is that it won't affect other bindings, but what do I know...

Anyway, apart from those side-effects, the setting does exactly what I want. Is there some way I can have my cake and eat it too?

  • Mapping esc is problematic because any key which uses escape sequences will be affected. The noremap variants don’t use other mappings, but can still affect other mappings. Furthermore, the arrow keys are « mapped » at all, but rather send special sequences to the terminal that it and vim know how to interpret. The mapping breaks vim’s ability to do so.
    – D. Ben Knoble
    Commented May 5, 2019 at 17:29
  • @D.BenKnoble I see, that makes sense and explains why the problem occurs in spite of inoremap. Is there a way to modify the command so that it follows the logic "only rebind <Esc> when the completion menu is open"? (I thought that's what this command was supposed to do, but obviously I don't know how it works.) Commented May 5, 2019 at 20:35
  • This maps escape in insert mode to take a particular action depending on whether or not the completion menu is open. The semantics are subtle but crucial. Im not aware of any such solution. You might be able to work with the autocmd events MenuPopup, CompleteChanged, and CompleteDone to set up / tear down mappings.
    – D. Ben Knoble
    Commented May 5, 2019 at 20:37
  • +1 for "some crazy $#!^ .. to describe" :D
    – B Layer
    Commented May 6, 2019 at 0:33


Your Answer

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

Browse other questions tagged or ask your own question.