2

For example, I use undo-tree and have the following in my config file:

(use-package undo-tree
  :ensure t
  :diminish (undo-tree-mode . "")
  :config
  (global-undo-tree-mode 1))

Now I want to use the hydra taken from the hydra wiki:

(defhydra hydra-undo-tree (:hint nil)
"
  _p_: undo  _n_: redo _s_: save _l_: load   "
  ("p"   undo-tree-undo)
  ("n"   undo-tree-redo)
  ("s"   undo-tree-save-history)
  ("l"   undo-tree-load-history)
  ("u"   undo-tree-visualize "visualize" :color blue)
  ("q"   nil "quit" :color blue))

(global-set-key (kbd "C-x u") 'hydra-undo-tree/undo-tree-undo)`

What would be the proper way to define the hydra inside use-package and set the keybinding using :bind?

2 Answers 2

6

The package use-package-hydra, which is also available on MELPA, should solve this problem. I combined your code blocks into a single (use-package) expression (see keywords :after, :bind and :hydra). Note the missing defhydra.

(use-package undo-tree
  :ensure t
  :diminish (undo-tree-mode . "")
  :after hydra
  :bind ("C-x u" . hydra-undo-tree/undo-tree-undo)
  :config
  (global-undo-tree-mode 1)
  :hydra (hydra-undo-tree (:hint nil)
  "
  _p_: undo  _n_: redo _s_: save _l_: load   "
  ("p"   undo-tree-undo)
  ("n"   undo-tree-redo)
  ("s"   undo-tree-save-history)
  ("l"   undo-tree-load-history)
  ("u"   undo-tree-visualize "visualize" :color blue)
  ("q"   nil "quit" :color blue)))
2

This was way more subtle than I would have expected!

Some keys seem to be to set :demand t to make sure it gets loaded, and to set the key binding in the undo-tree-map which is where C-x u is defined. After that, this seems to work for me without any additional packages.

(use-package undo-tree
  :ensure t
  :demand t
  :diminish (undo-tree-mode . "")
  :bind (:map undo-tree-map ("C-x u" . hydra-undo-tree/body))
  :init (defhydra hydra-undo-tree (:hint nil)
      "
  _p_: undo  _n_: redo _s_: save _l_: load   "
      ("p"   undo-tree-undo)
      ("n"   undo-tree-redo)
      ("s"   undo-tree-save-history)
      ("l"   undo-tree-load-history)
      ("u"   undo-tree-visualize "visualize" :color blue)
      ("q"   nil "quit" :color blue))
  :config
  (global-undo-tree-mode))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.