]> code.delx.au - gnu-emacs-elpa/blob - packages/hydra/README.md
Merge commit '6f142e342a4228640cb50a45e224f932679355bb' from hydra
[gnu-emacs-elpa] / packages / hydra / README.md
1 [![Build Status](https://travis-ci.org/abo-abo/hydra.svg?branch=master)](https://travis-ci.org/abo-abo/hydra)
2
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
5 Hydra.
6
7 ![hydra](http://oremacs.com/download/Hydra.png)
8
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
11 extension.
12
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.
18
19 ## Simplified usage
20
21 Here's how to quickly bind the examples bundled with Hydra:
22
23 ```cl
24 (require 'hydra-examples)
25 (hydra-create "C-M-y" hydra-example-move-window-splitter)
26 (hydra-create "M-g" hydra-example-goto-error)
27 (hydra-create "<f2>" hydra-example-text-scale)
28 ```
29
30 ## Using Hydra for global bindings
31
32 But it's much better to just take the examples as a template and write
33 down everything explicitly:
34
35 ```cl
36 (defhydra hydra-zoom (global-map "<f2>")
37 "zoom"
38 ("g" text-scale-increase "in")
39 ("l" text-scale-decrease "out"))
40 ```
41
42 With the example above, you can e.g.:
43
44 ```cl
45 (key-chord-define-global "tt" 'hydra-zoom/body)
46 ```
47
48 In fact, since `defhydra` returns the body symbol, you can even write
49 it like this:
50
51 ```cl
52 (key-chord-define-global
53 "tt"
54 (defhydra hydra-zoom (global-map "<f2>")
55 "zoom"
56 ("g" text-scale-increase "in")
57 ("l" text-scale-decrease "out")))
58 ```
59
60 If you like key chords so much that you don't want to touch the global
61 map at all, you can e.g.:
62
63 ```
64 (key-chord-define-global
65 "hh"
66 (defhydra hydra-error ()
67 "goto-error"
68 ("h" first-error "first")
69 ("j" next-error "next")
70 ("k" previous-error "prev")))
71 ```
72
73 You can also substitute `global-map` with any other keymap, like
74 `c++-mode-map` or `yas-minor-mode-map`.
75
76 See the [introductory blog post](http://oremacs.com/2015/01/20/introducing-hydra/) for more information.
77
78 ## Using Hydra for major-mode or minor-mode bindings
79
80 Here's an example:
81
82 ```cl
83 (defhydra lispy-vi (lispy-mode-map "C-z")
84 "vi"
85 ("l" forward-char)
86 ("h" backward-char)
87 ("j" next-line)
88 ("k" previous-line))
89 ```
90
91 ## Can Hydras can be helpful?
92
93 They can, if
94
95 ```cl
96 (setq hydra-is-helpful t)
97 ```
98
99 This is the default setting. In this case, you'll get a hint in the
100 echo area consisting of current Hydra's base comment and heads. You
101 can even add comments to the heads like this:
102
103 ```
104 (defhydra hydra-zoom (global-map "<f2>")
105 "zoom"
106 ("g" text-scale-increase "in")
107 ("l" text-scale-decrease "out"))
108 ```
109
110 With this, you'll see `zoom: [g]: in, [l]: out.` in your echo area,
111 once the zoom Hydra becomes active.
112
113 ## Colorful Hydras
114
115 Since version `0.5.0`, Hydra's heads all have a color associated with them:
116
117 - *red* (default) means the calling this head will not vanquish the Hydra
118 - *blue* means that the Hydra will be vanquished after calling this head
119
120 In all the older examples, all heads are red by default. You can specify blue heads like this:
121
122 ```cl
123 (global-set-key
124 (kbd "C-c C-v")
125 (defhydra toggle ()
126 "toggle"
127 ("a" abbrev-mode "abbrev" :color blue)
128 ("d" toggle-debug-on-error "debug" :color blue)
129 ("f" auto-fill-mode "fill" :color blue)
130 ("t" toggle-truncate-lines "truncate" :color blue)
131 ("w" whitespace-mode "whitespace" :color blue)
132 ("q" nil "cancel")))
133 ```
134
135 Or, since the heads can inherit the color from the body, the following is equivalent:
136
137 ```cl
138 (global-set-key
139 (kbd "C-c C-v")
140 (defhydra toggle (:color blue)
141 "toggle"
142 ("a" abbrev-mode "abbrev")
143 ("d" toggle-debug-on-error "debug")
144 ("f" auto-fill-mode "fill")
145 ("t" toggle-truncate-lines "truncate")
146 ("w" whitespace-mode "whitespace")
147 ("q" nil "cancel")))
148 ```
149
150 The above Hydra is very similar to this code:
151
152 ```cl
153 (global-set-key (kbd "C-c C-v t") 'toggle-truncate-lines)
154 (global-set-key (kbd "C-c C-v f") 'auto-fill-mode)
155 (global-set-key (kbd "C-c C-v a") 'abbrev-mode)
156 ```
157
158 However, there are two important differences:
159
160 - you get a hint like this right after <kbd>C-c C-v</kbd>:
161
162 toggle: [t]: truncate, [f]: fill, [a]: abbrev, [q]: cancel.
163
164 - you can cancel <kbd>C-c C-v</kbd> with a command while executing that command, instead of e.g.
165 getting an error `C-c C-v C-n is undefined` for <kbd>C-c C-v C-n</kbd>.