I configure numerous additional key bindings, and sometimes I unintentionally overwrite others. Is there a way to determine if a key binding is taken before binding a key, and if so throw an error?
1 Answer
See function lookup-key
. It will tell you whether a given key is defined in a given map, including in the global map, or even (with non-nil arg ACCEPT-DEFAULTS
) whether it has a fallback (default) binding in the current context.
For example, if you want to see whether C-M-kp-+
is defined in the current mode's map:
(lookup-key (current-local-map) (kbd "<C-M-S-kp-add>"))
And if you want to see whether it has a global binding:
(lookup-key (current-global-map) (kbd "<C-M-S-kp-add>"))
And if you want to see if has a default behavior in the context of the current mode, even though it is not specifically defined:
(lookup-key (current-local-map) (kbd "<C-M-S-kp-add>") t)
See also: Key Lookup.