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,878
  • 18
  • 28

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
  • 77,472
  • 10
  • 114
  • 243
  • 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. – 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,203
  • 1
  • 24
  • 53