Given an emacs command name, how would you find key-bindings ? (and vice versa)
To just find key bindings for a command, you can use emacs help's "where-is" feature
C-h w command-name
If multiple bindings are set for the command they will all be listed.
For the inverse, given a key sequence, you can type
C-h k key-sequence
To get the command that would run.
You can get detailed information about a command, also any non-interactive function defined, by typing
C-h f function-name
Which will give you detailed information about a function, including any key bindings for it, and
C-h v variable-name
will give you information about any (bound) variable. Key-maps are kept in variables, however the key codes are stored in a raw format. Try C-h v isearch-mode-map
for an example.
For more help on getting help, you can type
C-h ?
For interactively getting the command bound to a keyboard shortcut (or a key sequence in Emacs terms), see the selected answer.
For programmatically getting the command bound to a given key sequence, use the function key-binding
or lookup-key
that takes a key sequence and returns its bound command. The function key-binding
is what C-h k
uses.
(key-binding (kbd "C-h m"))
returns the command bound to C-h m
by searching in all current keymaps. The function lookup-key
searches in a single keymap:
(lookup-key (current-global-map) (kbd "TAB")) ; => indent-for-tab-command
(lookup-key org-mode-map (kbd "TAB")) ; => org-cycle
(lookup-key text-mode-map (kbd "TAB")) ; => nil
(lookup-key isearch-mode-map (kbd "TAB")) ; => isearch-printing-char
For programmatically getting all key sequences bound to a given command, where-is-internal
is probably the function to use. The name of the function ending with internal
seems to suggest that it's not for Emacs users to use in their init files but this function having a docstring seems to suggest otherwise. Anyone considering use of where-is-internal
should first check if remapping keys instead can achieve their goal.
An alternative for finding the keys that are bound to a specific command (e.g., forward-char
) is substitute-command-keys
(e.g., (substitute-command-keys "\\[forward-char]")
).
That is especially useful in larger texts.