Yay Emacs 8: which-key-replacement-alist

Posted: - Modified: | yay-emacs, emacs

[2025-01-07 Tue]: I like the way Karthik's blog posts have play-by-plays for the videos (ex: Fringe Matters: Finding the Right Difference), so I've added an experimental play by play at the bottom of this post.

I wish I could memorize all these keyboard shortcuts in Emacs, but I just don't use some commands often enough to build the muscle memory for them, especially since I have so many context-sensitive shortcuts thanks to Embark. That's why which-key is great. It used to be a separate package, but it's now part of Emacs 30. You just have to turn on which-key mode, and then, if you hesitate in the middle of a keyboard shortcut, Emacs will show you what you can do. It turns out that you can customize this menu,so here are some tweaks I'm trying. First, I want to change which-key-sort-order to the one that sorts by description. This groups similar functions together. Then I want to change the labels using which-key-replacement-alist. Let's try it without the dashes between words. Now let's turn on multiple replacements and make things even simpler. I can even use Unicode to make it easier to spot certain commands. And of course there's an Elisp way to customize all of this. I want to see if these little tweaks can help me use Emacs better. If you use which-key, maybe you'll also want to customize your menu. Let me know what you figure out!

You can watch this on YouTube, download the video, or download the audio.

Things I'm trying:

(with-eval-after-load 'which-key
  (setopt which-key-allow-multiple-replacements t
        which-key-sort-order 'which-key-description-order
        which-key-replacement-alist
        (seq-map
         (lambda (rep)
           `((nil . ,(elt rep 0))
             . (nil . ,(elt rep 1))))
         '(("my-subed-set-timestamp-to-mpv-position" "set to MPV")
           ("my-embark-org-copy-exported-url" "⭐🗐🔗 copy exported URL")
           ("my-subed-copy-timestamp-dwim" "copy")
           ("my-sketch-insert-file-as-link" "insert")
           ("my-geeqie-view" "geeqie")
           ("my-journal-edit" "edit")
           ("my-org-link-qr" "qr")
           ("my-image-open-in-" "")
           ("org-babel-" "ob-")
           ("next" "🠆")
           ("previous" "🠄")
           ("my-image-" "")
           ("my-embark-org-blog-" "")
           ("embark-collect" "⇶ collect")
           ("my-embark-org-" "")
           ("my-" "")
           ("embark-" "")
           ("embark-act-all" "all")
           ("embark-become" "become")
           ("embark-collect" "collect")
           ("-" " ")))))

To use which-key as part of Embark, see the Embark wiki's suggested configuration.

Play by play for the video:

  • I wish I could memorize all these keyboard shortcuts in Emacs, but I just don't use some commands often enough to build the muscle memory for them, especially since I have so many context-sensitive shortcuts thanks to Embark.
    • Showing the Embark context menu with C-. (embark-act) and then cycling through them with C-. (embark-cycle). In this case, I showed the Embark menus for a link, so it cycled through embark-url-map, embark-org-link-map, embark-identifier-map, and embark-expression-map. The active keymap and the value that will be passed as the first argument to functions is displayed in the echo area (ex: Act on identifier 'Embark').
  • That's why which-key is great. It used to be a separate package, but it's now part of Emacs 30. You just have to turn on which-key mode,
    • M-x customize-variable which-key-mode - toggle it on and save it.
  • and then, if you hesitate in the middle of a keyboard shortcut, Emacs will show you what you can do.
    • Here I started with C-c and then paused. Emacs displays the keyboard shortcuts that follow C-c.
  • It turns out that you can customize this menu,so here are some tweaks I'm trying. First, I want to change which-key-sort-order to the one that sorts by description.
    • M-x customize-variable which-key-sort-order, change it to which-key-description-order, and save.
  • This groups similar functions together.
    • The which-key screen now shows the embark- functions grouped together, the org- functions grouped together, etc.
  • Then I want to change the labels using which-key-replacement-alist. Let's try it without the dashes between words.
    • M-x customize-variable which-key-replacement-alist. I used INS to add a new entry. I clicked the first Value Menu to change it to nil, set the next value's regexp to "-", clicked the first Value Menu in the second cons cell to change it to nil, and set the next value's string to " ". The which-key menu now replaces most dashes with spaces, making things a little neater to look at.
  • Now let's turn on multiple replacements and make things even simpler.
    • M-x customize-variable which-key-allow-multiple-replacements, toggle it on, and save it.
    • I also added a bunch of other replacements, like removing the "embark " and "org " prefixes.
  • I can even use Unicode to make it easier to spot certain commands.
    • I added a replacement with some Unicode to make "⭐🗐🔗 copy exported URL" stand out in the menu.
  • And of course there's an Elisp way to customize all of this.
    • I showed the code snippet above.
  • I want to see if these little tweaks can help me use Emacs better. If you use which-key, maybe you'll also want to customize your menu. Let me know what you figure out!
    • We end with the screenshot of the which-key menu.
View org source for this post
You can comment with Disqus or you can e-mail me at sacha@sachachua.com.