Function: keymap-global-set

keymap-global-set is an interactive and byte-compiled function defined in keymap.el.gz.

Signature

(keymap-global-set KEY COMMAND)

Documentation

Give KEY a global binding as COMMAND.

When called interactively, KEY is a key sequence. When called from Lisp, KEY is a string that must satisfy key-valid-p.

COMMAND is the command definition to use. When called interactively, this function prompts for COMMAND and accepts only names of known commands, i.e., symbols that satisfy the commandp predicate. When called from Lisp, COMMAND can be anything that keymap-set accepts as its DEFINITION argument.

If COMMAND is a string (which can only happen when this function is called from Lisp), it must satisfy key-valid-p.

Note that if KEY has a local binding in the current buffer, that local binding will continue to shadow any global binding that you make with this function.

Other relevant functions are documented in the keymaps group.

View in manual

Probably introduced at or before Emacs version 29.1.

Key Bindings

Shortdoc

;; keymaps
(keymap-global-set "C-c C-c" #'quit-buffer)

Source Code

;; Defined in /usr/src/emacs/lisp/keymap.el.gz
(defun keymap-global-set (key command &optional interactive)
  "Give KEY a global binding as COMMAND.
When called interactively, KEY is a key sequence.  When called from
Lisp, KEY is a string that must satisfy `key-valid-p'.

COMMAND is the command definition to use.  When called interactively,
this function prompts for COMMAND and accepts only names of known
commands, i.e., symbols that satisfy the `commandp' predicate.  When
called from Lisp, COMMAND can be anything that `keymap-set' accepts
as its DEFINITION argument.

If COMMAND is a string (which can only happen when this function is
called from Lisp), it must satisfy `key-valid-p'.

Note that if KEY has a local binding in the current buffer,
that local binding will continue to shadow any global binding
that you make with this function."
  (declare (compiler-macro (lambda (form) (keymap--compile-check key) form))
           (advertised-calling-convention (key command) "29.1"))
  (interactive "KSet key globally: \nCSet key %s globally to command: \np")
  (when interactive
    (setq key (key-description key)))
  (keymap-set (current-global-map) key command))