50.3.6 Rebinding Keys in Your Init File

If you have a set of key bindings that you like to use all the time, you can specify them in your initialization file by writing Lisp code. See The Emacs Initialization File, for a description of the initialization file.

The recommended way to write a key binding using Lisp is to use either the keymap-global-set or the keymap-set functions. For example, here’s how to bind C-z to the shell command in the global keymap (see Interactive Subshell):

(keymap-global-set "C-z" 'shell)

The first argument to keymap-global-set describes the key sequence. It is a string made of a series of characters separated by spaces, with each character corresponding to a key. Keys with modifiers can be specified by prepending the modifier, such as ‘C-’ for Control, or ‘M-’ for Meta. Special keys, such as TAB and RET, can be specified within angle brackets as in TAB and RET.

The single-quote before the command name that is being bound to the key sequence, shell in the above example, marks it as a constant symbol rather than a variable. If you omit the quote, Emacs would try to evaluate shell as a variable. This will probably cause an error; it certainly isn’t what you want.

Here are some additional examples, including binding function keys and mouse events:

(keymap-global-set "C-c y" 'clipboard-yank)
(keymap-global-set "C-M-q" 'query-replace)
(keymap-global-set "<f5>" 'flyspell-mode)
(keymap-global-set "C-<f5>" 'display-line-numbers-mode)
(keymap-global-set "C-<right>" 'forward-sentence)
(keymap-global-set "<mouse-2>" 'mouse-save-then-kill)

Language and coding systems may cause problems with key bindings for non-ASCII characters. See Non-ASCII Characters in Init Files.

Alternatively, you can use the low level functions define-key and global-set-key. For example, to bind C-z to the shell command, as in the above example, using these low-level functions, use:

(global-set-key (kbd "C-z") 'shell)

There are various ways to specify the key sequence but the simplest is to use the function kbd as shown in the example above. kbd takes a single string argument that is a textual representation of a key sequence, and converts it into a form suitable for low-level functions such as global-set-key. For more details about binding keys using Lisp, see Keymaps in The Emacs Lisp Reference Manual.

As described in Local Keymaps, major modes and minor modes can define local keymaps. These keymaps are constructed when the mode is loaded for the first time in a session. The function keymap-set can be used to make changes in a specific keymap. To remove a key binding, use keymap-unset.

Since a mode’s keymaps are not constructed until it has been loaded, you must delay running code which modifies them, e.g., by putting it on a mode hook (see Hooks). For example, Texinfo mode runs the hook texinfo-mode-hook. Here’s how you can use the hook to add local bindings for C-c n and C-c p, and remove the one for C-c C-x x in Texinfo mode:

(add-hook 'texinfo-mode-hook
          (lambda ()
            (keymap-set texinfo-mode-map "C-c p"
            (keymap-set texinfo-mode-map "C-c n"
            (keymap-set texinfo-mode-map "C-c C-x x" nil)))