Substitute Command Keys

Marcin Borkowski (mbork) has a nice post on the Emacs substitute-command-keys command. The TL;DR is that it’s a way of mapping a command name to its keybinding. I’ve long been familiar with it but only in the context of Emacs Doc strings. For example, here’s the Doc string from one of my custom macros:

  "Convenience macro to generate a key sequence map entry
for \\[prettify-key-sequence]."

the \\[prettify-key-sequence] is mapped to whatever the current binding for prettify-key-sequence is. It’s nice because I can change the binding without having to worry about updating the documentation.

Mbork’s post tells us that my use above is really just a special case of a more general facility: substitute-command-keys. You can programmatically convert a function name to its binding by calling substitute-command-keys.

I’m not sure where you’d want to use this other than in a Doc string (or maybe an Org doc) but I’m ready to be educated on the matter. I can’t remember where I learned about using \\[...] in the Doc string. It was probably from seeing it in some function in Emacs core. One thing for sure, it’s not easy to discover without looking for it explicitly or stumbling across it like I probably did.

In any event, this is a useful thing to know. It’s a great example of how Emacs is self documenting.

This entry was posted in General and tagged . Bookmark the permalink.