Is there a command ie: get-bounded-string that is available that performs this?

(defun no-op () nil)

(define-key dired-mode-map (kbd  "C-c C-7") 'no-op)


(get-bound-string dired-mode-map 'no-op)
=> "C-c C-7"
  • Since this is meant to invoke no-op as a command, its definition should contain the (interactive) form. – Wojciech Gac Dec 18 '20 at 9:13

Emacs has a substitute-command-keys function, which takes a format string and substitutes occurrences of specific escape substrings according to their type. Specifically, it replaces \\[<command>] with the keybinding for <command> in the context of the current keymap.

What you can do is run the function in the context of a temporary buffer with desired keymap set. It might look something like this:

  (use-local-map dired-mode-map)
  (substitute-command-keys "\\[no-op]"))

You could, of course, wrap the above in a function definition.

  • oh wow. that's super impressive... though it's quite a round about way of doing it. What I'm looking to do is to be able to generate a hydra from a mode's keybindings. It's definitely a start but is is there something more efficient? – zcaudate Dec 18 '20 at 12:23
  • As a matter of fact there is. I just found out about it myself. You can do (substitute-command-keys "\\<dired-mode-map>\\[no-op]"). – Wojciech Gac Dec 18 '20 at 12:47
  • oh this is awesome. thanks heaps. – zcaudate Dec 18 '20 at 13:01
  • You're very welcome. – Wojciech Gac Dec 18 '20 at 13:03
  • this looks something very similar to what I had in mind: gitlab.com/jjzmajic/hercules.el – zcaudate Dec 18 '20 at 15:01

You already accepted an answer as correct, but I interpreted your question differently, as wanting Elisp code that tells you what key(s) a given command is bound to in a given mode.

If you're interested in that, where-is-internal will do it for you.

For example, to find out what keys command dired-mark is bound to in Dired mode, use this:

(where-is-internal 'dired-mark dired-mode-map)

See the Elisp manual, node Scanning Keymaps.

Interactively, you have command where-is, bound to C-h w. That tells you which keys a command is bound to in the current buffer (mode).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.