114
votes

If I know an emacs command name, says, "goto-line"; what if I want to query whether if there are any key-sequences bound to this command ?

And vice versa, given a key sequence, how can I find its command name ?

4

4 Answers

172
votes

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 ?
16
votes

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.

13
votes

C-h w (or F1-w): where-is RET somecommandname RET

Does just what you're asking - lists bound keys with no additional information. :)

5
votes

An old question, but for the benefit of new readers, there are some other nice ways to see key bindings

M-x describe-bindings

Lists all the bindings currently available, use isearch, occur, etc. to make good use of this list.

M-x describe-prefix-map

This shows all the bindings available from the current mode, you can use the display buffer as you would any other readonly Emacs buffer, for example you can search freely for strings etc.

M-x describe-mode

As well as giving you general info about the current mode, it will also list all the key bindings available.

M-x describe-minor-mode

You will be prompted to enter the name of a minor mode, and then be shown info and key bindings for that minor mode.

NOTE : The examples below use additional packages (available from MELPA)

Which key

This shows you bindings just before you need them. Enter a prefix, for example C-x or C-c and a list of the bindings available in that prefix will be shown.

You can also view a list of key bindings available from the current mode by using:

M-x which-key-show-top-level

It is useful to bind which-key-show-top-level to a key chord of your choice, so you can view the keys available from anywhere.

For example, C-s (isearch-forward) has an extensive key map which often unknown. e.g. M-s o starts occur using the current search string, I didn't know about this for many many years of using Emacs. Having which-key around has helped me discover many rare gems in Emacs.

https://github.com/justbur/emacs-which-key

Guide key

Guide key works in much the same way as which-key I'd recommend taking a look at it to compare features.

https://github.com/kai2nenobu/guide-key