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?

Drew
  • 75,699
  • 9
  • 109
  • 225
Florian
  • 241
  • 1
  • 11

2 Answers2

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)))
Stefanus
  • 161
  • 1
  • 5
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))
John Kitchin
  • 11,555
  • 1
  • 19
  • 41