I'm using the which key plugin in my emacs setup and I notice that when I set a key binding in a keymap with a lambda expression, like this

(define-key my-yank-keymap "l"
  (lambda ()
    "Yank whole line"
      (set-mark-command nil)
      (move-end-of-line nil)
      (kill-ring-save 'doesnt-matter-because 'of-next-argument t))))

(global-set-key (kbd "C-c w") my-yank-keymap)

Activating my-yank-keymap with C-c w displays my keybinding for the l key, but the label for it is simply lambda. Can which-key be configured to use the DOCSTRING of the function/lambda when there is no function name to display?

  • You can use which-key-add-key-based-replacement to change the description associated with a key combo (see the docs) – VanLaser Sep 4 '16 at 11:35
  • Since I don't have many lambda-based key bindings, this is an option, but I'd prefer to not to have to do this for every keybinding, but instead automate/configure this. Something like here, but where you can give a function/lambda in the cdr, instead of a simple string. It looks like I have to just define a "proper" function with defun to get this working. Thanks for the help though. – flooose Sep 4 '16 at 12:02
  • You're welcome. You can probably also use 'general.el', it has support for which-key description when mapping, and can be used without 'evil': github.com/noctuid/… (not sure if it's your use case, though) – VanLaser Sep 4 '16 at 12:12
  • Somewhat related: For an anonymous command, Icicles key completion, which is similar to which-key behavior, shows the key, together with the lambda-form itself (which includes its doc string, if any). E.g. C-o = (lambda nil "Doc string." (interactive) (foobar 1)) – Drew Sep 4 '16 at 15:48

Your Answer

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

Browse other questions tagged or ask your own question.