3

How can I swap M-x and M-q?

I can swap C-x and C-q with

;; swap C-q and C-x
(keyboard-translate ?\C-q ?\C-x)
(keyboard-translate ?\C-x ?\C-q)

The following errors out

(keyboard-translate ?\M-q ?\M-x)
(keyboard-translate ?\M-x ?\M-q)

as well as

(keyboard-translate (kbd "M-q") (kbd "M-x"))

I would expect the key references (?\C-x and ?\M-x) to be symmetric, but clearly something in my understanding is off.

EDIT: Reading @phils response and the referenced documentation, I can see that the above fails, at least in part, because there isn't a character in range to translate.

Thinking more about the problem, translation is probably not what I need but rather good old rebinding:

(global-set-key (kbd "M-q") 'execute-extended-command)
(global-set-key (kbd "M-x") 'fill-paragraph)

That is, make M-q call the command which is typically associated with M-x (execute-extended-command) and remap the displaced command (fill-paragraph).

8

A control-modified letter character still results in a character, and hence character translation works for those; but most other modified keys do not result in a character.

(characterp ?\C-q)
t

(characterp ?\M-q)
nil

Both are represented as integers, but there's a limit to the ints which are valid for characterp. See the following for details:

  • C-hig (elisp)Character Codes
  • C-hig (elisp)Keyboard Events

For translating key sequences generally, see:

C-hig (elisp)Translation Keymaps

  • +1. Good answer. – Drew Nov 4 at 19:18
0

You can use key-translation-map:

(define-key key-translation-map (kbd "M-q") (kbd "M-x"))
(define-key key-translation-map (kbd "M-x") (kbd "M-q"))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.