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.
C-g
to unhang it, the ESC
remapping dosn't work in this case.
ESC
so this didn't bother me. But if you need it you can do another translation, for example (define-key key-translation-map (kbd "C-<escape>") (kbd "ESC"))
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.
meta-prefix-char
to nil
on OSX will not disable it from being a prefix key -- it will only separate it from the meta key. On OSX with a graphical version of Emacs, I like to have the escape key separated from the meta key -- I set (setq meta-prefix-char nil)
at the outset of my initialization so that all subsequently loaded libraries understand that is what I want. This gives me the ability to use the meta key as prefix key, and the escape key as a prefix key. The esc-map
is defined at the C-source code level within keymap.c
. It is meant to be used as a prefix key.
esc-map
prefix to preserve the default M- bindings?
(setq ns-alternate-modifier 'meta)
, which makes meta the left alt key on the Apple keyboard. I like to use the right alt key to create unicode characters using OSX default settings: (setq ns-right-alternate-modifier 'none)
-- to insert Spanish characters, paragraph symbols, etc. When building --with-ns
, both the left and right alt keys are set to meta. The default setting ties the escape key to the meta key on OSX using the meta-prefix-char
, which is 27
as you stated.
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.
universal-argument
with theuniversal-argument-map
.