3

In a normal mode key mapping I'd like to match the ESC and only this key, not key sequences like caused by the arrow keys.

Background: I thought I'd torture myself in the process of avoiding excess ESC presses in normal mode by force-quitting vim.

My naive approach was:

:nmap <Esc> :q!<CR>

And it's actually working quite well1, except, when trying to navigate with the arrow keys in normal mode, vim quits too. I suppose it's because vim is converting the arrow keys into escape sequences internally, and then handling the <Esc> key before the rest of the sequence.

1: on another system with some 7.4 release, vim exits on startup because something issues an escape key (sequence), probably.

Reproducible with:

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Jun 21 2019 04:10:35)
Included patches: 1-197, 322, 377-378, 550, 649, 651, 703, 706-707
Extra patches: 8.1.1401, 8.1.1382, 8.1.1368, 8.1.1367, 8.1.1366, 
8.1.1365, 8.1.1046, 8.1.0613, 8.1.0547, 8.1.0546, 8.1.0544, 
8.1.0540, 8.1.0539, 8.1.0538, 8.1.0506, 8.1.0208, 8.1.0206,  
8.1.0205, 8.1.0189, 8.1.0177, 8.1.0067, 8.1.0066

(Ask for the list of included/missing features, if required.)

No plugins loaded, ensured with --noplugin.

I was learning via Fandom's vim key mapping tutorial and by searching this and other Stacks, so I might not have understood things well enough, yet.

How can I ignore <Esc> in escape sequences or match a single <Esc> or even debounce key sequences?

1 Answer 1

2

vim is converting the arrow keys into escape sequences internally

Not quite. This is the issue with all escape mappings: your terminal is sending the escape sequences, which vim knows to interpret as arrow keys. Flip the logic a little. Best answer: dont map escape.


I’m not aware of any way for vim to distinguish the different kinds of Escape presses. It does this internally via a really good guess, some timeout settings, and the terminfo database (in other words, it depends heavily on the terminal emulator and the terminal being emulated).

2
  • Of course, though I thought vim would convert the terminals sequences to single codes since there's <Left>, <Right>, ... (and not <Esc><A> or whatever). Thanks for your answer. Aug 10, 2019 at 17:57
  • 1
    <Left> is an internal vimscript notation that works regardless of what escape sequences are used (xterm, vt100, etc.). But i can see where the confusion lies.
    – D. Ben Knoble
    Aug 10, 2019 at 18:22

Your Answer

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

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