Mac – Is it possible to get ESC to behave as an actual escape key

emacskeyboard

So I have finally switched to Emacs, not so much because I'm yet convinced it is in itself the better editor but because it certainly does have more powerful extensions. I am still using vim-mode though, perhaps that's part of my problem… but I really don't intend to abandon the modes-approach, so I'll probably stay with it.

I'm getting along quite well, but one thing I find really unnerving is the behaviour of the esc key (which I have in the shift-lock position). I'm used to relying on this a lot as more or less a "panic key", which may not be nice but I find allows me to work generally quite a bit less caring about the keystrokes themselves, and thus faster.

What I'd like this key to do is just get me out of any minibuffer or special editing mode into a well-defined normal state. Perhaps most importantly, I would like it to not do anything unrelated,

  • Simulate meta. What do I have an alt key for?
  • Close windows I'm not even in at the time.
  • Getting interpreted as the final key in some key sequence.

Is it possible to turn all that off and make esc an actual escape key? Vim-mode does make it behave kind of as I like in some situations, but especially when other plugins are involved this often breaks.

Alternatively, are there different options that might suit my kind of workflow?

Best Answer

The behavior for which you're looking is embodied in the Emacs Lisp function keyboard-quit, bound by default to C-g (Control-g). Its behavior is roughly equivalent, in the context of Emacs, to the usual function of C-c in a shell; it will interrupt whatever Lisp code is running, and drop you back to the top level. (In particular, if you're being prompted for text entry in the minibuffer and decide you don't want to do whatever it is that's prompting you after all, C-g is how you cancel it.)

A single press of the Escape key, by default, acts as a Meta prefix key; that is, a keybinding which involves the Meta key (e.g. M-x, execute-extended-command) will work just as well if you press Alt-x, or if you press Escape, then press X -- Emacs doesn't differentiate. Triple-pressing the Escape key will run keyboard-escape-quit, which is like keyboard-quit but with more of a "do what I mean" behavior -- something which I've never needed in several years of using Emacs, and indeed didn't know about until a few minutes ago, but you may find it useful.

Speaking of things you may find useful: You can obtain documentation on any Emacs function via the describe-function function, invoked as C-h f FUNCTION-NAME RET (that is, press and release Control-h, press f, type in a function name at the prompt, press Return), or as M-x describe-function RET FUNCTION-NAME RET. For variables, it's the same, except C-h v or describe-variable, plus the variable name; for keystrokes, it's C-h k or describe-key, and you type the key chord whose binding you want described. These three functions, plus C-h m to retrieve documentation on the current buffer's major and minor modes, are enormously useful in discovering how Emacs works and what it can do, and I enthusiastically recommend them to you as the best and quickest way to reduce the degree to which Emacs seems alien and intimidating.

Now, finally, to answer your actual question: It may be possible to reconfigure Emacs' default key mapping, such that it differentiates the Escape key and the Meta key. However, I don't really recommend you try to do so, for a couple of reasons.

First, while I'm sure there is a way to do so, it is necessarily going to be obscure and tricky, and it will have a lot of potential to go wrong in non-obvious ways -- something which, while fine for Emacs veterans, isn't really advisable when you're trying to learn the editor. Worse, if you try to do it in the naive and obvious fashion (e.g. M-: (global-set-key (kbd "ESC") 'keyboard-quit) RET), you'll break Meta key behavior entirely. Actually getting it to work right will require descending into the lowest levels of Emacs' input event processing -- wear a headlamp, bring a machete, and pack a lunch. (Maybe two lunches.)

Second, even if you can get that binding working reliably, it's very much non-standard, whereas any Emacs you use, anywhere, will certainly have C-g bound to keyboard-quit. Especially while learning the editor, it's advisable to get as familiar as you can with the default keybindings, instead of rebinding things in a fashion which will only work with your own Emacs init files. (If you can guarantee either that you'll never use an Emacs anywhere but on your own box, or that you'll always be able to install your init files, great! Go for it. Also, for this purpose, a Vim mode which is part of the Emacs standard library counts as "default", because you can invoke it on any properly installed Emacs via M-x vim-mode RET or similar.)

tl;dr: You can probably do that with ESC, but it'll take horrible hackery to do so, assuming it's possible at all, and you're better off getting used to the facilities Emacs provides by default for invoking the behavior you want.

Related Question