]> code.delx.au - gnu-emacs-elpa/blob - doc/ivy.org
doc/ivy.org: Start writing a manual
[gnu-emacs-elpa] / doc / ivy.org
1 #+TITLE: Ivy User Manual
2 #+AUTHOR: Oleh Krehel
3 #+EMAIL: ohwoeowho@gmail.com
4 #+DATE: 2015
5 #+LANGUAGE: en
6
7 #+TEXINFO_DIR_CATEGORY: Emacs
8 #+TEXINFO_DIR_TITLE: Ivy: (ivy).
9 #+TEXINFO_DIR_DESC: Using Ivy for completion.
10 #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="style.css"/>
11
12 #+OPTIONS: H:4 num:3 toc:2
13 #+STARTUP: indent
14
15 * Copying
16 :PROPERTIES:
17 :COPYING: t
18 :END:
19
20 #+BEGIN_TEXINFO
21 @ifnottex
22 This manual is for Ivy version 0.7.0.
23
24 Ivy is a completion interface for Emacs. When @code{ivy-mode} is active,
25 any time another Elisp code requires completion you'll be able to
26 preview the candidates in the minibuffer and select them with both
27 simple input and powerful regexps.
28 @end ifnottex
29
30 Copyright @copyright{} 2015 Free Software Foundation, Inc.
31
32 @quotation
33 Permission is granted to copy, distribute and/or modify this document
34 under the terms of the GNU Free Documentation License, Version 1.3 or
35 any later version published by the Free Software Foundation; with no
36 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
37 and with the Back-Cover Texts as in (a) below. A copy of the license
38 is included in the section entitled ``GNU Free Documentation License.''
39
40 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
41 modify this GNU manual.''
42 @end quotation
43 #+END_TEXINFO
44
45 * Introduction
46 Any time Emacs prompts you for a string, with several choices, Ivy
47 allows you to preview and quickly select among these choices.
48
49 The key aspects of Ivy are minimalism, simplicity, customizability and
50 being discoverable.
51
52 #+BEGIN_TEXINFO
53 @subsubheading Minimalism
54 #+END_TEXINFO
55 Only the minimal necessary amount of information is
56 displayed in the minibuffer. Compared to the default completion,
57 additionally the current number of matches is displayed before the
58 prompt, and a few candidates are displayed below the input. You can
59 customize =ivy-length= to adjust the amount of displayed candidates,
60 it's 10 by default.
61
62 #+BEGIN_TEXINFO
63 @subsubheading Simplicity
64 #+END_TEXINFO
65 The minibuffer area should behave as a
66 =fundamental-mode= buffer as much as possible. For example, unlike the
67 default completion, ~SPC~ simply inserts a space, instead of being
68 bound to =minibuffer-complete-word=. Additionally Ivy aims to have
69 simple to understand code: everything is stored in easy to examine
70 global variables, and no branch-introducing custom macros are used.
71
72 #+BEGIN_TEXINFO
73 @subsubheading Customizability
74 #+END_TEXINFO
75 Ideally, you should be able to customize your completion session as
76 much as possible, with different settings for different completions,
77 if you so prefer. For example, you can add your own display function
78 to that points to a selected candidate with =->=, instead of
79 highlighting it with the =ivy-current-match= face. It's also possible
80 to customize many commands to do different things with the selected
81 candidate. For example, when describing functions with
82 =counsel-decribe-function=, you describe the candidate with ~RET~, but
83 can also jump to definition with ~M-o d~. While the ~M-o~ prefix isn't
84 normally customized, you can bind any following key like ~d~ to do
85 anything to the selected candidate. This way, it's possible to group
86 several functions into one through the completion interface.
87
88 #+BEGIN_TEXINFO
89 @subsubheading Discoverability
90 #+END_TEXINFO
91 While in the minibuffer, you can press ~C-o~ to call
92 =hydra-ivy/body=. This will expose a large amount of commands
93 available in the minibuffer, all annotated with short docstrings.
94
95 * Installation
96
97 Ivy can be installed using Emacs' package manager or manually from its
98 development repository.
99
100 ** Installing from ELPA
101 If you haven't used Emacs' package manager before, you can read about
102 it in the Emacs manual, see [[info:emacs#Packages]].
103
104 Ivy is available from both GNU ELPA - the official Emacs package
105 repository, and from MELPA - the most popular unofficial Emacs package
106 repository.
107
108 Your Emacs should be configured to use GNU ELPA automatically. To
109 also add MELPA, use this code:
110
111 #+begin_src elisp
112 (require 'package)
113 (add-to-list 'package-archives
114 '("melpa" . "http://melpa.org/packages/") t)
115 #+end_src
116
117 After this do ~M-x~ =package-refresh-contents= ~RET~, followed by
118 ~M-x~ =package-install= ~RET~ =swiper= ~RET~.
119
120 There's a version difference between GELPA and MELPA. GELPA holds the
121 latest stable version, while MELPA contains current hourly builds.
122
123 ** Installing from the Git repository
124
125 Installing from Git offers advanced users numerous advantages:
126
127 - You don't have to wait for MELPA's hourly build to finish to receive
128 an update.
129 - You can update to the current version and revert to an earlier one
130 whenever you want.
131 - You can contribute to the development of Ivy by editing the code and
132 sending patches/pull requests.
133
134 *Configuration steps*
135
136 Clone the Swiper repository:
137 #+begin_src sh
138 cd ~/git && git clone https://github.com/abo-abo/swiper
139 cd swiper && make compile
140 #+end_src
141
142 Add this code to your init:
143 #+begin_src elisp
144 (add-to-list 'load-path "~/git/swiper/")
145 (require 'ivy)
146 #+end_src
147
148 To update the code use:
149 #+begin_src sh
150 git pull
151 make
152 #+end_src
153
154 * Getting started
155
156 This section describes the most common configuration steps. First of
157 all, to get Ivy completion everywhere, use:
158
159 #+begin_src elisp
160 (ivy-mode 1)
161 #+end_src
162
163 You can also toggle =ivy-mode= on and off with ~M-x~ =ivy-mode=. This
164 is the minimal necessary step to get Ivy working.
165
166 ** Setting up Ivy global key bindings
167 These are the recommended mnemonic (and otherwise) key bindings:
168 #+BEGIN_TEXINFO
169 @subsubheading Ivy-improved versions of standard commands
170 #+END_TEXINFO
171 #+begin_src elisp
172 (global-set-key (kbd "C-s") 'swiper)
173 (global-set-key (kbd "M-x") 'counsel-M-x)
174 (global-set-key (kbd "C-x C-f") 'counsel-find-file)
175 (global-set-key (kbd "<f1> f") 'counsel-describe-function)
176 (global-set-key (kbd "<f1> v") 'counsel-describe-variable)
177 (global-set-key (kbd "<f1> l") 'counsel-load-library)
178 (global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
179 (global-set-key (kbd "<f2> u") 'counsel-unicode-char)
180 #+end_src
181 #+BEGIN_TEXINFO
182 @subsubheading Ivy-based interfaces to great shell and system tools
183 #+END_TEXINFO
184 #+begin_src elisp
185 (global-set-key (kbd "C-c g") 'counsel-git)
186 (global-set-key (kbd "C-c j") 'counsel-git-grep)
187 (global-set-key (kbd "C-c k") 'counsel-ag)
188 (global-set-key (kbd "C-x l") 'counsel-locate)
189 (global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
190 #+end_src
191 #+BEGIN_TEXINFO
192 @subsubheading Other useful commands
193 #+END_TEXINFO
194 The =ivy-resume= command allows to resume the last Ivy-based
195 completion.
196 #+begin_src elisp
197 (global-set-key (kbd "C-c C-r") 'ivy-resume)
198 #+end_src
199 ** Setting up common customizations
200
201 Here are some basic customizations that a new user might be interested
202 in, in no particular order:
203 #+begin_src elisp
204 (setq ivy-use-virtual-buffers t)
205 (setq ivy-height 10)
206 (setq ivy-display-style 'fancy)
207 (setq ivy-count-format "(%d/%d) ")
208 #+end_src
209
210 You can examine them more closely by looking at the documentation of
211 these variables.
212
213 ** Minibuffer bindings
214
215 Most of Ivy's minibuffer bindings are defined in =ivy-minibuffer-map=
216 keymap. Some commands, like =swiper= or =counsel-M-x= pass an
217 additional keymap through the =keymap= argument to =ivy-read=. The
218 additional bindings will be described in each command's section. This
219 section describes the most useful default key bindings.
220
221 *** Candidate navigation keys
222 The most basic navigation keys are ~C-n~ (=ivy-next-line=) and ~C-p~
223 (=ivy-previous-line=), they select the next and the previous candidate
224 respectively. By default, they don't wrap-around after reaching the
225 first or the last candidate. I you'd like to change this, customize
226 =ivy-wrap=.
227
228 Next, ~M-<~ (=ivy-beginning-of-buffer=) and ~M->~
229 (=ivy-end-of-buffer=) will select the first and the last candidate
230 respectively.
231
232 Additionally, ~C-v~ (=ivy-scroll-up-command=) and ~M-v~
233 (=ivy-scroll-down-command=) allow you to scroll by whole candidate
234 screen, which has the size =ivy-height=, 10 by default.
235
236 *** Candidate selection keys that exit the minibuffer
237 When you've finally selected a candidate you like, you'll want to do
238 something with it. In Ivy's terms it's called "calling an action",
239 which can also be combined with exiting the minibuffer and thus
240 finishing completion. Note that unlike with the default completion,
241 exiting the minibuffer is optional, because you might want to call an
242 action or actions for several candidates and not just one.
243
244 The most basic binding is ~C-m~ or ~RET~ (=ivy-done=). It calls the
245 action and exits the minibuffer.
246
247 The second important binding is ~C-j~ (=ivy-alt-done=). It's no
248 different from =ivy-done=, except when completing file names. In that
249 case pressing ~C-j~ on a directory will offer completion for that
250 directory, while ~C-m~ will select that directory and exit the
251 minibuffer.
252
253 Another binding, which may be familiar is ~TAB~
254 (=ivy-partial-or-done=). It will attempt to do partial completion:
255 extend the current input as much as possible, according to the
256 candidates that currently match. Pressing ~TAB TAB~ is equivalent to
257 ~C-j~.
258
259 With all above bindings, the action is called for the /currently
260 selected/ candidate. But what if the input you want isn't in the
261 collection, but still matches one of the candidates? Pressing either
262 ~C-m~, or ~C-j~ would call the action for that selected candidate,
263 which isn't what you wanted to do. Use ~C-M-j~ (=ivy-immediate-done=)
264 to call the action for /the current input/ instead of /the current
265 candidate/. Common uses of ~C-M-j~ are with =find-file= and
266 =dired-create-directory=: the new name might match the already
267 existing files or directories.
268
269 The penultimate key binding from the set that exits the minibuffer is
270 ~M-o~ (=ivy-dispatching-done=). In case the current completion has
271 more than one action to choose from to act on the selected candidate,
272 ~M-o~ will allow you to select and call that action. In case there's
273 only one action, ~M-o~ does the same and ~C-m~.
274
275 #+BEGIN_TEXINFO
276 And the final binding is @kbd{C-'} (@code{ivy-avy}).
277 #+END_TEXINFO
278 It allows to select a visible candidate faster than e.g. ~C-n C-n C-n C-n C-m~.
279
280 *** Candidate selection keys that don't exit the minibuffer
281 The bindings that don't exit the minibuffer are usually constructed by
282 adding the meta key to the other version.
283
284 ~C-M-m~ (=ivy-call=) is the non-exiting version of ~C-m~. Suppose you
285 have a =counsel-describe-function= completion session, you've narrowed
286 the candidate list significantly, say to 5 candidates, and you want to
287 describe the second and the fourth candidates. With the default
288 completion you would probably describe the second candidate, then
289 call =describe-function= again, recall history with ~M-p~ and edit it
290 to match the forth candidate and exit once more. With Ivy, you can
291 press ~C-n~ to select the second candidate, ~C-M-m~ to describe it,
292 ~C-n C-n~ to skip to the fourth candidate and ~C-m~ to describe it and
293 exit the minibuffer.
294
295 Alternatively, you could select the second candidate with ~C-m~, then
296 resume completion with =ivy-resume=. That will bring up the completion
297 session in a state as if you hadn't exited: the input will be the
298 same, and the second candidate will still be selected. Then you could
299 once again select the fourth one with ~C-n C-n C-m~.
300
301 ~C-M-o~ (=ivy-dispatching-call=) is a non-exiting version of ~M-o~.
302 It might be useful for instance in =counsel-rhythmbox=: use ~C-M-o e~
303 to enqueue the selected candidate, and ~C-n C-m~ to play the next
304 one. Here, =play= is the default action, and =enqueue= is an extra
305 action bound to ~e~.
306
307 ~C-M-n~ (=ivy-next-line-and-call=) is a combination of ~C-n~ and
308 ~C-M-m~. ~C-M-p~ (=ivy-previous-line-and-call=) is a combination of
309 ~C-p~ and ~C-M-m~. Both can be used to call the action many
310 times. For instance to open a lot of files in the current directory
311 with =counsel-find-file=, press and hold ~C-M-n~. Same for cycling
312 matches in =counsel-git-grep= / =counsel-ag= / =counsel-locate=.
313
314 *** Key bindings that change the minibuffer input
315 ~M-p~ (=ivy-previous-history-element=) and ~M-n~
316 (=ivy-next-history-element=) allow to cycle a command's history. A
317 new entry is added to the history each time an action is called on a
318 candidate. Additionally, ~M-n~ has a special behavior when it's the
319 first command (i.e. there's no history element to scroll down to): in
320 that case URL or symbol at point is inserted into the minibuffer.
321
322 ~M-i~ (=ivy-insert-current=) will insert the current candidate into
323 the minibuffer. It's especially useful for copying files to a
324 slightly different name: press ~M-i~ to insert the original, modify it
325 slightly and ~C-m~.
326
327 ~M-j~ (=ivy-yank-word=) will insert the subword at point into the
328 minibuffer. This is the closest thing to ~C-s C-w~ with
329 =isearch=. It's not bound to ~C-w~ because ~C-w~ calls =kill-region= -
330 a pretty useful editing function.
331
332 ~S-SPC~ (=ivy-restrict-to-matches=) will delete all current input. In
333 addition it will reset the candidates collection to the one that was
334 active at the moment of calling. This allows to narrow the candidate
335 list in tiers if necessary.
336
337 ~C-r~ (=ivy-reverse-i-search=) works in a similar way to ~C-r~ bash:
338 it opens a recursive completion session with the history elements as
339 candidates. Once finished, that history element is inserted into the
340 minibuffer.
341
342 *** Miscellaneous key bindings
343 ~M-w~ (=ivy-kill-ring-save=) will work as regular =kill-ring-save=
344 when the region is active, otherwise it will copy all selected
345 candidates to the kill ring.
346
347 *** The mini-documentation hydra
348 ~C-o~ (=hydra-ivy/body=) is a prefix to a multitude of shortcuts. For
349 example: ~C-n C-n C-n C-n~ is equivalent to ~C-o jjjj~. When ~C-o~ is
350 toggled on, you can no longer enter text into the minibuffer. If you
351 want to resume entering text, press ~C-o~ or ~i~.
352
353 It serves several purposes:
354
355 - It can be more efficient in terms of shorter bindings.
356 - It contains less popular bindings, like ~<~ and ~>~ for adjusting
357 the height of the minibuffer.
358 - It describes the current completion state, like the case folding and
359 the current action.
360
361 *** Storing the current completion session to a buffer
362 ~C-c C-o~ (=ivy-occur=) will store the current candidates into a new
363 buffer. Pressing ~RET~ or ~mouse-1~ in that buffer will result in the
364 appropriate action being called on the selected candidate. You can
365 have as many of these buffers as you like, and they will be named
366 appropriately to show what they do, e.g =*ivy-occur
367 cousnel-describe-variable "function$*=.
368
369 ** Completion styles
370 The completion in Ivy is customizable through regex builder functions.
371 The default settings start out at:
372 #+begin_src elisp
373 (setq ivy-re-builders-alist
374 '((t . ivy--regex-plus)))
375 #+end_src
376
377 Which means that =ivy--regex-plus= is used for all collections. Here's
378 how to use another re-builder specifically for file name completion:
379 #+begin_src elisp
380 (setq ivy-re-builders-alist
381 '((read-file-name-internal . ivy--regex-fuzzy)
382 (t . ivy--regex-plus)))
383 #+end_src
384
385 These two and other styles of re-builders will be described below.
386
387 *** ivy--regex-plus
388 The default completion method in Ivy is represented by the
389 =ivy--regex-plus= function. For this function, the matching is done by
390 splitting the input by spaces and rebuilding it into a regex.
391
392 So "for example" is transformed into "\\(for\\).*\\(example\\)", which
393 means to match "for", followed by wildcard, followed by "example".
394 You get used to how this works very fast since each part is
395 highlighted with a different face in the minibuffer.
396
397 If you need to match literal spaces, input that amount of spaces plus
398 one, e.g. input two spaces to match one, three to match two etc.
399
400 Regexp negation is also supported and is done by entering words that
401 you don't want to match after a "!". For example "define key ! ivy
402 quit" will first select everything that matches "define.*key", then
403 remove everything that matches "ivy" and everything that matches
404 "quit".
405
406 Other than spaces being translated into ".*" and "!" starting a
407 negation group, the minibuffer input is treated as a regular regexp,
408 so you can simply input things like "^", "$", "\b" or "[a-z]".
409
410 *** ivy--regex-ignore-order
411 This works similarly to =ivy--regex-plus= except the order of the
412 parts doesn't matter any more. For instance, the input "for example"
413 will match "example test for".
414
415 *** ivy--regex-fuzzy
416 This method splits each character separately, so "for" is translated
417 into "f.*o.*r". This means it might result in a huge amount of
418 matches. To manage this amount of matches somehow, you can install
419 the =flx= package which will automatically be used by Ivy to do the
420 candidate scoring. If you've used =ido-flx= before, it's almost the
421 same.