57.4.7 Rebinding Function Keys
Key sequences can contain function keys as well as ordinary
characters. Just as Lisp characters (actually integers) represent
keyboard characters, Lisp symbols represent function keys. If the
function key has a word as its label, then that word is also the name of
the corresponding Lisp symbol. Here are the conventional Lisp names for
common function keys:
- Cursor arrow keys.
- Other cursor repositioning keys.
- Miscellaneous function keys.
- Numbered function keys (across the top of the keyboard).
- Keypad keys (to the right of the regular keyboard), with names or punctuation.
- Keypad keys with digits.
- Keypad PF keys.
These names are conventional, but some systems (especially when using
X) may use different names. To make certain what symbol is used for a
given function key on your terminal, type C-h c followed by that
A key sequence which contains function key symbols (or anything but
ASCII characters) must be a vector rather than a string.
Thus, to bind function key ‘f1’ to the command
write the following:
(global-set-key [f1] 'rmail)
To bind the right-arrow key to the command
forward-char, you can
use this expression:
(global-set-key [right] 'forward-char)
This uses the Lisp syntax for a vector containing the symbol
right. (This binding is present in Emacs by default.)
See Init Rebinding, for more information about using vectors for
You can mix function keys and characters in a key sequence. This
example binds C-x <NEXT> to the command
(global-set-key [?\C-x next] 'forward-page)
?\C-x is the Lisp character constant for the character
C-x. The vector element
next is a symbol and therefore
does not take a question mark.
You can use the modifier keys <CTRL>, <META>, <HYPER>,
<SUPER>, <ALT> and <SHIFT> with function keys. To represent
these modifiers, add the strings ‘C-’, ‘M-’, ‘H-’,
‘s-’, ‘A-’ and ‘S-’ at the front of the symbol name.
Thus, here is how to make Hyper-Meta-<RIGHT> move forward a
(global-set-key [H-M-right] 'forward-word)
Many keyboards have a “numeric keypad” on the right hand side.
The numeric keys in the keypad double up as cursor motion keys,
toggled by a key labeled ‘Num Lock’. By default, Emacs
translates these keys to the corresponding keys in the main keyboard.
For example, when ‘Num Lock’ is on, the key labeled ‘8’ on
the numeric keypad produces
kp-8, which is translated to
8; when ‘Num Lock’ is off, the same key produces
kp-up, which is translated to <UP>. If you rebind a key
such as 8 or <UP>, it affects the equivalent keypad key too.
However, if you rebind a ‘kp-’ key directly, that won't affect
its non-keypad equivalent.
Emacs provides a convenient method for binding the numeric keypad
keys, using the variables
keypad-numlock-shifted-setup. These can be found in the
‘keyboard’ customization group (see Easy Customization). You
can rebind the keys to perform other tasks, such as issuing numeric