1

Attempting to bind M-ESC M-ESC in the global map to a command:

(global-set-key (kbd "M-ESC M-ESC") #'my-command)

results in the following:

Debugger entered--Lisp error: (error "Key sequence M-ESC M-ESC starts with non-prefix key M-ESC ESC")

I supposed this is due to meta-prefix-char being ESC by default, so M-<char> gets translated into ESC-<char>. Therefore, M-ESC M-ESC is ESC ESC ESC ESC, and ESC ESC ESC is already bound to keyboard-escape-quit.

Is there a safe way to separate the Meta and ESC keys so a binding like M-ESC M-ESC is possible? If so, how?

Tianxiang Xiong
  • 3,848
  • 16
  • 27
  • @Drew What do you mean? – Tianxiang Xiong Feb 24 '17 at 18:38
  • Done. It's simple: `(global-set-key (kbd "M-ESC M-ESC") #'my-command)` – Tianxiang Xiong Feb 24 '17 at 23:32
  • This is similar to trying to bind `(kbd "ESC ESC ESC ESC")`, which tells you that `ESC ESC ESC` is not a prefix key. It's not a prefix key because it is not bound to a keymap. See [(elisp) Prefix Keys](http://www.gnu.org/software/emacs/manual/html_node/elisp/Prefix-Keys.html). – Drew Feb 24 '17 at 23:42

2 Answers2

2

It's not a prefix key because it is not bound to a keymap. See (elisp) Prefix Keys.

Try this:

(define-prefix-command 'foo)
(global-set-key (kbd "M-ESC") 'foo)
(global-set-key (kbd "M-ESC M-ESC") 'forward-char)
Drew
  • 75,699
  • 9
  • 109
  • 225
  • Cool, but now `ESC ESC ESC` is no longer bound to `keyboard-escape-quit`. – Tianxiang Xiong Feb 24 '17 at 23:57
  • Yes. And that's one reason that we don't recommend that you bind `ESC` in such ways. `ESC` is kind of hard-coded to do some pretty important things in Emacs. So while you can rebind things you probably don't necessarily want to do it. – Drew Feb 24 '17 at 23:59
  • Does `meta-prefix-char` have a role to play here? Maybe changing it to something else will allow us to bind `M-ESC` to a prefix map and still keep `ESC ESC ESC`? – Tianxiang Xiong Feb 25 '17 at 00:49
  • Yes, it will, but I don't recommend that. You can certainly try it. But be aware of the consequences. See [(elisp) Functions for Key Lookup](http://www.gnu.org/software/emacs/manual/html_node/elisp/Functions-for-Key-Lookup.html). – Drew Feb 25 '17 at 01:36
0

Is there a safe way to separate the Meta and ESC keys so a binding like M-ESC M-ESC is possible? If so, how?

You could bind <escape> instead:

(define-key global-map (kbd "M-<escape>  M-<escape>") #'my-command)

This only works in a GUI frame though.

npostavs
  • 9,033
  • 1
  • 21
  • 53