ARCHIVED: In Emacs, how do I swap two keys?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

In Emacs, you can swap two keys (or key sequences) by using the keyboard-translate function. For example, to turn C-h into DEL, you would add this line to your .emacs file:

  (keyboard-translate ?\C-h ?\C-?)

To turn DEL into C-h, you would add this line to your .emacs file:

  (keyboard-translate ?\C-? ?\C-h)

The first key sequence of the pair after the function identifies what is produced by the keyboard; the second, what is matched for in the keymaps.

Keyboard translations are not the same as key bindings in keymaps. Emacs contains numerous keymaps that apply in different situations, but there is only one set of keyboard translations, and it applies to every character that Emacs reads from the terminal. Keyboard translations take place at the lowest level of input processing; the keys that are looked up in keymaps contain the characters that result from keyboard translation.

Also see "Keyboard Translations" in the online manual.

This information comes from the Emacs FAQ.

At Indiana University, for personal or departmental Linux or Unix systems support, see Get help for Linux or Unix at IU.

This is document ajzv in the Knowledge Base.
Last modified on 2018-01-18 13:17:40.