23.16 Commands for Binding Keys

This section describes some convenient interactive interfaces for changing key bindings. They work by calling keymap-set (see Changing Key Bindings). In interactive use, these commands prompt for the argument key and expect the user to type a valid key sequence; they also prompt for the binding of the key sequence, and expect the name of a command (i.e., a symbol that satisfies commandp, see Interactive Call). When called from Lisp, these commands expect key to be a string that satisfies key-valid-p (see Key Sequences), and binding to be any Lisp object that is meaningful in a keymap (see Key Lookup).

People often use keymap-global-set in their init files (see The Init File) for simple customization. For example,

(keymap-global-set "C-x C-\\" 'next-line)

redefines C-x C-\ to move down a line.

(keymap-global-set "M-<mouse-1>" 'mouse-set-point)

redefines the first (leftmost) mouse button, entered with the Meta key, to set point where you click.

Be careful when using non-ASCII text characters in Lisp specifications of keys to bind. If these are read as multibyte text, as they usually will be in a Lisp file (see Loading Non-ASCII Characters), you must type the keys as multibyte too. For instance, if you use this:

(keymap-global-set "รถ" 'my-function) ; bind o-umlaut

and your language environment is multibyte Latin-1, these commands actually bind the multibyte character with code 246, not the byte code 246 (M-v) sent by a Latin-1 terminal. In order to use this binding, you need to teach Emacs how to decode the keyboard by using an appropriate input method (see Input Methods in The GNU Emacs Manual).

Command: keymap-global-set key binding

This function sets the binding of key in the current global map to binding.

(keymap-global-set key binding)
≡
(keymap-set (current-global-map) key binding)
Command: keymap-global-unset key

This function removes the binding of key from the current global map.

One use of this function is in preparation for defining a longer key that uses key as a prefix—which would not be allowed if key has a non-prefix binding. For example:

(keymap-global-unset "C-l")
    ⇒ nil
(keymap-global-set "C-l C-l" 'redraw-display)
    ⇒ nil
Command: keymap-local-set key binding

This function sets the binding of key in the current local keymap to binding.

(keymap-local-set key binding)
≡
(keymap-set (current-local-map) key binding)
Command: keymap-local-unset key

This function removes the binding of key from the current local map.