elisp: Key Binding Commands
21.15 Commands for Binding Keys
===============================
This section describes some convenient interactive interfaces for
changing key bindings. They work by calling ‘define-key’.
People often use ‘global-set-key’ in their init files (Init
File) for simple customization. For example,
(global-set-key (kbd "C-x C-\\") 'next-line)
or
(global-set-key [?\C-x ?\C-\\] 'next-line)
or
(global-set-key [(control ?x) (control ?\\)] 'next-line)
redefines ‘C-x C-\’ to move down a line.
(global-set-key [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 (Loading Non-ASCII), you
must type the keys as multibyte too. For instance, if you use this:
(global-set-key "ö" 'my-function) ; bind o-umlaut
or
(global-set-key ?ö '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 (Input Methods (emacs)Input Methods.).
-- Command: global-set-key key binding
This function sets the binding of KEY in the current global map to
BINDING.
(global-set-key KEY BINDING)
≡
(define-key (current-global-map) KEY BINDING)
-- Command: global-unset-key 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:
(global-unset-key "\C-l")
⇒ nil
(global-set-key "\C-l\C-l" 'redraw-display)
⇒ nil
This function is equivalent to using ‘define-key’ as follows:
(global-unset-key KEY)
≡
(define-key (current-global-map) KEY nil)
-- Command: local-set-key key binding
This function sets the binding of KEY in the current local keymap
to BINDING.
(local-set-key KEY BINDING)
≡
(define-key (current-local-map) KEY BINDING)
-- Command: local-unset-key key
This function removes the binding of KEY from the current local
map.
(local-unset-key KEY)
≡
(define-key (current-local-map) KEY nil)