I first learned about Emacs Hydra in early 2015, and I didn’t really understand why it would be useful, but it seemed neat, so I dug in. In short, Hydra provides a transient form of modal editing in which keys are rebound to various actions. It turns out this is a very cool abstraction, even if it can take a few moments to wrap your head around the utility. Examples are helpful.
Managing Window Configurations
I recently wanted to replace elscreen due to some minor incompatabilities with multi-term. I shopped around on MELPA for a replacement, and found winring, which was lightweight and fast, but lacked elscreen’s fast navigation between window configurations. Hydra is born for this.
Here is a very basic hydra that provides
(defhydra hydra-winring (:color blue :hint nil) " Winring _p_: Previous configuration _n_: Next configuration _k_: Kill current configuration _c_: Create new configuration " ("p" winring-prev-configuration :color red) ("n" winring-next-configuration :color red) ("k" winring-delete-configuration) ("c" winring-new-configuration) ("q" nil "cancel")) (global-set-key (kbd "C-z") 'hydra-winring/body)
This works particularly well with the
mplayer backend, since it
supports seeking within tracks, which also highlights the utility of
overriding the hydra color on a per-command basis.
(defhydra hydra-bongo (:color blue :hint nil) " Bongo control _l_: library _p_: pause/resume _s_: stop _b_: back 10s ^^_f_: fwd 10s _B_: back 60s ^^_F_: fwd " ("b" bongo-seek-backward-10 :color red) ("f" bongo-seek-forward-10 :color red) ("B" bongo-seek-backward-60 :color red) ("F" bongo-seek-forward-60 :color red) ("l" bongo) ("p" bongo-pause/resume) ("s" bongo-stop) ("q" nil "cancel")) (global-set-key (kbd "C-c b") 'hydra-bongo/body)
(defhydra hydra-spotify (:color blue :hint nil) " Spotify control _c_: current _p_: pause/resume _s_: stop _b_: back track ^^_f_: fwd track " ("p" spotify-playpause) ("c" spotify-current :color red) ("s" spotify-pause) ("f" spotify-next :color red) ("b" spotify-previous :color red) ("q" nil "cancel")) (global-set-key (kbd "C-c s") 'hydra-spotify/body)
Moving Between Windows
C-x o is the standard key sequence to move between windows, and
works well for two windows. But when you move to three or four, it’s
a bit cumbersome to type over and over.
(defhydra hydra-window (:color red :hint nil) "[o]: other window" ("o" other-window) ("q" nil "cancel")) (defun other-window-and-hydra () (interactive) (other-window 1) (hydra-window/body)) (global-set-key (kbd "C-x o") 'other-window-and-hydra)