]> code.delx.au - gnu-emacs-elpa/blob - doc/ivy.org
doc/ivy.org: Improve the markup
[gnu-emacs-elpa] / doc / ivy.org
1 #+TITLE: Ivy User Manual
2 #+AUTHOR: Oleh Krehel
3 #+EMAIL: ohwoeowho@gmail.com
4 #+LANGUAGE: en
5
6 #+TEXINFO_DIR_CATEGORY: Emacs
7 #+TEXINFO_DIR_TITLE: Ivy: (ivy).
8 #+TEXINFO_DIR_DESC: Using Ivy for completion.
9 #+SETUPFILE: ~/git/org-html-themes/setup/theme-readtheorg.setup
10 #+HTML_HEAD: <link rel="stylesheet" type="text/css" href="kbd-style.css"/>
11 #+EXPORT_FILE_NAME: index.html
12
13 #+OPTIONS: H:6 num:6 toc:4
14 #+STARTUP: indent
15 * Setup :noexport:
16 #+BEGIN_SRC elisp :exports results :results silent
17 (add-to-list 'load-path default-directory)
18 (require 'ivy-ox)
19 #+END_SRC
20 * Writing this manual :noexport:
21 For highlighting a section without introducing a new subheading use
22 definition lists. The definition list "owns" the following text if the
23 text is indented by 5 spaces. Use ~C-q~ to indent these
24 paragraphs. New paragraphs can also be started, as long as they have
25 the 5 spaces indent.
26
27 For declaring a =@defopt= section for =defcustom= or =defvar=, also
28 use definition lists. They need to have the following form in order to
29 be recognized in the texinfo export:
30
31 #+BEGIN_EXAMPLE
32 User Option =ivy-wrap= ::
33 #+END_EXAMPLE
34
35 To name each heading, set its =CUSTOM_ID= property. This can be done
36 easily with =worf='s ~C-u L~.
37
38 * Copying
39 :PROPERTIES:
40 :COPYING: t
41 :CUSTOM_ID: copying
42 :END:
43 #+TEXINFO: @ifnottex
44 Ivy manual, version 0.7.0
45
46 Ivy is an interactive interface for completion in Emacs. Emacs uses
47 completion mechanism in a variety of contexts: code, menus, commands,
48 variables, functions, etc. Completion entails listing, sorting,
49 filtering, previewing, and applying actions on selected items. When
50 active, =ivy-mode= completes the selection process by narrowing
51 available choices while previewing in the minibuffer. Selecting the
52 final candidate is either through simple keyboard character inputs or
53 through powerful regular expressions.
54 #+TEXINFO: @end ifnottex
55
56 Copyright (C) 2015 Free Software Foundation, Inc.
57
58 #+BEGIN_QUOTE
59 Permission is granted to copy, distribute and/or modify this document
60 under the terms of the GNU Free Documentation License, Version 1.3 or
61 any later version published by the Free Software Foundation; with no
62 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
63 and with the Back-Cover Texts as in (a) below. A copy of the license
64 is included in the section entitled ``GNU Free Documentation License.''
65
66 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
67 modify this GNU manual.''
68 #+END_QUOTE
69
70 #+HTML: <a href="https://github.com/abo-abo/swiper/blob/master/doc/ivy.org">This manual source</a>
71 * Introduction
72 :PROPERTIES:
73 :CUSTOM_ID: introduction
74 :END:
75 Ivy is for quick and easy selection from a list. When Emacs prompts
76 for a string from a list of several possible choices, Ivy springs into
77 action to assist in narrowing and picking the right string from a vast
78 number of choices.
79
80 Ivy strives for minimalism, simplicity, customizability and
81 discoverability.
82
83 - Minimalism ::
84 Uncluttered minibuffer is minimalism. Ivy shows the completion
85 defaults, the number of matches, and 10 candidate matches below
86 the input line. Customize =ivy-length= to adjust the number of
87 candidate matches displayed in the minibuffer.
88
89 - Simplicity ::
90 Simplicity is about Ivy's behavior in the minibuffer. It is also
91 about the code interface to extend Ivy's functionality. The
92 minibuffer area behaves as close to =fundamental-mode= as
93 possible. ~SPC~ inserts a space, for example, instead of being
94 bound to the more complex =minibuffer-complete-word=. Ivy's code
95 uses easy-to-examine global variables; avoids needless
96 complications with branch-introducing custom macros.
97
98 - Customizability ::
99 Customizability is about being able to use different methods and
100 interfaces of completion to tailor the selection process. For
101 example, adding a custom display function that points to a
102 selected candidate with =->=, instead of highlighting the
103 selected candidate with the =ivy-current-match= face. Or take the
104 customization of actions, say after the candidate function is
105 selected. ~RET~ uses =counsel-describe-function= to describe the
106 function, whereas ~M-o d~ jumps to that function's definition in
107 the code. The ~M-o~ prefix can be uniformly used with characters
108 like ~d~ to group similar actions.
109
110 - Discoverability ::
111 Ivy displays easily discoverable commands through the hydra
112 facility. ~C-o~ in the minibuffer displays a hydra menu. It
113 opens up within an expanded minibuffer area. Each menu item comes
114 with short documentation strings and highlighted one-key
115 completions. So discovering even seldom used keys is simply a
116 matter of ~C-o~ in the minibuffer while in the midst of the Ivy
117 interaction. This discoverability minimizes exiting Ivy interface
118 for documentation look-ups.
119
120 * Installation
121 :PROPERTIES:
122 :CUSTOM_ID: installation
123 :END:
124
125 Install Ivy automatically through Emacs's package manager, or manually
126 from Ivy's development repository.
127
128 ** Installing from Emacs Package Manager
129 :PROPERTIES:
130 :CUSTOM_ID: installing-from-emacs-package-manager
131 :END:
132
133 ~M-x~ =package-install= ~RET~ =swiper= ~RET~
134
135 Ivy is installed as part of =swiper= package. =swiper= is available
136 from two different package archives, GNU ELPA and MELPA. For the
137 latest stable version, use the GNU ELPA archives using the above M-x
138 command.
139
140 For current hourly builds, use the MELPA archives. See the code below
141 for adding MELPA to the list of package archives:
142
143 #+begin_src elisp
144 (require 'package)
145 (add-to-list 'package-archives
146 '("melpa" . "http://melpa.org/packages/"))
147 #+end_src
148
149 After this do ~M-x~ =package-refresh-contents= ~RET~, followed by
150 ~M-x~ =package-install= ~RET~ =swiper= ~RET~.
151
152 For package manager details, see [[info:emacs#Packages]].
153
154 ** Installing from the Git repository
155 :PROPERTIES:
156 :CUSTOM_ID: installing-from-the-git-repository
157 :END:
158
159 Why install from Git?
160
161 - No need to wait for MELPA's hourly builds
162 - Easy to revert to previous versions
163 - Contribute to Ivy's development; send patches; pull requests
164
165 *Configuration steps*
166
167 First clone the Swiper repository:
168 #+begin_src sh
169 cd ~/git && git clone https://github.com/abo-abo/swiper
170 cd swiper && make compile
171 #+end_src
172
173 Then add this to Emacs init:
174 #+begin_src elisp
175 (add-to-list 'load-path "~/git/swiper/")
176 (require 'ivy)
177 #+end_src
178
179 To update the code:
180 #+begin_src sh
181 git pull
182 make
183 #+end_src
184
185 * Getting started
186 :PROPERTIES:
187 :CUSTOM_ID: getting-started
188 :END:
189 First, enable Ivy completion everywhere:
190
191 #+begin_src elisp
192 (ivy-mode 1)
193 #+end_src
194
195 Note: =ivy-mode= can be toggled on and off with ~M-x~ =ivy-mode=.
196 ** Basic customization
197 :PROPERTIES:
198 :CUSTOM_ID: basic-customization
199 :END:
200 Here are some basic settings particularly useful for new Ivy
201 users:
202 #+begin_src elisp
203 (setq ivy-use-virtual-buffers t)
204 (setq ivy-height 10)
205 (setq ivy-display-style 'fancy)
206 (setq ivy-count-format "(%d/%d) ")
207 #+end_src
208
209 For additional customizations, refer to =M-x describe-variable=
210 documentation.
211
212 * Key bindings
213 :PROPERTIES:
214 :CUSTOM_ID: key-bindings
215 :END:
216 ** Global key bindings
217 :PROPERTIES:
218 :CUSTOM_ID: global-key-bindings
219 :END:
220 The recommended key bindings are:
221
222 - Ivy-based interface to standard commands ::
223 #+begin_src elisp
224 (global-set-key (kbd "C-s") 'swiper)
225 (global-set-key (kbd "M-x") 'counsel-M-x)
226 (global-set-key (kbd "C-x C-f") 'counsel-find-file)
227 (global-set-key (kbd "<f1> f") 'counsel-describe-function)
228 (global-set-key (kbd "<f1> v") 'counsel-describe-variable)
229 (global-set-key (kbd "<f1> l") 'counsel-load-library)
230 (global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
231 (global-set-key (kbd "<f2> u") 'counsel-unicode-char)
232 #+end_src
233
234 - Ivy-based interface to shell and system tools ::
235 #+begin_src elisp
236 (global-set-key (kbd "C-c g") 'counsel-git)
237 (global-set-key (kbd "C-c j") 'counsel-git-grep)
238 (global-set-key (kbd "C-c k") 'counsel-ag)
239 (global-set-key (kbd "C-x l") 'counsel-locate)
240 (global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
241 #+end_src
242
243 - Ivy-resume and other commands ::
244 =ivy-resume= resumes the last Ivy-based completion.
245 #+begin_src elisp
246 (global-set-key (kbd "C-c C-r") 'ivy-resume)
247 #+end_src
248
249 ** Minibuffer key bindings
250 :PROPERTIES:
251 :CUSTOM_ID: minibuffer-key-bindings
252 :END:
253
254 Ivy includes several minibuffer bindings, which are defined in the
255 =ivy-minibuffer-map= keymap variable. The most frequently used ones
256 are described here.
257
258 =swiper= or =counsel-M-x= add more through the =keymap= argument to
259 =ivy-read=. These keys, also active in the minibuffer, are described
260 under their respective commands.
261
262 *** Key bindings for navigation
263 :PROPERTIES:
264 :CUSTOM_ID: key-bindings-for-navigation
265 :END:
266
267 - ~C-n~ (=ivy-next-line=) selects the next candidate
268 - ~C-p~ (=ivy-previous-line=) selects the previous candidate
269 - ~M-<~ (=ivy-beginning-of-buffer=) selects the first candidate
270 - ~M->~ (=ivy-end-of-buffer=) selects the last candidate
271 - ~C-v~ (=ivy-scroll-up-command=) scrolls up by =ivy-height= lines
272 - ~M-v~ (=ivy-scroll-down-command=) scrolls down by =ivy-height= lines
273
274
275 - User Option =ivy-wrap= ::
276 This user option allows to get the wrap-around behavior for ~C-n~
277 and ~C-p~. When set to =t=, =ivy-next-line= and
278 =ivy-previous-line= will cycle past the last and the first
279 candidates respectively.
280
281 This behavior is off by default.
282
283 - User Option =ivy-height= ::
284 Use this variable to adjust the minibuffer height, and therefore
285 the scroll size for ~C-v~ and ~M-v~.
286
287
288 *** Key bindings for single selection, action, then exit minibuffer
289 :PROPERTIES:
290 :CUSTOM_ID: key-bindings-for-single-selection-action-then-exit-minibuffer
291 :END:
292
293 Ivy can offer several actions from which to choose which action to
294 run. This "calling an action" operates on the selected candidate. For
295 example, when viewing a list of files, one action could open it for
296 editing, one to view it, another to invoke a special function, and so
297 on. Custom actions can be added to this interface. The precise action
298 to call on the selected candidate can be delayed until after the
299 narrowing is completed. No need to exit the interface if unsure which
300 action to run. This delayed flexibility and customization of actions
301 extends usability of lists in Emacs.
302
303 - ~C-m~ or ~RET~ (=ivy-done=) ::
304 Calls the default action and exits the minibuffer.
305
306 - ~M-o~ (=ivy-dispatching-done=) ::
307 Presents all available valid actions from which to choose. When
308 there is only one action available, there is no difference
309 between ~M-o~ and ~C-m~.
310
311 - ~C-j~ (=ivy-alt-done=) ::
312 When completing file names, selects the current directory
313 candidate and starts a new completion session there. Otherwise,
314 it's the same as =ivy-done=.
315
316 - ~TAB~ (=ivy-partial-or-done=) ::
317 Attempts partial completion, extending current input as much as
318 possible. ~TAB TAB~ is the same as ~C-j~.
319
320 - ~C-M-j~ (=ivy-immediate-done=) ::
321 Exits with /the current input/ instead of /the current candidate/
322 (like other commands).
323
324 This is useful e.g. when you call =find-file= to create a new
325 file, but the desired name matches an existing file. In that
326 case, using ~C-j~ would select that existing file, which isn't
327 what you want - use this command instead.
328
329 - ~C-'~ (=ivy-avy=) ::
330 Uses avy to select one of the candidates on the current candidate
331 page. This can often be faster than multiple ~C-n~ or ~C-p~
332 keystrokes followed by ~C-m~.
333
334 *** Key bindings for multiple selections and actions, keep minibuffer open
335 :PROPERTIES:
336 :CUSTOM_ID: key-bindings-for-multiple-selections-and-actions-keep-minibuffer-open
337 :END:
338
339 For repeatedly applying multiple actions or acting on multiple
340 candidates, Ivy does not close the minibuffer between commands. It
341 keeps the minibuffer open for applying subsequent actions.
342
343 Adding an extra meta key to the normal key chord invokes the special
344 version of the regular commands that enables applying multiple
345 actions.
346
347 - ~C-M-m~ (=ivy-call=) ::
348 Is the non-exiting version of ~C-m~ (=ivy-done=).
349
350 Instead of closing the minibuffer, ~C-M-m~ allows selecting
351 another candidate or another action. For example, ~C-M-m~ on
352 functions list invokes =describe-function=. When combined with
353 ~C-n~, function descriptions can be invoked quickly in
354 succession.
355
356 - ~C-M-o~ (=ivy-dispatching-call=) ::
357 Is the non-exiting version of ~M-o~ (=ivy-dispatching-done=).
358
359 For example, during the =counsel-rhythmbox= completion, press
360 ~C-M-o e~ to en-queue the selected candidate, followed by ~C-n
361 C-m~ to play the next candidate - the current action reverts to
362 the default one after ~C-M-o~.
363
364 - ~C-M-n~ (=ivy-next-line-and-call=) ::
365 Combines ~C-n~ and ~C-M-m~. Applies an action and moves to next
366 line.
367
368 Comes in handy when opening multiple files from
369 =counsel-find-file=, =counsel-git-grep=, =counsel-ag=, or
370 =counsel-locate= lists. Just hold ~C-M-n~ for rapid-fire default
371 action on each successive element of the list.
372
373 - ~C-M-p~ (=ivy-previous-line-and-call=) ::
374 Combines ~C-p~ and ~C-M-m~.
375
376 Similar to the above except it moves through the list in the
377 other direction.
378
379 - =ivy-resume= ::
380 Recalls the state of the completion session just before its last exit.
381
382 Useful after an accidental ~C-m~ (=ivy-done=).
383
384 *** Key bindings that alter the minibuffer input
385 :PROPERTIES:
386 :CUSTOM_ID: key-bindings-that-alter-the-minibuffer-input
387 :END:
388
389 - ~M-n~ (=ivy-next-history-element=) ::
390 Cycles forward through the Ivy command history.
391
392 Ivy updates an internal history list after each action. When this
393 history list is empty, ~M-n~ inserts symbol (or URL) at point
394 into the minibuffer.
395
396 - ~M-p~ (=ivy-previous-history-element=) ::
397 Cycles forward through the Ivy command history.
398
399 - ~M-i~ (=ivy-insert-current=) ::
400 Inserts the current candidate into the minibuffer.
401
402 Useful for copying and renaming files, for example: ~M-i~ to
403 insert the original file name string, edit it, and then ~C-m~ to
404 complete the renaming.
405
406 - ~M-j~ (=ivy-yank-word=) ::
407 Inserts the sub-word at point into the minibuffer.
408
409 This is similar to ~C-s C-w~ with =isearch=. Ivy reserves ~C-w~
410 for =kill-region=.
411
412 - ~S-SPC~ (=ivy-restrict-to-matches=) ::
413 Deletes the current input, and resets the candidates list to the
414 currently restricted matches.
415
416 This is how Ivy provides narrowing in successive tiers.
417
418 - ~C-r~ (=ivy-reverse-i-search=) ::
419 Starts a recursive completion session through the command's
420 history.
421
422 This works just like ~C-r~ at the bash command prompt, where the
423 completion candidates are the history items. Upon completion, the
424 selected candidate string is inserted into the minibuffer.
425
426 *** Other key bindings
427 :PROPERTIES:
428 :CUSTOM_ID: other-key-bindings
429 :END:
430
431 - ~M-w~ (=ivy-kill-ring-save=) ::
432 Copies selected candidates to the kill ring.
433
434 When the region is active, copies active region instead.
435
436 *** Hydra in the minibuffer
437 :PROPERTIES:
438 :CUSTOM_ID: hydra-in-the-minibuffer
439 :END:
440
441 - ~C-o~ (=hydra-ivy/body=) ::
442 Invokes the hydra menu with short key bindings.
443
444 Minibuffer editing is disabled when Hydra is active. Instead, you get
445 short aliases for the common commands:
446
447 | Short | Normal | Command name |
448 |-------+-----------+---------------------------|
449 | ~o~ | ~C-g~ | =keyboard-escape-quit= |
450 | ~j~ | ~C-n~ | =ivy-next-line= |
451 | ~k~ | ~C-p~ | =ivy-previous-line= |
452 | ~h~ | ~M-<~ | =ivy-beginning-of-buffer= |
453 | ~l~ | ~M->~ | =ivy-end-of-buffer= |
454 | ~d~ | ~C-m~ | =ivy-done= |
455 | ~f~ | ~C-j~ | =ivy-alt-done= |
456 | ~g~ | ~C-M-m~ | =ivy-call= |
457 | ~u~ | ~C-c C-o~ | =ivy-occur= |
458
459 Hydra reduces key strokes, for example: ~C-n C-n C-n C-n~ is ~C-o
460 jjjj~ in Hydra.
461
462 Additionally, you get access to the folowing commands that are
463 normally not bound:
464
465 - ~c~ (=ivy-toggle-calling=) ::
466 Toggle calling the action after each candidate change. This
467 effectively modifies ~j~ to ~jg~, ~k~ to ~kg~ etc.
468
469 - ~m~ (=ivy-toggle-fuzzy=) ::
470 Toggle the current regexp matcher.
471
472 - ~>~ (=ivy-minibuffer-grow=) ::
473 Increase =ivy-height= for the current minibuffer.
474
475 - ~<~ (=ivy-minibuffer-shrink=) ::
476 Decrease =ivy-height= for the current minibuffer.
477
478 - ~w~ (=ivy-prev-action=) ::
479 Select the previous action.
480
481 - ~s~ (=ivy-next-action=) ::
482 Select the next action.
483
484 - ~a~ (=ivy-read-action=) ::
485 Use a menu to select an action.
486
487 - ~C~ (=ivy-toggle-case-fold=) ::
488 Toggle case folding (matching both upper and lower case
489 characters with lower case input).
490
491 *** Saving the current completion session to a buffer
492 :PROPERTIES:
493 :CUSTOM_ID: saving-the-current-completion-session-to-a-buffer
494 :END:
495
496 - ~C-c C-o~ (=ivy-occur=) ::
497 Saves the current candidates to a new buffer and exits
498 completion.
499
500 The new buffer is read-only and has a few useful bindings defined.
501
502 - ~RET~ or ~j~ (=ivy-occur-press=) ::
503 Call the current action on the selected candidate.
504
505 - ~mouse-1~ (=ivy-occur-click=) ::
506 Call the current action on the selected candidate.
507
508 - ~j~ (=next-line=) ::
509 Move to next line.
510
511 - ~k~ (=previous-line=) ::
512 Move to previous line.
513
514 - ~a~ (=ivy-occur-read-action=) ::
515 Read an action and make it current for this buffer.
516
517 - ~o~ (=ivy-occur-dispatch=) ::
518 Read an action and call it on the selected candidate.
519
520 - ~q~ (=quit-window=) ::
521 Bury the current buffer.
522
523
524 Ivy has no limit on the number of active buffers like these.
525
526 Ivy takes care of making these buffer names unique. It applies
527 descriptive names, for example: =*ivy-occur counsel-describe-variable
528 "function$*=.
529
530 * Completion styles
531 :PROPERTIES:
532 :CUSTOM_ID: completion-styles
533 :END:
534
535 Ivy's completion functions rely on the highly configurable regex
536 builder.
537
538 The default is:
539 #+begin_src elisp
540 (setq ivy-re-builders-alist
541 '((t . ivy--regex-plus)))
542 #+end_src
543
544 The default =ivy--regex-plus= narrowing is always invoked unless
545 specified otherwise. For example, file name completion may have a
546 custom completion function:
547 #+begin_src elisp
548 (setq ivy-re-builders-alist
549 '((read-file-name-internal . ivy--regex-fuzzy)
550 (t . ivy--regex-plus)))
551 #+end_src
552
553 Ivy's flexibility extends to using different styles of completion
554 mechanics (regex-builders) for different types of lists. Despite this
555 flexibility, Ivy operates within a consistent and uniform interface.
556 The main regex-builders currently in Ivy are:
557
558 ** ivy--regex-plus
559 :PROPERTIES:
560 :CUSTOM_ID: ivy--regex-plus
561 :END:
562
563 =ivy--regex-plus= is Ivy's default completion method.
564
565 =ivy--regex-plus= matches by splitting the input by spaces and
566 rebuilding it into a regex.
567
568 As the search string is typed in Ivy's minibuffer, it is transformed
569 into proper regex syntax. If the string is ="for example"=, it is
570 transformed into
571
572 #+begin_src elisp
573 "\\(for\\).*\\(example\\)"
574 #+end_src
575
576 which in regex terminology matches ="for"= followed by a wild card and
577 then ="example"=. Note how Ivy uses the space character to build wild
578 cards. For literal white space matching in Ivy, use an extra space: to
579 match one space type two spaces, to match two spaces type three
580 spaces, and so on.
581
582 As Ivy transforms typed characters into regex strings, it provides an
583 intuitive feedback through font highlights.
584
585 Ivy supports regexp negation with ="!"=.
586 For example, ="define key ! ivy quit"= first selects everything
587 matching ="define.*key"=, then removes everything matching ="ivy"=,
588 and finally removes everything matching ="quit"=. What remains is the
589 final result set of the negation regexp.
590
591 Since Ivy treats minibuffer input as a regexp, the standard regexp
592 identifiers work: ="^"=, ="$"=, ="\b"= or ="[a-z]"=. The exceptions
593 are spaces, which translate to =".*"=, and ="!"= that signal the
594 beginning of a negation group.
595
596 ** ivy--regex-ignore-order
597 :PROPERTIES:
598 :CUSTOM_ID: ivy--regex-ignore-order
599 :END:
600
601 =ivy--regex-ignore-order= ignores the order of regexp tokens when
602 searching for matching candidates. For instance, the input
603 ="for example"= will match ="example test for"=.
604
605 ** ivy--regex-fuzzy
606 :PROPERTIES:
607 :CUSTOM_ID: ivy--regex-fuzzy
608 :END:
609
610 =ivy--regex-fuzzy= splits each character with a wild card. Searching
611 for ="for"= returns all ="f.*o.*r"= matches, resulting in a large
612 number of hits. Yet some searches need these extra hits. Ivy sorts
613 such large lists using =flx= package's scoring mechanism, if it's
614 installed.
615
616 * Variable Index
617 :PROPERTIES:
618 :INDEX: vr
619 :CUSTOM_ID: variable-index
620 :END: