Next: , Previous: , Up: Rebinding   [Contents][Index]


27.4.2.1 Changing Key Bindings Interactively

M-x global-set-key RET key cmd RET

Defines key globally to run cmd.

M-x local-set-key RET keys cmd RET

Defines key locally (in the major mode now in effect) to run cmd.

M-x local-unset-key RET keys RET

Removes the local binding of key.

cmd is a symbol naming an interactively-callable function.

When called interactively, key is the next complete key sequence that you type. When called as a function, key is a string, a vector of events, or a vector of key-description lists as described in the define-key function description. The binding goes in the current buffer’s local map, which is shared with other buffers in the same major mode.

The following example:

M-x global-set-key RET C-f next-line RET

redefines C-f to move down a line. The fact that cmd is read second makes it serve as a kind of confirmation for key.

These functions offer no way to specify a particular prefix keymap as the one to redefine in, but that is not necessary, as you can include prefixes in key. key is read by reading characters one by one until they amount to a complete key (that is, not a prefix key). Thus, if you type C-f for key, Emacs enters the minibuffer immediately to read cmd. But if you type C-x, another character is read; if that character is 4, another character is read, and so on. For example,

M-x global-set-key RET C-x 4 $ spell-other-window RET

redefines C-x 4 $ to run the (fictitious) command spell-other-window.

The most general way to modify a keymap is the function define-key, used in Lisp code (such as your init file). define-key takes three arguments: the keymap, the key to modify in it, and the new definition. See Init File, for an example. substitute-key-definition is used similarly; it takes three arguments, an old definition, a new definition, and a keymap, and redefines in that keymap all keys that were previously defined with the old definition to have the new definition instead.


Next: , Previous: , Up: Rebinding   [Contents][Index]