]> code.delx.au - gnu-emacs-elpa/blob - doc/ivy.org
doc/ivy.org: CUSTOM_ID should not end in "?"
[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 Definition lists need to be separated from regular lists with two newlines.
27
28 For declaring a =@defopt= section for =defcustom= or =defvar=, also
29 use definition lists. They need to have the following form in order to
30 be recognized in the texinfo export:
31
32 #+BEGIN_EXAMPLE
33 User Option =ivy-wrap= ::
34 #+END_EXAMPLE
35
36 To name each heading, set its =CUSTOM_ID= property. This can be done
37 easily with =worf='s ~C-u L~.
38
39 Texinfo export likes to have one empty line before each source block.
40 * Copying
41 :PROPERTIES:
42 :COPYING: t
43 :CUSTOM_ID: copying
44 :END:
45 #+TEXINFO: @ifnottex
46 Ivy manual, version 0.7.0
47
48 Ivy is an interactive interface for completion in Emacs. Emacs uses
49 completion mechanism in a variety of contexts: code, menus, commands,
50 variables, functions, etc. Completion entails listing, sorting,
51 filtering, previewing, and applying actions on selected items. When
52 active, =ivy-mode= completes the selection process by narrowing
53 available choices while previewing in the minibuffer. Selecting the
54 final candidate is either through simple keyboard character inputs or
55 through powerful regular expressions.
56 #+TEXINFO: @end ifnottex
57
58 Copyright (C) 2015 Free Software Foundation, Inc.
59
60 #+BEGIN_QUOTE
61 Permission is granted to copy, distribute and/or modify this document
62 under the terms of the GNU Free Documentation License, Version 1.3 or
63 any later version published by the Free Software Foundation; with no
64 Invariant Sections, with the Front-Cover Texts being ``A GNU Manual,''
65 and with the Back-Cover Texts as in (a) below. A copy of the license
66 is included in the section entitled ``GNU Free Documentation License.''
67
68 (a) The FSF's Back-Cover Text is: ``You have the freedom to copy and
69 modify this GNU manual.''
70 #+END_QUOTE
71
72 #+HTML: <a href="https://github.com/abo-abo/swiper/blob/master/doc/ivy.org">This manual source</a>
73 * Introduction
74 :PROPERTIES:
75 :CUSTOM_ID: introduction
76 :END:
77 Ivy is for quick and easy selection from a list. When Emacs prompts
78 for a string from a list of several possible choices, Ivy springs into
79 action to assist in narrowing and picking the right string from a vast
80 number of choices.
81
82 Ivy strives for minimalism, simplicity, customizability and
83 discoverability.
84
85 - Minimalism ::
86 Uncluttered minibuffer is minimalism. Ivy shows the completion
87 defaults, the number of matches, and 10 candidate matches below
88 the input line. Customize =ivy-length= to adjust the number of
89 candidate matches displayed in the minibuffer.
90
91 - Simplicity ::
92 Simplicity is about Ivy's behavior in the minibuffer. It is also
93 about the code interface to extend Ivy's functionality. The
94 minibuffer area behaves as close to =fundamental-mode= as
95 possible. ~SPC~ inserts a space, for example, instead of being
96 bound to the more complex =minibuffer-complete-word=. Ivy's code
97 uses easy-to-examine global variables; avoids needless
98 complications with branch-introducing custom macros.
99
100 - Customizability ::
101 Customizability is about being able to use different methods and
102 interfaces of completion to tailor the selection process. For
103 example, adding a custom display function that points to a
104 selected candidate with =->=, instead of highlighting the
105 selected candidate with the =ivy-current-match= face. Or take the
106 customization of actions, say after the candidate function is
107 selected. ~RET~ uses =counsel-describe-function= to describe the
108 function, whereas ~M-o d~ jumps to that function's definition in
109 the code. The ~M-o~ prefix can be uniformly used with characters
110 like ~d~ to group similar actions.
111
112 - Discoverability ::
113 Ivy displays easily discoverable commands through the hydra
114 facility. ~C-o~ in the minibuffer displays a hydra menu. It
115 opens up within an expanded minibuffer area. Each menu item comes
116 with short documentation strings and highlighted one-key
117 completions. So discovering even seldom used keys is simply a
118 matter of ~C-o~ in the minibuffer while in the midst of the Ivy
119 interaction. This discoverability minimizes exiting Ivy interface
120 for documentation look-ups.
121
122 * Installation
123 :PROPERTIES:
124 :CUSTOM_ID: installation
125 :END:
126
127 Install Ivy automatically through Emacs's package manager, or manually
128 from Ivy's development repository.
129
130 Ivy should run fine on a typical Emacs bundled in your OS's package
131 manager, the oldest of which is Emacs 24.3.1. However, the faces
132 display will work much better for Emacs 24.5.1, which is the latest
133 stable version.
134
135 ** Installing from Emacs Package Manager
136 :PROPERTIES:
137 :CUSTOM_ID: installing-from-emacs-package-manager
138 :END:
139
140 ~M-x~ =package-install= ~RET~ =swiper= ~RET~
141
142 Ivy is installed as part of =swiper= package. =swiper= is available
143 from two different package archives, GNU ELPA and MELPA. For the
144 latest stable version, use the GNU ELPA archives using the above M-x
145 command.
146
147 For current hourly builds, use the MELPA archives. See the code below
148 for adding MELPA to the list of package archives:
149
150 #+begin_src elisp
151 (require 'package)
152 (add-to-list 'package-archives
153 '("melpa" . "http://melpa.org/packages/"))
154 #+end_src
155
156 After this do ~M-x~ =package-refresh-contents= ~RET~, followed by
157 ~M-x~ =package-install= ~RET~ =swiper= ~RET~.
158
159 For package manager details, see [[info:emacs#Packages]].
160
161 ** Installing from the Git repository
162 :PROPERTIES:
163 :CUSTOM_ID: installing-from-the-git-repository
164 :END:
165
166 - Why install from Git? ::
167
168 - No need to wait for MELPA's hourly builds
169 - Easy to revert to previous versions
170 - Contribute to Ivy's development; send patches; pull requests
171
172
173 - Configuration steps ::
174 First clone the Swiper repository:
175
176 #+begin_src sh
177 cd ~/git && git clone https://github.com/abo-abo/swiper
178 cd swiper && make compile
179 #+end_src
180
181 Then add this to Emacs init:
182
183 #+begin_src elisp
184 (add-to-list 'load-path "~/git/swiper/")
185 (require 'ivy)
186 #+end_src
187
188 To update the code:
189
190 #+begin_src sh
191 git pull
192 make
193 #+end_src
194
195 * Getting started
196 :PROPERTIES:
197 :CUSTOM_ID: getting-started
198 :END:
199 First, enable Ivy completion everywhere:
200
201 #+begin_src elisp
202 (ivy-mode 1)
203 #+end_src
204
205 Note: =ivy-mode= can be toggled on and off with ~M-x~ =ivy-mode=.
206 ** Basic customization
207 :PROPERTIES:
208 :CUSTOM_ID: basic-customization
209 :END:
210 Here are some basic settings particularly useful for new Ivy users:
211
212 #+begin_src elisp
213 (setq ivy-use-virtual-buffers t)
214 (setq ivy-height 10)
215 (setq ivy-display-style 'fancy)
216 (setq ivy-count-format "(%d/%d) ")
217 #+end_src
218
219 For additional customizations, refer to =M-x describe-variable=
220 documentation.
221
222 * Key bindings
223 :PROPERTIES:
224 :CUSTOM_ID: key-bindings
225 :END:
226 ** Global key bindings
227 :PROPERTIES:
228 :CUSTOM_ID: global-key-bindings
229 :END:
230 The recommended key bindings are:
231
232 - Ivy-based interface to standard commands ::
233
234 #+begin_src elisp
235 (global-set-key (kbd "C-s") 'swiper)
236 (global-set-key (kbd "M-x") 'counsel-M-x)
237 (global-set-key (kbd "C-x C-f") 'counsel-find-file)
238 (global-set-key (kbd "<f1> f") 'counsel-describe-function)
239 (global-set-key (kbd "<f1> v") 'counsel-describe-variable)
240 (global-set-key (kbd "<f1> l") 'counsel-load-library)
241 (global-set-key (kbd "<f2> i") 'counsel-info-lookup-symbol)
242 (global-set-key (kbd "<f2> u") 'counsel-unicode-char)
243 #+end_src
244
245 - Ivy-based interface to shell and system tools ::
246
247 #+begin_src elisp
248 (global-set-key (kbd "C-c g") 'counsel-git)
249 (global-set-key (kbd "C-c j") 'counsel-git-grep)
250 (global-set-key (kbd "C-c k") 'counsel-ag)
251 (global-set-key (kbd "C-x l") 'counsel-locate)
252 (global-set-key (kbd "C-S-o") 'counsel-rhythmbox)
253 #+end_src
254
255 - Ivy-resume and other commands ::
256
257 =ivy-resume= resumes the last Ivy-based completion.
258
259 #+begin_src elisp
260 (global-set-key (kbd "C-c C-r") 'ivy-resume)
261 #+end_src
262
263 ** Minibuffer key bindings
264 :PROPERTIES:
265 :CUSTOM_ID: minibuffer-key-bindings
266 :END:
267
268 Ivy includes several minibuffer bindings, which are defined in the
269 =ivy-minibuffer-map= keymap variable. The most frequently used ones
270 are described here.
271
272 =swiper= or =counsel-M-x= add more through the =keymap= argument to
273 =ivy-read=. These keys, also active in the minibuffer, are described
274 under their respective commands.
275
276 An important idea behind =ivy-minibuffer-map=, unlike
277 e.g. =isearch-mode-map= or Ido keymap is that the minibuffer is a
278 fully capable editing area: bindings like ~C-a~, ~C-f~, ~M-d~,
279 ~M-DEL~, ~M-b~, ~M-w~, ~C-k~, ~C-y~ all work as if you were in a
280 =fundamental-mode= buffer.
281
282 *** Key bindings for navigation
283 :PROPERTIES:
284 :CUSTOM_ID: key-bindings-for-navigation
285 :END:
286
287 - ~C-n~ (=ivy-next-line=) selects the next candidate
288 - ~C-p~ (=ivy-previous-line=) selects the previous candidate
289 - ~M-<~ (=ivy-beginning-of-buffer=) selects the first candidate
290 - ~M->~ (=ivy-end-of-buffer=) selects the last candidate
291 - ~C-v~ (=ivy-scroll-up-command=) scrolls up by =ivy-height= lines
292 - ~M-v~ (=ivy-scroll-down-command=) scrolls down by =ivy-height= lines
293
294
295 - User Option =ivy-wrap= ::
296 This user option allows to get the wrap-around behavior for ~C-n~
297 and ~C-p~. When set to =t=, =ivy-next-line= and
298 =ivy-previous-line= will cycle past the last and the first
299 candidates respectively.
300
301 This behavior is off by default.
302
303 - User Option =ivy-height= ::
304 Use this variable to adjust the minibuffer height, and therefore
305 the scroll size for ~C-v~ and ~M-v~.
306
307 *** Key bindings for single selection, action, then exit minibuffer
308 :PROPERTIES:
309 :CUSTOM_ID: key-bindings-for-single-selection-action-then-exit-minibuffer
310 :END:
311
312 Ivy can offer several actions from which to choose which action to
313 run. This "calling an action" operates on the selected candidate. For
314 example, when viewing a list of files, one action could open it for
315 editing, one to view it, another to invoke a special function, and so
316 on. Custom actions can be added to this interface. The precise action
317 to call on the selected candidate can be delayed until after the
318 narrowing is completed. No need to exit the interface if unsure which
319 action to run. This delayed flexibility and customization of actions
320 extends usability of lists in Emacs.
321
322 - ~C-m~ or ~RET~ (=ivy-done=) ::
323 Calls the default action and exits the minibuffer.
324
325 - ~M-o~ (=ivy-dispatching-done=) ::
326 Presents all available valid actions from which to choose. When
327 there is only one action available, there is no difference
328 between ~M-o~ and ~C-m~.
329
330 - ~C-j~ (=ivy-alt-done=) ::
331 When completing file names, selects the current directory
332 candidate and starts a new completion session there. Otherwise,
333 it's the same as =ivy-done=.
334
335 - ~TAB~ (=ivy-partial-or-done=) ::
336 Attempts partial completion, extending current input as much as
337 possible. ~TAB TAB~ is the same as ~C-j~.
338
339 Example ERT test:
340
341 #+begin_src elisp
342 (should
343 (equal (ivy-with
344 '(progn
345 (ivy-read "Test: " '("can do" "can't, sorry" "other"))
346 ivy-text)
347 "c <tab>")
348 "can"))
349 #+end_src
350
351 - ~C-M-j~ (=ivy-immediate-done=) ::
352 Exits with /the current input/ instead of /the current candidate/
353 (like other commands).
354
355 This is useful e.g. when you call =find-file= to create a new
356 file, but the desired name matches an existing file. In that
357 case, using ~C-j~ would select that existing file, which isn't
358 what you want - use this command instead.
359
360 - ~C-'~ (=ivy-avy=) ::
361 Uses avy to select one of the candidates on the current candidate
362 page. This can often be faster than multiple ~C-n~ or ~C-p~
363 keystrokes followed by ~C-m~.
364
365 *** Key bindings for multiple selections and actions, keep minibuffer open
366 :PROPERTIES:
367 :CUSTOM_ID: key-bindings-for-multiple-selections-and-actions-keep-minibuffer-open
368 :END:
369
370 For repeatedly applying multiple actions or acting on multiple
371 candidates, Ivy does not close the minibuffer between commands. It
372 keeps the minibuffer open for applying subsequent actions.
373
374 Adding an extra meta key to the normal key chord invokes the special
375 version of the regular commands that enables applying multiple
376 actions.
377
378 - ~C-M-m~ (=ivy-call=) ::
379 Is the non-exiting version of ~C-m~ (=ivy-done=).
380
381 Instead of closing the minibuffer, ~C-M-m~ allows selecting
382 another candidate or another action. For example, ~C-M-m~ on
383 functions list invokes =describe-function=. When combined with
384 ~C-n~, function descriptions can be invoked quickly in
385 succession.
386
387 - ~C-M-o~ (=ivy-dispatching-call=) ::
388 Is the non-exiting version of ~M-o~ (=ivy-dispatching-done=).
389
390 For example, during the =counsel-rhythmbox= completion, press
391 ~C-M-o e~ to en-queue the selected candidate, followed by ~C-n
392 C-m~ to play the next candidate - the current action reverts to
393 the default one after ~C-M-o~.
394
395 - ~C-M-n~ (=ivy-next-line-and-call=) ::
396 Combines ~C-n~ and ~C-M-m~. Applies an action and moves to next
397 line.
398
399 Comes in handy when opening multiple files from
400 =counsel-find-file=, =counsel-git-grep=, =counsel-ag=, or
401 =counsel-locate= lists. Just hold ~C-M-n~ for rapid-fire default
402 action on each successive element of the list.
403
404 - ~C-M-p~ (=ivy-previous-line-and-call=) ::
405 Combines ~C-p~ and ~C-M-m~.
406
407 Similar to the above except it moves through the list in the
408 other direction.
409
410 - =ivy-resume= ::
411 Recalls the state of the completion session just before its last exit.
412
413 Useful after an accidental ~C-m~ (=ivy-done=).
414
415 *** Key bindings that alter the minibuffer input
416 :PROPERTIES:
417 :CUSTOM_ID: key-bindings-that-alter-the-minibuffer-input
418 :END:
419
420 - ~M-n~ (=ivy-next-history-element=) ::
421 Cycles forward through the Ivy command history.
422
423 Ivy updates an internal history list after each action. When this
424 history list is empty, ~M-n~ inserts symbol (or URL) at point
425 into the minibuffer.
426
427 - ~M-p~ (=ivy-previous-history-element=) ::
428 Cycles forward through the Ivy command history.
429
430 - ~M-i~ (=ivy-insert-current=) ::
431 Inserts the current candidate into the minibuffer.
432
433 Useful for copying and renaming files, for example: ~M-i~ to
434 insert the original file name string, edit it, and then ~C-m~ to
435 complete the renaming.
436
437 - ~M-j~ (=ivy-yank-word=) ::
438 Inserts the sub-word at point into the minibuffer.
439
440 This is similar to ~C-s C-w~ with =isearch=. Ivy reserves ~C-w~
441 for =kill-region=.
442
443 - ~S-SPC~ (=ivy-restrict-to-matches=) ::
444 Deletes the current input, and resets the candidates list to the
445 currently restricted matches.
446
447 This is how Ivy provides narrowing in successive tiers.
448
449 - ~C-r~ (=ivy-reverse-i-search=) ::
450 Starts a recursive completion session through the command's
451 history.
452
453 This works just like ~C-r~ at the bash command prompt, where the
454 completion candidates are the history items. Upon completion, the
455 selected candidate string is inserted into the minibuffer.
456
457 *** Other key bindings
458 :PROPERTIES:
459 :CUSTOM_ID: other-key-bindings
460 :END:
461
462 - ~M-w~ (=ivy-kill-ring-save=) ::
463 Copies selected candidates to the kill ring.
464
465 When the region is active, copies active region instead.
466
467 *** Hydra in the minibuffer
468 :PROPERTIES:
469 :CUSTOM_ID: hydra-in-the-minibuffer
470 :END:
471
472 - ~C-o~ (=hydra-ivy/body=) ::
473 Invokes the hydra menu with short key bindings.
474
475 Minibuffer editing is disabled when Hydra is active. Instead, you get
476 short aliases for the common commands:
477
478 | Short | Normal | Command name |
479 |-------+-----------+---------------------------|
480 | ~o~ | ~C-g~ | =keyboard-escape-quit= |
481 | ~j~ | ~C-n~ | =ivy-next-line= |
482 | ~k~ | ~C-p~ | =ivy-previous-line= |
483 | ~h~ | ~M-<~ | =ivy-beginning-of-buffer= |
484 | ~l~ | ~M->~ | =ivy-end-of-buffer= |
485 | ~d~ | ~C-m~ | =ivy-done= |
486 | ~f~ | ~C-j~ | =ivy-alt-done= |
487 | ~g~ | ~C-M-m~ | =ivy-call= |
488 | ~u~ | ~C-c C-o~ | =ivy-occur= |
489
490 Hydra reduces key strokes, for example: ~C-n C-n C-n C-n~ is ~C-o
491 jjjj~ in Hydra.
492
493 Additionally, you get access to the folowing commands that are
494 normally not bound:
495
496 - ~c~ (=ivy-toggle-calling=) ::
497 Toggle calling the action after each candidate change. This
498 effectively modifies ~j~ to ~jg~, ~k~ to ~kg~ etc.
499
500 - ~m~ (=ivy-toggle-fuzzy=) ::
501 Toggle the current regexp matcher.
502
503 - ~>~ (=ivy-minibuffer-grow=) ::
504 Increase =ivy-height= for the current minibuffer.
505
506 - ~<~ (=ivy-minibuffer-shrink=) ::
507 Decrease =ivy-height= for the current minibuffer.
508
509 - ~w~ (=ivy-prev-action=) ::
510 Select the previous action.
511
512 - ~s~ (=ivy-next-action=) ::
513 Select the next action.
514
515 - ~a~ (=ivy-read-action=) ::
516 Use a menu to select an action.
517
518 - ~C~ (=ivy-toggle-case-fold=) ::
519 Toggle case folding (matching both upper and lower case
520 characters with lower case input).
521
522 *** Saving the current completion session to a buffer
523 :PROPERTIES:
524 :CUSTOM_ID: saving-the-current-completion-session-to-a-buffer
525 :END:
526
527 - ~C-c C-o~ (=ivy-occur=) ::
528 Saves the current candidates to a new buffer and exits
529 completion.
530
531 The new buffer is read-only and has a few useful bindings defined.
532
533 - ~RET~ or ~j~ (=ivy-occur-press=) ::
534 Call the current action on the selected candidate.
535
536 - ~mouse-1~ (=ivy-occur-click=) ::
537 Call the current action on the selected candidate.
538
539 - ~j~ (=next-line=) ::
540 Move to next line.
541
542 - ~k~ (=previous-line=) ::
543 Move to previous line.
544
545 - ~a~ (=ivy-occur-read-action=) ::
546 Read an action and make it current for this buffer.
547
548 - ~o~ (=ivy-occur-dispatch=) ::
549 Read an action and call it on the selected candidate.
550
551 - ~q~ (=quit-window=) ::
552 Bury the current buffer.
553
554
555 Ivy has no limit on the number of active buffers like these.
556
557 Ivy takes care of making these buffer names unique. It applies
558 descriptive names, for example: =*ivy-occur counsel-describe-variable
559 "function$*=.
560
561 * Completion styles
562 :PROPERTIES:
563 :CUSTOM_ID: completion-styles
564 :END:
565
566 Ivy's completion functions rely on a regex builder - a function that
567 transforms a string input to a string regex. All current candidates
568 simply have to match this regex. Each collection can be assigned its
569 own regex builder by customizing =ivy-re-builders-alist=.
570
571 The keys of this alist are collection names, and the values are one of:
572 - =ivy--regex=
573 - =ivy--regex-plus=
574 - =ivy--regex-ignore-order=
575 - =ivy--regex-fuzzy=
576 - =regexp-quote=
577
578 There's also a catch-all key =t= that applies to all collections that
579 don't have their own key.
580
581 The default is:
582
583 #+begin_src elisp
584 (setq ivy-re-builders-alist
585 '((t . ivy--regex-plus)))
586 #+end_src
587
588 For example, here is how to assign a custom regex builder to file name
589 completion:
590
591 #+begin_src elisp
592 (setq ivy-re-builders-alist
593 '((read-file-name-internal . ivy--regex-fuzzy)
594 (t . ivy--regex-plus)))
595 #+end_src
596
597 Here, =read-file-name-internal= is a function passed as the second
598 argument to =completing-read= when completing file names.
599
600 The regex builder resolution is a follows, in order of priority:
601 1. =re-builder= argument passed to =ivy-read=.
602 2. =collection= argument passed to =ivy-read= is a function and has an
603 entry on =ivy-re-builders-alist=.
604 3. =caller= argument passed to =ivy-read= has an entry on
605 =ivy-re-builders-alist=.
606 4. =this-command= has an entry on =ivy-re-builders-alist=.
607 5. =t= has an entry on =ivy-re-builders-alist=.
608 6. =ivy--regex=.
609
610 ** ivy--regex-plus
611 :PROPERTIES:
612 :CUSTOM_ID: ivy--regex-plus
613 :END:
614
615 =ivy--regex-plus= is Ivy's default completion method.
616
617 =ivy--regex-plus= matches by splitting the input by spaces and
618 rebuilding it into a regex.
619
620 As the search string is typed in Ivy's minibuffer, it is transformed
621 into proper regex syntax. If the string is ="for example"=, it is
622 transformed into
623
624 #+begin_src elisp
625 "\\(for\\).*\\(example\\)"
626 #+end_src
627
628 which in regex terminology matches ="for"= followed by a wild card and
629 then ="example"=. Note how Ivy uses the space character to build wild
630 cards. For literal white space matching in Ivy, use an extra space: to
631 match one space type two spaces, to match two spaces type three
632 spaces, and so on.
633
634 As Ivy transforms typed characters into regex strings, it provides an
635 intuitive feedback through font highlights.
636
637 Ivy supports regexp negation with ="!"=.
638 For example, ="define key ! ivy quit"= first selects everything
639 matching ="define.*key"=, then removes everything matching ="ivy"=,
640 and finally removes everything matching ="quit"=. What remains is the
641 final result set of the negation regexp.
642
643 Since Ivy treats minibuffer input as a regexp, the standard regexp
644 identifiers work: ="^"=, ="$"=, ="\b"= or ="[a-z]"=. The exceptions
645 are spaces, which translate to =".*"=, and ="!"= that signal the
646 beginning of a negation group.
647
648 ** ivy--regex-ignore-order
649 :PROPERTIES:
650 :CUSTOM_ID: ivy--regex-ignore-order
651 :END:
652
653 =ivy--regex-ignore-order= ignores the order of regexp tokens when
654 searching for matching candidates. For instance, the input
655 ="for example"= will match ="example test for"=.
656
657 ** ivy--regex-fuzzy
658 :PROPERTIES:
659 :CUSTOM_ID: ivy--regex-fuzzy
660 :END:
661
662 =ivy--regex-fuzzy= splits each character with a wild card. Searching
663 for ="for"= returns all ="f.*o.*r"= matches, resulting in a large
664 number of hits. Yet some searches need these extra hits. Ivy sorts
665 such large lists using =flx= package's scoring mechanism, if it's
666 installed.
667
668 In case =ivy--regex-fuzzy= isn't your current regexp builder, you
669 toggle it during completion with ~C-o m~.
670
671 * Customization
672 :PROPERTIES:
673 :CUSTOM_ID: customization
674 :END:
675 ** Faces
676 :PROPERTIES:
677 :CUSTOM_ID: faces
678 :END:
679 - =ivy-current-match= ::
680 Highlights the currently selected candidate.
681 - =ivy-minibuffer-match-face-1= ::
682 Highlights the background of the match.
683 - =ivy-minibuffer-match-face-2= ::
684 Highlights the first (modulo 3) matched group.
685 - =ivy-minibuffer-match-face-3= ::
686 Highlights the second (modulo 3) matched group.
687 - =ivy-minibuffer-match-face-4= ::
688 Highlights the third (modulo 3) matched group.
689 - =ivy-confirm-face= ::
690 Highlights the "(confirm)" part of the prompt.
691
692 Used in conjunction with the built-in
693 =confirm-nonexistent-file-or-buffer= defcustom. When you set
694 this variable to =t=, you'll have to confirm non-existent files
695 and buffer with another ~RET~ in =ivy-mode=.
696
697 This face will be used to highlight the confirmation prompt.
698
699 For example, use this setting:
700
701 #+begin_src elisp
702 (setq confirm-nonexistent-file-or-buffer t)
703 #+end_src
704
705 Then call =find-file=, enter "eldorado" and press ~RET~ - the
706 prompt will be appended with "(confirm)". Press ~RET~ once more
707 to confirm, or any key to continue the completion.
708 - =ivy-match-required-face= ::
709 Highlights the "(match required)" part of the prompt.
710
711 Sometimes, the Emacs functions that call completion specify to it
712 that a match is required, i.e. you can't just type in some random
713 stuff - you have to select one of the candidates given to you.
714 In that case, =ivy-mode= will appropriately change the prompt.
715
716 For example, call =describe-variable=, enter "waldo" and press
717 ~RET~ - the prompt will be appended with "(match required)".
718 Press any key and the prompt warning will disappear.
719 - =ivy-subdir= ::
720 Highlights directories when completing file names.
721 - =ivy-remote= ::
722 Highlights remote files when completing file names.
723 - =ivy-virtual= ::
724 Highlights virtual buffers when completing buffer names.
725
726 Virtual buffers correspond to your bookmarks and the =recentf=
727 list.
728
729 Enable the virtual buffers like this:
730
731 #+begin_src elisp
732 (setq ivy-use-virtual-buffers t)
733 #+end_src
734 ** Defcustoms
735 :PROPERTIES:
736 :CUSTOM_ID: defcustoms
737 :END:
738 - User Option =ivy-count-format= ::
739 A string that describes how to show the number of candidates and
740 possibly the current candidate in the prompt.
741
742 By default, the number of matching candidates will be shown as an
743 integer with padding on the right.
744
745 To disable showing the number of candidates:
746
747 #+begin_src elisp
748 (setq ivy-count-format "")
749 #+end_src
750
751 To show the current candidate, in addition to the number of candidates:
752
753 #+begin_src elisp
754 (setq ivy-count-format "(%d/%d) ")
755 #+end_src
756
757 This variable uses =format=-style switches, see the documentation
758 of =format= for more info.
759
760 - User Option =ivy-display-style= ::
761 Decides how to highlight the candidates in the minibuffer.
762
763 The default setting is ='fancy= and it's available only for Emacs
764 versions 24.5 or newer.
765
766 Set this to =nil= to get a more plain minibuffer.
767
768 - User Option =ivy-on-del-error-function= ::
769 Decides what to do when ~DEL~ (=ivy-backward-delete-char=)
770 throws.
771
772 The default behavior is to quit the completion - this is handy if
773 you invoke the completion by mistake.
774
775 ** Actions
776 :PROPERTIES:
777 :CUSTOM_ID: actions
778 :END:
779 *** What are actions?
780 :PROPERTIES:
781 :CUSTOM_ID: what-are-actions
782 :END:
783 An action is a function of a single argument that gets called after
784 you select a candidate during completion. The selected candidate is
785 passed to this function as a string argument.
786
787 - Window context when calling an action ::
788 Currently, the action is executed in the minibuffer window
789 context. This means e.g. that if you call =insert= the text will
790 be inserted into the minibuffer.
791
792 If you want to execute the action in the initial window from
793 which the completion started, use the =with-ivy-window= wrapper
794 macro.
795
796 #+begin_src elisp
797 (defun ivy-insert-action (x)
798 (with-ivy-window
799 (insert x)))
800 #+end_src
801
802 *** How can different actions be called?
803 :PROPERTIES:
804 :CUSTOM_ID: how-can-different-actions-be-called
805 :END:
806 - ~C-m~ (=ivy-done=) calls the current/default action.
807 - ~M-o~ (=ivy-dispatching-done=) selects among all actions, calls it
808 and exits.
809 - ~C-M-o~ (=ivy-dispatching-call=) selects among all actions, calls it
810 and doesn't exit.
811
812 *** How can the action list be modified?
813 :PROPERTIES:
814 :CUSTOM_ID: how-can-the-action-list-be-modified
815 :END:
816 Currently, you can append any amount of your own actions to the
817 default list of actions. This can be done either for a specific
818 command, or for all commands at once.
819
820 Usually, the command has only one default action. The convention is to
821 use single letters when selecting a command, and the letter ~o~ is
822 designated for the default command. This way, ~M-o o~ should be always
823 equivalent to ~C-m~.
824
825 *** Example - add two actions to each command
826 :PROPERTIES:
827 :CUSTOM_ID: example---add-two-actions-to-each-command
828 :END:
829 The first action inserts the current candidate into the Ivy window -
830 the window from which =ivy-read= was called.
831
832 The second action copies the current candidate to the kill ring.
833
834 #+begin_src elisp
835 (defun ivy-yank-action (x)
836 (kill-new x))
837
838 (defun ivy-copy-to-buffer-action (x)
839 (with-ivy-window
840 (insert x)))
841
842 (ivy-set-actions
843 t
844 '(("i" ivy-copy-to-buffer-action "insert")
845 ("y" ivy-yank-action "yank")))
846 #+end_src
847
848 Now in any completion session you can access =ivy-yank-action= with
849 ~M-o y~ and =ivy-copy-to-buffer-action= with ~M-o i~.
850
851 **** How to undo adding the two actions
852 :PROPERTIES:
853 :CUSTOM_ID: how-to-undo-adding-the-two-actions
854 :END:
855 =ivy-set-actions= simply modifies the internal dict with new data, so
856 you can set the extra actions list to =nil= by assigning =nil= value
857 to the =t= key:
858
859 #+begin_src elisp
860 (ivy-set-actions t nil)
861 #+end_src
862
863 **** How to add actions to a specific command
864 :PROPERTIES:
865 :CUSTOM_ID: how-to-add-actions-to-a-specific-command
866 :END:
867 Use the command name as the key:
868
869 #+begin_src elisp
870 (ivy-set-actions
871 'swiper
872 '(("i" ivy-copy-to-buffer-action "insert")
873 ("y" ivy-yank-action "yank")))
874 #+end_src
875
876 *** Example - define a new command with several actions
877 :PROPERTIES:
878 :CUSTOM_ID: example---define-a-new-command-with-several-actions
879 :END:
880 #+begin_src elisp
881 (defun my-action-1 (x)
882 (message "action-1: %s" x))
883
884 (defun my-action-2 (x)
885 (message "action-2: %s" x))
886
887 (defun my-action-3 (x)
888 (message "action-3: %s" x))
889
890 (defun my-command-with-3-actions ()
891 (interactive)
892 (ivy-read "test: " '("foo" "bar" "baz")
893 :action '(1
894 ("o" my-action-1 "action 1")
895 ("j" my-action-2 "action 2")
896 ("k" my-action-3 "action 3"))))
897 #+end_src
898
899 Here, the number determines the index of the default action. For each
900 action, the strings are used to describe it during the selection.
901
902 **** Testing out the above function with =ivy-occur=
903 :PROPERTIES:
904 :CUSTOM_ID: testing-out-the-above-function-with-ivy-occur
905 :END:
906 To examine each action with each candidate in a key-efficient way, try:
907
908 - Call =my-command-with-3-actions=.
909 - Press ~C-c C-o~ to close the completion and move to an ivy-occur buffer.
910 - Press ~kkk~ to move to the first candidate, since you're likely at the end of the buffer.
911 - Press ~oo~ to call the first action.
912 - Press ~oj~ and ~ok~ to call the second and the third actions.
913 - Press ~j~ to move to the next candidate
914 - ...
915 ** Packages
916 :PROPERTIES:
917 :CUSTOM_ID: packages
918 :END:
919 - =org-mode= ::
920 With the most recent version, =org-mode= will obey
921 =completing-read-function= (which =ivy-mode= sets), so it should
922 work by default. If you try it for refiling to headings with
923 similar names, you'll really notice how much better =ivy-mode= is
924 at it.
925 - =magit= ::
926 This setting is needed to use ivy completion:
927
928 #+begin_src elisp
929 (setq magit-completing-read-function 'ivy-completing-read)
930 #+end_src
931 - =find-file-in-project= ::
932 Will use ivy by default if it's available.
933 - =projectile= ::
934 This setting is needed to use ivy completion:
935
936 #+begin_src elisp
937 (setq projectile-completion-system 'ivy)
938 #+end_src
939 - =helm-make= ::
940 This setting is needed to use ivy completion:
941
942 #+begin_src elisp
943 (setq helm-make-completion-method 'ivy)
944 #+end_src
945
946 * Commands
947 :PROPERTIES:
948 :CUSTOM_ID: commands
949 :END:
950 ** File Name Completion
951 :PROPERTIES:
952 :CUSTOM_ID: file-name-completion
953 :END:
954 Since file name completion is so essential, ivy has a few extra
955 bindings that work here.
956
957 - ~C-j~ (=ivy-alt-done=) ::
958 Use on a directory to restart the completion from that
959 directory. Use it on a file or =./= to exit the completion with
960 the selected candidate.
961 - ~DEL~ (=ivy-backward-delete-char=) ::
962 When completing file names, and the current input is empty,
963 restart the completion in the parent directory.
964 - ~//~ (=self-insert-command=) ::
965 Switch to the root directory.
966 - ~~~ (=self-insert-command=) ::
967 Switch to the home directory.
968 - ~/~ (=self-insert-command=) ::
969 If the current input is precisely an existing directory, switch
970 the completion to that directory.
971 - ~M-q~ (=ivy-toggle-regexp-quote=) ::
972 Toggle between your input being a regexp and not.
973
974 Since file names tend to include =.=, which matches any char in
975 regexp mode, you might want to switch to matching literally.
976
977 - User Option =ivy-extra-directories= ::
978 Decide if you want to see =../= and =./= during file name
979 completion.
980
981 You might want to remove =../=, since selecting it is the same as
982 ~DEL~. On the other hand, having it around makes it possible to
983 navigate anywhere with only ~C-n~, ~C-p~ and ~C-j~.
984
985 Similarly, =./= can be removed as well.
986
987 - Using TRAMP ::
988 Completion for TRAMP is supported in a peculiar way. From any
989 directory, with the empty input, inputting =/ssh:= and pressing
990 ~C-j~ (or ~RET~ which is the same thing) will give you a
991 completion for host and user names.
992
993 You can also input =/ssh:user@= to get domain completion with
994 user name already selected.
995
996 Described above is a recommended and simple method of
997 interaction. If you find it lacking, you can still use ~C-i~,
998 which does largely the same as the default completion does.
999
1000 - History ::
1001 The history works with ~M-p~, ~M-n~, and ~C-r~, as in all other
1002 completion sessions. A custom history code was implemented for
1003 file name completion. This code will cycle you through all
1004 previous files that you opened, including the input with which
1005 the file was opened. It also works well with TRAMP.
1006
1007 ** Buffer Name Completion
1008 :PROPERTIES:
1009 :CUSTOM_ID: buffer-name-completion
1010 :END:
1011 - User Option =ivy-use-virtual-buffers= ::
1012 When non-nil, add =recentf-mode= and bookmarks to =ivy-switch-buffer=.
1013
1014 If you add this to your setup:
1015
1016 #+begin_src elisp
1017 (setq ivy-use-virtual-buffers t)
1018 #+end_src
1019 when using =ivy-switch-buffer= additional buffers will be
1020 appended to your live buffer list. These buffers will be
1021 highlighted with the =ivy-virtual= face, and selecting them will
1022 open the corresponding file.
1023 ** Counsel commands
1024 :PROPERTIES:
1025 :CUSTOM_ID: counsel-commands
1026 :END:
1027 The main advantage of using =counsel-= functions over their basic
1028 equivalents with =ivy-mode= enabled are the following:
1029
1030 1. You can use everything related to multi-actions and non-exiting actions.
1031 2. You can use =ivy-resume= to resume your last completion session.
1032 3. You can customize them further with =ivy-set-actions=, =ivy-re-builders-alist=.
1033 4. You can customize their individual keymaps, like
1034 =counsel-describe-map=, =counsel-git-grep-map=, or
1035 =counsel-find-file-map=, instead of just customizing
1036 =ivy-minibuffer-map= that applies to all completion sessions.
1037 * API
1038 :PROPERTIES:
1039 :CUSTOM_ID: api
1040 :END:
1041 The main (and only) entry point is =ivy-read= function. It has only
1042 two required arguments and many optional arguments that you can pass
1043 by key. Although the =:action= argument is optional, it's very
1044 recommended that you use it, otherwise the extra features (as compared
1045 to the default completion) like multi-actions, non-exiting actions,
1046 =ivy-occur= and =ivy-resume= will not be possible.
1047
1048 ** Required arguments for =ivy-read=
1049 :PROPERTIES:
1050 :CUSTOM_ID: required-arguments-for-ivy-read
1051 :END:
1052 - =prompt= ::
1053 A format string normally ending in a colon and a space.
1054
1055 =%d= anywhere in the string is replaced by the current number of
1056 matching candidates. To use a literal =%= character, escape it as
1057 =%%=. See also =ivy-count-format=.
1058
1059 - =collection= ::
1060 Either a list of strings, a function, an alist or a hash table.
1061
1062 In case it's a function, it has to be compatible with
1063 =all-completions=.
1064
1065 ** Optional arguments for =ivy-read=
1066 :PROPERTIES:
1067 :CUSTOM_ID: optional-arguments-for-ivy-read
1068 :END:
1069 - =predicate= ::
1070 A function to filter the initial collection with, compatible with =all-completions=.
1071 - =require-match= ::
1072 When non-nil, don't let the user exit with a custom input - it
1073 must match one of the candidates.
1074 - =initial-input= ::
1075 A string to be initially inserted into the minibuffer. This
1076 argument is included for compatibility with
1077 =completing-read=. Consider using the =preselect= argument
1078 instead - it's often superior.
1079 - =history= ::
1080 A symbol name to store the history. See =completing-read=.
1081 - =preselect= ::
1082 When it's a string, make the first candidate matching this string
1083 initially selected.
1084
1085 When it's an integer, make the candidate with that index
1086 initially selected.
1087
1088 Every time the input becomes empty, the item corresponding to to
1089 =preselect= is selected.
1090 - =keymap= ::
1091 A keymap to be composed with =ivy-minibuffer-map=. This keymap
1092 has priority over =ivy-minibuffer-map= and can be modified at any
1093 later stage.
1094 - =update-fn= ::
1095 A function to call each time the current candidate is changed.
1096 This function takes no arguments and is called in the
1097 minibuffer's =post-command-hook=. See =swiper= for an example
1098 usage.
1099 - =sort= ::
1100 When non-nil, use =ivy-sort-functions-alist= to sort the given
1101 collection. The collection will not be sorted when it's larger
1102 than =ivy-sort-max-size=.
1103 - =action= ::
1104 A function to call after a result is selected. Takes a single
1105 string argument.
1106 - =unwind= ::
1107 A function with no arguments to call before exiting
1108 completion. This function is called even if the completion is
1109 interrupted with e.g. ~C-g~. See =swiper= for an example usage.
1110 - =re-builder= ::
1111 A function that takes a string and returns a corresponding regex.
1112 See the section on completion styles.
1113 - =matcher= ::
1114 A function that takes a regex and a list of strings and returns a
1115 list of strings that "match" the regex. Normally a
1116 straightforward function is used. Use this argument to really
1117 fine-tune the matching process. See =counsel-find-file= for an
1118 example usage.
1119 - =dynamic-collection= ::
1120 When non-nil, =collection= will be used to dynamically generate
1121 the candidates each time the input changes, instead of being used
1122 once statically with =all-completions= to generate a list of
1123 strings. See =counsel-locate= for an example usage.
1124 - =caller= ::
1125 A symbol to uniquely identify the function that called
1126 =ivy-read=. This is useful in all kinds of customization
1127 scenarios.
1128 ** Example - =counsel-describe-function=
1129 :PROPERTIES:
1130 :CUSTOM_ID: example---counsel-describe-function
1131 :END:
1132 This is a typical example of a function with a non-async collection:
1133 all the strings in the collection are known before the user does any
1134 input.
1135
1136 Note that only the first two arguments (and the =action=) are really
1137 important - the rest is just fine-tuning and could be omitted.
1138
1139 The =action= argument could also be omitted - but then =ivy-read=
1140 would no nothing except returning the string result, which you could
1141 later use yourself. However, it's recommended that you use the
1142 =action= argument.
1143
1144 #+begin_src elisp
1145 (defun counsel-describe-function ()
1146 "Forward to `describe-function'."
1147 (interactive)
1148 (ivy-read "Describe function: "
1149 (let (cands)
1150 (mapatoms
1151 (lambda (x)
1152 (when (fboundp x)
1153 (push (symbol-name x) cands))))
1154 cands)
1155 :keymap counsel-describe-map
1156 :preselect (counsel-symbol-at-point)
1157 :history 'counsel-describe-symbol-history
1158 :require-match t
1159 :sort t
1160 :action (lambda (x)
1161 (describe-function
1162 (intern x)))
1163 :caller 'counsel-describe-function))
1164 #+end_src
1165
1166 Here are the interesting features of the above function, in the order that they appear:
1167
1168 - The =prompt= argument is a simple string ending in ": ".
1169 - The =collection= argument evaluates to a (large) list of strings.
1170 - The =keymap= argument allows for a custom keymap to supplement =ivy-minibuffer-map=.
1171 - The =history= argument ensures that the command has its own history,
1172 and doesn't need to share the common history =ivy-history= that all
1173 commands without their own history share.
1174 - The =require-match= is set to =t=, since it doesn't make sense to
1175 call =describe-function= on an un-interned symbol.
1176 - The =sort= argument is set to =t=, since it's usually useful to have
1177 functions with similar names be close to each other in the candidate
1178 list. However, after loading many packages the collection often
1179 exceeds the default value of =ivy-sort-max-size= (30000). The user
1180 can customize this variable to decide which is more important: the
1181 sorting or the completion start-up time.
1182 - The =action= argument calls =describe-function= on the interned
1183 selected candidate.
1184 - The =caller= argument identifies this completion session. This is
1185 important, since with the collection being a list of strings and not
1186 a function name, the only other way for =ivy-read= to identify
1187 "who's calling" and to apply the appropriate customizations is to
1188 examine =this-command=. But =this-command= would be modified if
1189 another command called =counsel-describe-function=.
1190 ** Example - =counsel-locate=
1191 :PROPERTIES:
1192 :CUSTOM_ID: example---counsel-locate
1193 :END:
1194 This is a typical example of a function with an async collection.
1195 Since we can't pre-compute all the collection items valid for an empty
1196 input and store them in the memory, the collection function is called
1197 each time the user updates the input. However, while the returned
1198 list of strings is used immediately (usually it's something like
1199 ='("please wait...")=), it's expected of the collection function to
1200 make a call to =start-process= and update the minibuffer text at some
1201 point when the process is finished.
1202
1203 Async collections are a good fit for long-running shell commands, like
1204 =locate=. As soon as there is enough input, a new process is started
1205 and the old process is killed (since the old input is no longer
1206 relevant). The user can either type more or wait for the already
1207 running process to finish and update the minibuffer.
1208
1209 #+begin_src elisp
1210 (defun counsel-locate-function (str)
1211 (if (< (length str) 3)
1212 (counsel-more-chars 3)
1213 (counsel--async-command
1214 (format "locate %s '%s'"
1215 (mapconcat #'identity counsel-locate-options " ")
1216 (counsel-unquote-regex-parens
1217 (ivy--regex str))))
1218 '("" "working...")))
1219
1220 ;;;###autoload
1221 (defun counsel-locate (&optional initial-input)
1222 "Call the \"locate\" shell command.
1223 INITIAL-INPUT can be given as the initial minibuffer input."
1224 (interactive)
1225 (ivy-read "Locate: " #'counsel-locate-function
1226 :initial-input initial-input
1227 :dynamic-collection t
1228 :history 'counsel-locate-history
1229 :action (lambda (file)
1230 (with-ivy-window
1231 (when file
1232 (find-file file))))
1233 :unwind #'counsel-delete-process
1234 :caller 'counsel-locate))
1235 #+end_src
1236
1237 Here are the interesting features of the above functions, in the order
1238 that they appear:
1239
1240 - =counsel-locate-function= takes a string argument and returns a list
1241 of strings. Note that it's not compatible with =all-completions=,
1242 but since we're not using that here, might as well use one argument
1243 instead of three.
1244 - =counsel-more-chars= is a simple function that returns e.g.
1245 ='("2 chars more")= asking the user for more input.
1246 - =counsel--async-command= is a very easy API simplification that
1247 takes a single string argument suitable for
1248 =shell-command-to-string=. So you could prototype your function as
1249 non-async using =shell-command-to-string= and =split-string= to
1250 produce a collection, then decide that you want async and simply swap in
1251 =counsel--async-command=.
1252 - =counsel-locate= is an interactive function with optional =initial-input=.
1253 - =#'counsel-locate-function= is passed as the =collection= argument.
1254 - =dynamic-collection= argument is set to t, since we have an async collection.
1255 - =action= argument uses =with-ivy-window= wrapper, since we want to open the
1256 selected file in the same window from which =counsel-locate= was
1257 called.
1258 - =unwind= argument is set to =#'counsel-delete-process=: when we press ~C-g~
1259 we want to kill the running process created by
1260 =counsel--async-command=.
1261 - =caller= argument identifies this command for easier customization.
1262
1263 * Variable Index
1264 :PROPERTIES:
1265 :INDEX: vr
1266 :CUSTOM_ID: variable-index
1267 :END: