Nested Key Maps

Protesilaos Stavrou (Prot) has a great video on nesting key maps. A nested key map is just what it sounds like: a key map inside another key map. What that means practically is that one key shortcut will lead to another key map of additional choices.

In theory it works just as you think it should but there are some nuances and it’s nice to have the whole thing explained in one place. Prot’s video uses keymap-set and defvar-keymap, which assume you’re using Emacs 29 or later but even if you aren’t the process is the same except that you have to use the older commands such as define-key and make-sparse-keymap.

You can watch the video to see how to set up nested key-maps but, as I say, it works just like you think it would. One of the non obvious things that the video explains is how to deal with describing the key maps in which-key. To see why this matters, type Ctrl+x and wait until which-key pops up. You’ll see a lot of entries that say +prefix but there’s no indication of what the resulting key map does. Prot shows how to configure which-key to give the key map a meaningful name. That alone is worth watching the video for.

The video is 23 minutes, 17 seconds long so plan accordingly but you really should find some time to watch this video. If you hate watching videos, at least take a look at the sample code at the link. It’s a nice prĂ©cis of how to set up nested key maps and configure which-key.

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