Next: Key Bindings Using Strings, Previous: Interactive Rebinding, Up: Rebinding [Contents][Index]
You can use the functions global-set-key
and define-key
to rebind keys under program control.
(global-set-key keys cmd)
Defines keys globally to run cmd.
(define-key keymap keys def)
Defines keys to run def in the keymap keymap.
keymap is a keymap object.
keys is the sequence of keystrokes to bind.
def is anything that can be a key’s definition:
nil
, meaning key is undefined in this keymap
(string . defn)
, meaning that defn is the definition
(defn should be a valid definition in its own right)
(keymap . char)
, meaning use the definition of
char in map keymap
For backward compatibility, SXEmacs allows you to specify key sequences as strings. However, the preferred method is to use the representations of key sequences as vectors of keystrokes. See Keystrokes, for more information about the rules for constructing key sequences.
Emacs allows you to abbreviate representations for key sequences in most places where there is no ambiguity. Here are some rules for abbreviation:
f1
is equivalent to (f1)
.
(control a)
is equivalent to [(control a)]
.
65
is equivalent to A
. (This is not so much an
abbreviation as an alternate representation.)
Here are some examples of programmatically binding keys:
;;; Bindmy-command
to f1 (global-set-key 'f1 'my-command) ;;; Bindmy-command
to Shift-f1 (global-set-key '(shift f1) 'my-command) ;;; Bindmy-command
to C-c Shift-f1 (global-set-key '[(control c) (shift f1)] 'my-command) ;;; Bindmy-command
to the middle mouse button. (global-set-key 'button2 'my-command) ;;; Bindmy-command
to META CTL Right Mouse Button ;;; in the keymap that is in force when you are runningdired
. (define-key dired-mode-map '(meta control button3) 'my-command)
Next: Key Bindings Using Strings, Previous: Interactive Rebinding, Up: Rebinding [Contents][Index]