Function: evil-define-key*

evil-define-key* is a byte-compiled function defined in evil-core.el.

Signature

(evil-define-key* STATE KEYMAP KEY DEF &rest BINDINGS)

Documentation

Create a STATE binding from KEY to DEF for KEYMAP.

STATE is one of normal, insert, visual, replace, operator, motion, emacs, or a list of one or more of these. Omitting a state by using nil corresponds to a standard Emacs binding using define-key The remaining arguments are like those of define-key. For example:

    (evil-define-key* 'normal foo-map "a" 'bar)

This creates a binding from "a" to bar in Normal state, which is active whenever foo-map is active. Using nil for the state, the following are equivalent:

    (evil-define-key* nil foo-map "a" 'bar)

    (define-key foo-map "a" 'bar)

 It is possible to specify multiple states and/or bindings at
 once:

    (evil-define-key* '(normal visual) foo-map
      "a" 'bar
      "b" 'foo)

KEYMAP may also be a quoted symbol. If the symbol is global, the global evil keymap corresponding to the state(s) is used, meaning the following are equivalent:

    (evil-define-key* 'normal 'global "a" 'bar)

    (evil-global-set-key 'normal "a" 'bar)

The symbol local may also be used, which corresponds to using evil-local-set-key.

The use is nearly identical to evil-define-key with the exception that this is a function and not a macro (and so will not be expanded when compiled which can have unintended consequences). evil-define-key* also does not defer any bindings like evil-define-key does using evil-with-delay. This allows errors in the bindings to be caught immediately, and makes its behavior more predictable.

Source Code

;; Defined in ~/.emacs.d/elpa/evil-20251108.138/evil-core.el
(defun evil-define-key* (state keymap key def &rest bindings)
  "Create a STATE binding from KEY to DEF for KEYMAP.
STATE is one of normal, insert, visual, replace, operator,
motion, emacs, or a list of one or more of these. Omitting a
state by using nil corresponds to a standard Emacs binding using
`define-key' The remaining arguments are like those of
`define-key'. For example:

    (evil-define-key* \\='normal foo-map \"a\" \\='bar)

This creates a binding from \"a\" to bar in Normal state, which
is active whenever foo-map is active. Using nil for the state,
the following are equivalent:

    (evil-define-key* nil foo-map \"a\" \\='bar)

    (define-key foo-map \"a\" \\='bar)

 It is possible to specify multiple states and/or bindings at
 once:

    (evil-define-key* \\='(normal visual) foo-map
      \"a\" \\='bar
      \"b\" \\='foo)

KEYMAP may also be a quoted symbol. If the symbol is global, the
global evil keymap corresponding to the state(s) is used, meaning
the following are equivalent:

    (evil-define-key* \\='normal \\='global \"a\" \\='bar)

    (evil-global-set-key \\='normal \"a\" \\='bar)

The symbol local may also be used, which corresponds to using
`evil-local-set-key'.

The use is nearly identical to `evil-define-key' with the
exception that this is a function and not a macro (and so will
not be expanded when compiled which can have unintended
consequences). `evil-define-key*' also does not defer any
bindings like `evil-define-key' does using `evil-with-delay'.  This
allows errors in the bindings to be caught immediately, and makes
its behavior more predictable."
  (declare (indent defun))
  (let ((maps
         (if state
             (mapcar
              (lambda (st)
                (cond ((eq keymap 'global)
                       (evil-state-property st :keymap t))
                      ((eq keymap 'local)
                       (evil-state-property st :local-keymap t))
                      (t
                       (evil-get-auxiliary-keymap keymap st t t))))
              (if (listp state) state (list state)))
           (list
            (cond ((eq keymap 'global)
                   global-map)
                  ((eq keymap 'local)
                   ;; see `local-set-key'
                   (or (current-local-map)
                       (let ((map (make-sparse-keymap)))
                         (use-local-map map)
                         map)))
                  (t
                   keymap))))))
    (while key
      (dolist (map maps)
        (define-key map key def))
      (setq key (pop bindings)
            def (pop bindings)))
    ;; ensure the prompt string comes first
    (dolist (map maps)
      (evil-set-keymap-prompt map (keymap-prompt map)))))