1 [![Build Status](https://travis-ci.org/abo-abo/hydra.svg?branch=master)](https://travis-ci.org/abo-abo/hydra)
3 This is a package for GNU Emacs that can be used to tie related
4 commands into a family of short bindings with a common prefix - a
7 ![hydra](http://oremacs.com/download/Hydra.png)
9 Once you summon the Hydra through the prefixed binding (the body + any
10 one head), all heads can be called in succession with only a short
13 The Hydra is vanquished once Hercules, any binding that isn't the
14 Hydra's head, arrives. Note that Hercules, besides vanquishing the
15 Hydra, will still serve his orignal purpose, calling his proper
16 command. This makes the Hydra very seamless, it's like a minor mode
17 that disables itself auto-magically.
19 ## Sample global Hydras
23 (defhydra hydra-zoom (global-map "<f2>")
25 ("g" text-scale-increase "in")
26 ("l" text-scale-decrease "out"))
32 (defhydra hydra-error (global-map "M-g")
34 ("h" first-error "first")
35 ("j" next-error "next")
36 ("k" previous-error "prev")
37 ("v" recenter-top-bottom "recenter")
44 (require 'hydra-examples)
45 (defhydra hydra-splitter (global-map "C-M-s")
47 ("h" hydra-move-splitter-left)
48 ("j" hydra-move-splitter-down)
49 ("k" hydra-move-splitter-up)
50 ("l" hydra-move-splitter-right))
53 ## Using the functions generated by `defhydra`
55 With the example above, you can e.g.:
58 (key-chord-define-global "tt" 'hydra-zoom/body)
61 In fact, since `defhydra` returns the body symbol, you can even write
65 (key-chord-define-global
67 (defhydra hydra-zoom (global-map "<f2>")
69 ("g" text-scale-increase "in")
70 ("l" text-scale-decrease "out")))
73 If you like key chords so much that you don't want to touch the global
74 map at all, you can e.g.:
77 (key-chord-define-global
79 (defhydra hydra-error ()
81 ("h" first-error "first")
82 ("j" next-error "next")
83 ("k" previous-error "prev")))
86 You can also substitute `global-map` with any other keymap, like
87 `c++-mode-map` or `yas-minor-mode-map`.
89 See the [introductory blog post](http://oremacs.com/2015/01/20/introducing-hydra/) for more information.
91 ## Using Hydra for major-mode or minor-mode bindings
96 (defhydra lispy-vi (lispy-mode-map "C-z")
104 ## Can Hydras can be helpful?
109 (setq hydra-is-helpful t)
112 This is the default setting. In this case, you'll get a hint in the
113 echo area consisting of current Hydra's base comment and heads. You
114 can even add comments to the heads like this:
117 (defhydra hydra-zoom (global-map "<f2>")
119 ("g" text-scale-increase "in")
120 ("l" text-scale-decrease "out"))
123 With this, you'll see `zoom: [g]: in, [l]: out.` in your echo area,
124 once the zoom Hydra becomes active.
128 Since version `0.5.0`, Hydra's heads all have a color associated with them:
130 - *red* (default) means the calling this head will not vanquish the Hydra
131 - *blue* means that the Hydra will be vanquished after calling this head
133 In all the older examples, all heads are red by default. You can specify blue heads like this:
140 ("a" abbrev-mode "abbrev" :color blue)
141 ("d" toggle-debug-on-error "debug" :color blue)
142 ("f" auto-fill-mode "fill" :color blue)
143 ("t" toggle-truncate-lines "truncate" :color blue)
144 ("w" whitespace-mode "whitespace" :color blue)
148 Or, since the heads can inherit the color from the body, the following is equivalent:
153 (defhydra toggle (:color blue)
155 ("a" abbrev-mode "abbrev")
156 ("d" toggle-debug-on-error "debug")
157 ("f" auto-fill-mode "fill")
158 ("t" toggle-truncate-lines "truncate")
159 ("w" whitespace-mode "whitespace")
163 The above Hydra is very similar to this code:
166 (global-set-key (kbd "C-c C-v t") 'toggle-truncate-lines)
167 (global-set-key (kbd "C-c C-v f") 'auto-fill-mode)
168 (global-set-key (kbd "C-c C-v a") 'abbrev-mode)
171 However, there are two important differences:
173 - you get a hint like this right after <kbd>C-c C-v</kbd>:
175 toggle: [t]: truncate, [f]: fill, [a]: abbrev, [q]: cancel.
177 - you can cancel <kbd>C-c C-v</kbd> with a command while executing that command, instead of e.g.
178 getting an error `C-c C-v C-n is undefined` for <kbd>C-c C-v C-n</kbd>.
180 ## Hydras and numeric arguments
182 Since version `0.6.0`, for any Hydra:
184 - `digit-argment` can be called with <kbd>0</kbd>-<kbd>9</kbd>.
185 - `negative-argument` can be called with <kbd>-</kbd>
186 - `universal-argument` can be called with <kbd>C-u</kbd>
188 ## Hydras can have `:pre` and `:post` statements
190 Since version `0.7.0`, you can specify code that will be called before each head, and
191 after the body. For example:
198 (set-cursor-color "#40e0d0")
201 (set-cursor-color "#ffffff")
203 "Thank you, come again.")))
212 ## New Hydra color: amaranth
214 Since version `0.8.0`, a new color - amaranth, in addition to the previous red and blue, is
215 available for the Hydra body.
217 According to [Wikipedia](http://en.wikipedia.org/wiki/Amaranth):
219 > The word amaranth comes from the Greek word amaranton, meaning "unwilting" (from the
220 > verb marainesthai, meaning "wilt"). The word was applied to amaranth because it did not
221 > soon fade and so symbolized immortality.
223 Hydras with amaranth body are impossible to quit with any binding *except* a blue head.
224 A check for at least one blue head exists in `defhydra`, so that you don't get stuck by accident.
226 Here's an example of an amaranth Hydra:
233 (set-cursor-color "#40e0d0")
235 (set-cursor-color "#ffffff")
245 The only way to exit it, is to press <kbd>q</kbd>. No other methods will work. You can
246 use an amaranth Hydra instead of a red one, if for you the cost of being able to exit only
247 though certain bindings is less than the cost of accidentally exiting a red Hydra by
248 pressing the wrong prefix.
250 Note that it does not make sense to define a single amaranth head, so this color can only
251 be assigned to the body. An amaranth body will always have some amaranth heads and some
252 blue heads (otherwise, it's impossible to exit), no reds.
254 ## Generate simple lambdas in-place:
256 Since version `0.9.0` it's possible to pass a single sexp instead of a function name or a lambda
257 to a head. This sexp will be wrapped in an interactive lambda. Here's an example:
260 (defhydra hydra-launcher (:color blue)
263 ("r" (browse-url "http://www.reddit.com/r/emacs/") "reddit")
264 ("w" (browse-url "http://www.emacswiki.org/") "emacswiki")
267 (global-set-key (kbd "C-c r") 'hydra-launcher/body)