I'd like to use the ESC key instead of C-g for keyboard-quit. My naïve approach to just do
(define-key global-map (kbd "ESC") 'keyboard-quit)
does not work. ESC is still a prefix key and pressing it does not call keyboard-quit.
I'd like to use the ESC key instead of C-g for keyboard-quit. My naïve approach to just do
(define-key global-map (kbd "ESC") 'keyboard-quit)
does not work. ESC is still a prefix key and pressing it does not call keyboard-quit.
You can do (define-key key-translation-map (kbd "ESC") (kbd "C-g")). I did the same a long time ago and had no problems.
Edit to improve the answer according to the comments:
If you want to keep the ESC key functionality, you can do
(define-key key-translation-map (kbd "C-<escape>") (kbd "ESC"))
One thing to note is that if Emacs hangs you still have to use the C-g key for some reason, but that happens rarely.
My advice would be to leave ESC alone, because it is a special key.
By default, Emacs uses ESC (ASCII 27) as the meta-prefix-key. From the Elisp Manual:
‘esc-map’ is the global keymap for the prefix key. Thus, the global definitions of all meta characters are actually found here. This map is also the function definition of ‘ESC-prefix’.
and
Instead, meta characters are regarded for purposes of key lookup as sequences of two characters, the first of which is ESC (or whatever is currently the value of ‘meta-prefix-char’). Thus, the key ‘M-a’ is internally represented as ‘ESC a’, and its global binding is found at the slot for ‘a’ in ‘esc-map’.
So to use ESC for something else you would need to set meta-pefix-key to something else, and also bind some other key to ESC-prefix. There may be other changes required as well -- I've never tried this.
For more on meta-prefix-char, see Functions for Key Lookup in the Elisp manual.
Lovely answer over here: https://superuser.com/a/945245/624661
Quoting:
You can use this in your Emacs init file:
;;; esc always quits
(define-key minibuffer-local-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-ns-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-completion-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-must-match-map [escape] 'minibuffer-keyboard-quit)
(define-key minibuffer-local-isearch-map [escape] 'minibuffer-keyboard-quit)
(global-set-key [escape] 'keyboard-quit)
You want to bind [escape], not (kbd "ESC"), as the bindings referenced by Ole show. In stock, this will work only if emacs frames in a window system. I think this is worth a bit of explanation.
(kdb "ESC") means actually the ASCII control character ESC, and as glucas mentioned, you certainly don't want to remap this one. But Esc is not ASCII ESC: it is symbol escape, at least in a graphical environment. How does it occur that binding (kbd "ESC") affects Esc then? This happens because emacs translates escape to ESC if there is no available binding for escape. Kind of a fallback if you wish (which is implemented by the means of local-function-key-map if you're interested in such things).
Thus, when you bind [escape], you're safe and don't have to worry about ESC; apart from your own bindings, you just have to augment the keymaps that say ESC when they mean [escape].
Hmm... well, almost. Why don't these maps use [escape] at the first time? Because this won't work in a terminal.
If you want your Esc work on a such a device, you'll have at the very least to customize your terminal before.
The story of the terminal is reported in another post:
How to bind C-[ for real?.
What you have to do is tell the terminal to send some custom sequence when Esc is pressed, then map this sequence to [escape] at an early input stage in emacs (the input-decode-map).
Hope this helps.