Emacs: Swap Keys (Remap Key, translation-keymaps)

By Xah Lee. Date: . Last updated: .

Swap Keys

Here's how to swap keys.

put this in your Emacs Init File:

;; swap keys
(define-key key-translation-map (kbd "<f11>") (kbd "<f12>"))
(define-key key-translation-map (kbd "<f12>") (kbd "<f11>"))

Translation Keymaps (ELISP Manual)


Note: there's the function keyboard-translate. However, it is designed to translate character only.

So, key combination isn't a character and you can't use it for Hyper key combination. (Due to historical reasons, keyboard-translate does work for some Ctrl combination key. (thanks to Stefan Monnier [http://www.iro.umontreal.ca/~monnier/] and Deniz Dogan for this tip. (http://groups.google.com/group/gnu.emacs.help/msg/e0dc12074c776bda)))

key-translation-map or function-key-map

(global-set-key (kbd "C-b") ctl-x-map)
(global-set-key (kbd "C-x") nil)

by Johan Bockgård keyboard-translate only works for characters. You can use

(define-key key-translation-map [?\M-x] [?\M-t])
(define-key key-translation-map [?\M-t] [?\M-x])