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)
then
(get-bound-string dired-mode-map 'no-op)
=> "C-c C-7"
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:
(with-temp-buffer
(use-local-map dired-mode-map)
(substitute-command-keys "\\[no-op]"))
You could, of course, wrap the above in a function definition.
(substitute-command-keys "\\<dired-mode-map>\\[no-op]")
.
Commented
Dec 18, 2020 at 12:47
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).
no-op
as a command, its definition should contain the(interactive)
form.