ChatGPT解决这个技术问题 Extra ChatGPT

Given an emacs command name, how would you find key-bindings ? (and vice versa)

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 ?


A
Adam Rosenfield

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 ?

You can also do C-h w <function name> to know just where the key is, not the full documentation. Conversely, you can also do C-h c <key sequence> to just know what function is bound to a key sequence.
Well this is an even better answer. Thanks !
also: C-h w is typically bound to where-is.
So, I can where-is where-is when I forget C-h w. That's great ! ;)
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.
T
Tobias

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.


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.
Usage of suffix -internal is described there: gnu.org/software/emacs/manual/html_node/elisp/…. It should be used for internal C functions.
G
Glen Best

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

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


o
ocodo

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


j
jagrg

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.