]> code.delx.au - gnu-emacs-elpa/blob - doc/ivy.org
37c215b13142c183333278ad1c64982a86eada11
[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:6 num:6 toc:4
13 #+STARTUP: indent
14
15 * Copying
16 :PROPERTIES:
17 :COPYING: t
18 :END:
19
20 #+BEGIN_TEXINFO
21 @ifnottex
22 Ivy manual, version 0.7.0
23
24 Ivy is an interactive interface for completion in Emacs. Emacs uses
25 completion mechanism in a variety of contexts: code, menus, commands,
26 variables, functions, etc. Completion entails listing, sorting,
27 filtering, previewing, and applying actions on selected items. When
28 active, @code{ivy-mode} completes the selection process by narrowing
29 available choices while previewing in the minibuffer. Selecting the
30 final candidate is either through simple keyboard character inputs or
31 through powerful regular expressions. @end ifnottex
32
33 Copyright @copyright{} 2015 Free Software Foundation, Inc.
34
35 @quotation
36 Permission is granted to copy, distribute and/or modify this document
37 under the terms of the GNU Free Documentation License, Version 1.3 or
38 any later version published by the Free Software Foundation; with no
39 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
40 and with the Back-Cover Texts as in (a) below. A copy of the license
41 is included in the section entitled ``GNU Free Documentation License.''
42
43 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
44 modify this GNU manual.''
45 @end quotation
46 #+END_TEXINFO
47
48 * Introduction
49 Ivy is for quick and easy selection from a list. When Emacs prompts
50 for a string from a list of several possible choices, Ivy springs into
51 action to assist in narrowing and picking the right string from a vast
52 number of choices.
53
54 Ivy strives for minimalism, simplicity, customizability and
55 discoverability.
56
57 #+BEGIN_TEXINFO
58 @subsubheading Minimalism
59 #+END_TEXINFO
60 Uncluttered minibuffer is minimalism. Ivy shows the completion
61 defaults, the number of matches, and 10 candidate matches below the
62 input line. Customize =ivy-length= to adjust the number of candidate
63 matches displayed in the minibuffer.
64
65 #+BEGIN_TEXINFO
66 @subsubheading Simplicity
67 #+END_TEXINFO
68 Simplicity is about Ivy's behavior in the minibuffer. It is also about
69 the code interface to extend Ivy's functionality. The minibuffer area
70 behaves as close to =fundamental-mode= as possible. ~SPC~ inserts a
71 space, for example, instead of being bound to the more complex
72 =minibuffer-complete-word=. Ivy's code uses easy-to-examine global
73 variables; avoids needless complications with branch-introducing
74 custom macros.
75
76 #+BEGIN_TEXINFO
77 @subsubheading Customizability
78 #+END_TEXINFO
79 Customizability is about being able to use different methods and
80 interfaces of completion to tailor the selection process. For example,
81 adding a custom display function that points to a selected candidate
82 with =->=, instead of highlighting the selected candidate with the
83 =ivy-current-match= face. Or take the customization of actions, say
84 after the candidate function is selected. ~RET~ uses
85 =counsel-describe-function= to describe the function, whereas ~M-o d~
86 jumps to that function's definition in the code. The ~M-o~ prefix can
87 be uniformly used with characters like ~d~ to group similar actions.
88
89 #+BEGIN_TEXINFO
90 @subsubheading Discoverability
91 #+END_TEXINFO
92 Ivy displays easily discoverable commands through the hydra facility.
93 ~C-o~ in the minibuffer displays a hydra menu. It opens up within an
94 expanded minibuffer area. Each menu item comes with short
95 documentation strings and highlighted one-key completions. So
96 discovering even seldom used keys is simply a matter of ~C-o~ in the
97 minibuffer while in the midst of the Ivy interaction. This
98 discoverability minimizes exiting Ivy interface for documentation
99 look-ups.
100
101 * Installation
102
103 Install Ivy automatically through Emacs's package manager, or manually
104 from Ivy's development repository.
105
106 ** Installing from Emacs Package Manager
107
108 ~M-x~ =package-install= ~RET~ =swiper= ~RET~
109
110 Ivy is installed as part of =swiper= package. =swiper= is available
111 from two different package archives, GNU ELPA and MELPA. For the
112 latest stable version, use the GNU ELPA archives using the above M-x
113 command.
114
115 For current hourly builds, use the MELPA archives. See the code below
116 for adding MELPA to the list of package archives:
117
118 #+begin_src elisp
119 (require 'package)
120 (add-to-list 'package-archives
121 '("melpa" . "http://melpa.org/packages/"))
122 #+end_src
123
124 After this do ~M-x~ =package-refresh-contents= ~RET~, followed by
125 ~M-x~ =package-install= ~RET~ =swiper= ~RET~.
126
127 For package manager details, see [[info:emacs#Packages]].
128
129 ** Installing from the Git repository
130
131 Why install from Git?
132
133 - No need to wait for MELPA's hourly builds
134 - Easy to revert to previous versions
135 - Contribute to Ivy's development; send patches; pull requests
136
137 *Configuration steps*
138
139 First clone the Swiper repository:
140 #+begin_src sh
141 cd ~/git && git clone https://github.com/abo-abo/swiper
142 cd swiper && make compile
143 #+end_src
144
145 Then add this to Emacs init:
146 #+begin_src elisp
147 (add-to-list 'load-path "~/git/swiper/")
148 (require 'ivy)
149 #+end_src
150
151 To update the code:
152 #+begin_src sh
153 git pull
154 make
155 #+end_src
156
157 * Getting started
158
159 First enable Ivy completion everywhere:
160
161 #+begin_src elisp
162 (ivy-mode 1)
163 #+end_src
164
165 Note: =ivy-mode= can be toggled on and off with ~M-x~ =ivy-mode=.
166 ** Basic customization
167 Here are some basic settings particularly useful for new Ivy
168 users:
169 #+begin_src elisp
170 (setq ivy-use-virtual-buffers t)
171 (setq ivy-height 10)
172 (setq ivy-display-style 'fancy)
173 (setq ivy-count-format "(%d/%d) ")
174 #+end_src
175
176 For additional customizations, refer to =M-x describe-variable=
177 documentation.
178
179 * Key bindings
180 ** Global key bindings
181
182 Recommended key bindings are:
183 #+BEGIN_TEXINFO
184 @subsubheading Ivy-based interface to standard commands
185 #+END_TEXINFO
186 #+begin_src elisp
187 (global-set-key (kbd "C-s") 'swiper)
188 (global-set-key (kbd "M-x") 'counsel-M-x)
189 (global-set-key (kbd "C-x C-f") 'counsel-find-file)
190 (global-set-key (kbd "<f1> f") 'counsel-describe-function)
191 (global-set-key (kbd "<f1> v") 'counsel-describe-variable)
192 (global-set-key (kbd "<f1> l") 'counsel-load-library)
193 (global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
194 (global-set-key (kbd "<f2> u") 'counsel-unicode-char)
195 #+end_src
196 #+BEGIN_TEXINFO
197 @subsubheading Ivy-based interface to shell and system tools
198 #+END_TEXINFO
199 #+begin_src elisp
200 (global-set-key (kbd "C-c g") 'counsel-git)
201 (global-set-key (kbd "C-c j") 'counsel-git-grep)
202 (global-set-key (kbd "C-c k") 'counsel-ag)
203 (global-set-key (kbd "C-x l") 'counsel-locate)
204 (global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
205 #+end_src
206 #+BEGIN_TEXINFO
207 @subsubheading Ivy-resume and other commands
208 #+END_TEXINFO
209 =ivy-resume= resumes the last Ivy-based completion.
210 #+begin_src elisp
211 (global-set-key (kbd "C-c C-r") 'ivy-resume)
212 #+end_src
213
214 ** Minibuffer key bindings
215
216 Ivy includes several minibuffer bindings, which are defined in the
217 =ivy-minibuffer-map= keymap variable. The most frequently used ones
218 are described here.
219
220 =swiper= or =counsel-M-x= add more through the =keymap= argument to
221 =ivy-read=. These keys, also active in the minibuffer, are described
222 under their respective commands.
223
224 *** Key bindings for navigation
225
226 - ~C-n~ (=ivy-next-line=) select next candidate
227 - ~C-p~ (=ivy-previous-line=) selects previous candidate
228 - ~M-<~ (=ivy-beginning-of-buffer=) selects the first candidate
229 - ~M->~ (=ivy-end-of-buffer=) selects the last candidate
230 - ~C-v~ (=ivy-scroll-up-command=) scrolls up by 10 lines
231 - ~M-v~ (=ivy-scroll-down-command=) scrolls down by 10 lines
232
233 To get the wrap-around behavior of ~C-n~ and ~C-p~ to cycle past the
234 last and first candidates, change the default (=ivy-wrap nil=) to
235 (=ivy-warp t=).
236
237 Adjust scroll size through the variable =ivy-height=, which by default
238 is set to 10.
239
240 *** Key bindings for single selection, action, then exit minibuffer
241
242 Ivy can offer several actions from which to choose which action to
243 run. This "calling an action" operates on the selected candidate. For
244 example, when viewing a list of files, one action could open it for
245 editing, one to view it, another to invoke a special function, and so
246 on. Custom actions can be added to this interface. The precise action
247 to call on the selected candidate can be delayed until after the
248 narrowing is completed. No need to exit the interface if unsure which
249 action to run. This delayed flexibility and customization of actions
250 extends usability of lists in Emacs.
251
252 ~C-m~ or ~RET~ (=ivy-done=) calls the default action and exits the
253 minibuffer.
254
255 ~M-o~ (=ivy-dispatching-done=) presents all available valid actions
256 from which to choose. When there is only one action available, there
257 is no difference between ~M-o~ and ~C-m~.
258
259 ~C-j~ (=ivy-alt-done=) calls the alternate action, such as completing
260 a directory name in a file list whereas ~C-m~ will select that directory
261 and exit the minibuffer.
262
263 Exiting the minibuffer also closes the Ivy window (as specified by
264 =ivy-height=). This closing and exiting sequence is conveniently off
265 when applying multiple actions. Multiple actions and multiple
266 selections as covered in the next section of this manual.
267
268 ~TAB~ (=ivy-partial-or-done=) attempts partial completion, extending
269 current input as much as possible.
270
271 ~TAB TAB~ is the same as ~C-j~.
272
273 ~C-M-j~ (=ivy-immediate-done=) is useful when there is no match for
274 the given input. Or there is an incorrect partial match. ~C-M-j~ with
275 =find-file= lists ignores the partial match and instead takes the
276 current input to create a new directory with =dired-create-directory=.
277
278 =ivy-immediate-done= illustrates how Ivy distinguishes between calling
279 an action on the /currently selected/ candidate and calling an action
280 on the /current input/.
281
282 #+BEGIN_TEXINFO
283 Invoking avy completion with @kbd{C-'} (@code{ivy-avy}).
284 #+END_TEXINFO
285 ~C-`~ uses avy's visible jump mechanism, which can further reduce
286 Ivy's line-by-line scrolling that requires multiple ~C-n~ or ~C-p~
287 keystrokes.
288
289 *** Key bindings for multiple selections and actions, keep minibuffer open
290
291 For repeatedly applying multiple actions or acting on multiple
292 candidates, Ivy does not close the minibuffer between commands. It
293 keeps the minibuffer open for applying subsequent actions.
294
295 Adding an extra meta key to the normal key chord invokes the special
296 version of the regular commands that enables applying multiple
297 actions.
298
299 ~C-M-m~ (=ivy-call=) is the non-exiting version of the default action,
300 ~C-m~ (=ivy-done=). Instead of closing the minibuffer, ~C-M-m~ allows
301 selecting another candidate or another action. For example, ~C-M-m~ on
302 functions list invokes =describe-function=. When combined with ~C-n~,
303 function descriptions can be invoked quickly in succession.
304
305 ~RET~ exits the minibuffer.
306
307 =ivy-resume= recalls the state of the completion session just before
308 its last exit. Useful after an accidental ~C-m~ (=ivy-done=).
309
310 ~C-M-o~ (=ivy-dispatching-call=) is a non-exiting version of ~M-o~
311 (=ivy-dispatching-done=) that can accumulate candidates into a queue.
312 For example, for playback in =counsel-rhythmbox=, ~C-M-o e~ en-queues
313 the selected candidate, and ~C-n C-m~ plays the next one in the queue.
314
315 ~C-M-n~ (=ivy-next-line-and-call=) combines ~C-n~ and ~C-M-m~. Applies
316 an action and moves to next line. Comes in handy when opening multiple
317 files from =counsel-find-file=, =counsel-git-grep=, =counsel-ag=, or
318 =counsel-locate= lists. Just hold ~C-M-n~ for rapid-fire default
319 action on each successive element of the list.
320
321 ~C-M-p~ (=ivy-previous-line-and-call=) combines ~C-p~ and ~C-M-m~. Is
322 the same as above except that it moves through the list in the other
323 direction.
324
325 *** Key bindings that alter minibuffer input
326
327 ~M-n~ (=ivy-next-history-element=) and ~M-p~
328 (=ivy-previous-history-element=) cycle through the Ivy command
329 history. Ivy updates an internal history list after each action. When
330 this history list is empty, ~M-n~ inserts symbol (or URL) at point
331 into the minibuffer.
332
333 ~M-i~ (=ivy-insert-current=) inserts the current candidate into the
334 minibuffer. Useful for copying and renaming files, for example: ~M-i~
335 to insert the original file name string, edit it, and then ~C-m~ to
336 complete the renaming.
337
338 ~M-j~ (=ivy-yank-word=) inserts sub-word at point into minibuffer. This
339 is similar to ~C-s C-w~ with =isearch=. Ivy reserves ~C-w~ for
340 =kill-region=.
341
342 ~S-SPC~ (=ivy-restrict-to-matches=) deletes the current input, and
343 resets the candidates list to the currently restricted matches. This
344 is how Ivy provides narrowing in successive tiers.
345
346 ~C-r~ (=ivy-reverse-i-search=) works just like ~C-r~ at bash command
347 prompt, where the completion candidates are the history items. Upon
348 completion, the selected candidate string is inserted into the
349 minibuffer.
350
351 *** Other key bindings
352
353 ~M-w~ (=ivy-kill-ring-save=) copies selected candidates to the kill
354 ring; when the region is active, copies active region.
355
356 *** Hydra in the minibuffer
357
358 ~C-o~ (=hydra-ivy/body=) invokes Hydra menus with key shortcuts.
359
360 ~C-o~ or ~i~ resumes editing.
361
362 Hydra reduces key strokes, for example: ~C-n C-n C-n C-n~ is ~C-o
363 jjjj~ in Hydra. Hydra has other benefits besides certain shorter key
364 bindings:
365 - ~<~ and ~>~ to adjust height of minibuffer,
366 - describes the current completion state, such as case folding and the
367 current action.
368
369 Minibuffer editing is disabled when Hydra is active.
370
371 *** Saving the current completion session to a buffer
372
373 ~C-c C-o~ (=ivy-occur=) saves the current candidates to a new buffer;
374 the list is active in the new buffer.
375
376 ~RET~ or ~mouse-1~ in the new buffer calls the appropriate action on
377 the selected candidate.
378
379 Ivy has no limit on the number of active buffers like these.
380
381 Ivy takes care of making these buffer names unique. It applies
382 descriptive names, for example: =*ivy-occur counsel-describe-variable
383 "function$*=.
384
385 * Completion styles
386
387 Ivy's completion functions rely on the highly configurable regex
388 builder.
389
390 The default is:
391 #+begin_src elisp
392 (setq ivy-re-builders-alist
393 '((t . ivy--regex-plus)))
394 #+end_src
395
396 The default =ivy--regex-plus= narrowing is always invoked unless
397 specified otherwise. For example, file name completion may have a
398 custom completion function:
399 #+begin_src elisp
400 (setq ivy-re-builders-alist
401 '((read-file-name-internal . ivy--regex-fuzzy)
402 (t . ivy--regex-plus)))
403 #+end_src
404
405 Ivy's flexibility extends to using different styles of completion
406 mechanics (regex-builders) for different types of lists. Despite this
407 flexibility, Ivy operates within a consistent and uniform interface.
408 The main regex-builders currently in Ivy are:
409
410 ** ivy--regex-plus
411
412 =ivy--regex-plus= is Ivy's default completion method.
413
414 =ivy--regex-plus= matches by splitting the input by spaces and
415 rebuilding it into a regex.
416
417 As the search string is typed in Ivy's minibuffer, it is transformed
418 into proper regex syntax. If the string is "for example", it is
419 transformed into
420
421 #+BEGIN_EXAMPLE
422 "\\(for\\).*\\(example\\)"
423 #+END_EXAMPLE
424
425 which in regex terminology matches "for" followed by a wild card and
426 then "example". Note how Ivy uses the space character to build
427 wild cards. For literal white space matching in Ivy, use an extra space:
428 to match one space type two spaces, to match two spaces type three
429 spaces, and so on.
430
431 As Ivy transforms typed characters into regex strings, it provides an
432 intuitive feedback through font highlights.
433
434 Ivy supports regexp negation with "!". For example, "define key ! ivy
435 quit" first selects everything matching "define.*key", then removes
436 everything matching "ivy", and finally removes everything matching
437 "quit". What remains is the final result set of the negation regexp.
438
439 #+BEGIN_EXAMPLE
440 Standard regexp identifiers work:
441
442 "^", "$", "\b" or "[a-z]"
443 #+END_EXAMPLE
444
445 Since Ivy treats minibuffer input as a regexp, standard regexp
446 identifiers work as usual. The exceptions are spaces, which
447 translate to ".*", and "!" that signal the beginning of a negation
448 group.
449
450 ** ivy--regex-ignore-order
451
452 =ivy--regex-ignore-order= ignores the order of regexp tokens when
453 searching for matching candidates. For instance, the input "for
454 example" will match "example test for". Otherwise =ivy--regex-plus=
455 normal behavior is to honor the order of regexp tokens.
456
457 ** ivy--regex-fuzzy
458
459 =ivy--regex-fuzzy= splits each character with a wild card. Searching
460 for "for" returns all "f.*o.*r" matches, resulting in a large number
461 of hits. Yet some searches need these extra hits. Ivy sorts such
462 large lists using =flx= package's scoring mechanism, if it's
463 installed.