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.
Best Answer
Setting
escape-time
to zero interferes with tmux recognizing function-keys. Its manual page saysThe term "function key" applies to anything that has that format (including pageup, used in scrolling by tmux, and cursor-keys). 500 milliseconds may be excessive if you never work remotely. 20 milliseconds is workable for local connections. The analogous
ESCDELAY
in ncurses defaults to 1000 milliseconds; only a very small fraction of users find it necessary to change that.