Next: , Previous: Backspace invokes help, Up: Key bindings

9.7 How do I swap two keys?

You can swap two keys (or key sequences) by using the keyboard-translate function. For example, to turn C-h into <DEL> and <DEL> to C-h, use

     (keyboard-translate ?\C-h ?\C-?)  ; translate `C-h' to DEL
     (keyboard-translate ?\C-? ?\C-h)  ; translate DEL to `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.

However, in the specific case of C-h and <DEL>, you should toggle normal-erase-is-backspace-mode instead of calling keyboard-translate. See DEL Does Not Delete.

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.