124

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 ?

Sake
  • 4,033
  • 6
  • 33
  • 37

5 Answers5

183

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 ?
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 51
    You can also do `C-h w ` to know just where the key is, not the full documentation. Conversely, you can also do `C-h c ` to just know what function is bound to a key sequence. – ShreevatsaR Jun 08 '09 at 15:07
  • Well this is an even better answer. Thanks ! – Sake Jun 08 '09 at 15:11
  • 8
    also: C-h w is typically bound to where-is. – Cheeso Jun 08 '09 at 16:54
  • 2
    So, I can where-is where-is when I forget C-h w. That's great ! ;) – Sake Jun 09 '09 at 00:48
  • It would be good if one could generate a list of all current key bindings...must be possible. – ftravers Apr 04 '11 at 04:53
  • @ftravers - the `which-key` package on MELPA (also part of the Spacemacs config.) does a fine job of this. It shows the available key bindings after an initial prefix is entered. (e.g. `C-x`) a list of top level bindings can be shown using `M-x which-key-show-top-level` – ocodo Sep 02 '16 at 00:21
  • minor typo correction: s/`C-h f variable-name`/`C-h v variable-name` – conner May 16 '18 at 21:20
  • 2
    For those who have bound `C-h` to something else in their window managers: `C-h w` is equivalent to `M-x where-is`. `C-h k` is equivalent to `M-x describe-key`. `C-h f` is equivalent to `M-x describe-function`. `C-h v` is equivalent to `M-x describe-variable`. `C-h ?` is equivalent to `M-x help-for-help`. Also note that `f1` can be used instead of `C-h`. – Ricardo Dec 24 '19 at 04:21
17

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.

Tobias
  • 5,038
  • 1
  • 18
  • 39
Jisang Yoo
  • 3,670
  • 20
  • 31
  • Looking at the documentation of `where-is-internal` suggests it’s OK to use it. Also, conventionally we use a format like `where-is--internal` (note the double dash) for “private” functions. – GergelyPolonkai Nov 02 '16 at 11:50
  • 2
    Usage of suffix `-internal` is described there: https://www.gnu.org/software/emacs/manual/html_node/elisp/Function-Names.html#Function-Names. It should be used for internal C functions. – Tobias May 13 '19 at 10:43
13

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

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

Glen Best
  • 22,769
  • 3
  • 58
  • 74
6

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

ocodo
  • 29,401
  • 18
  • 105
  • 117
0

The lookup-key function works well, but there's no function for finding the key binding programmatically from a command AFAIK. The following does that and is based on the slime-cheat-sheet command.

(defun lookup-function (keymap func)
  (let ((all-bindings (where-is-internal (if (symbolp func)
                                             func
                                           (cl-first func))
                                         keymap))
        keys key-bindings)
    (dolist (binding all-bindings)
      (when (and (vectorp binding)
                 (integerp (aref binding 0)))
        (push binding key-bindings)))
    (push (mapconcat #'key-description key-bindings " or ") keys)
    (car keys)))

(lookup-key (current-global-map) (kbd "C-x C-b"))    ; => list-buffers
(lookup-function (current-global-map) 'list-buffers) ; => "C-x C-b"

Interactively, as @ShreevatsaR suggested, you can use C-h w <function name> (where-is) and C-h c <key sequence> (describe-key-briefly). See here for more information.

jagrg
  • 181
  • 5