15.1.2 Repeating Incremental Search
Suppose you search forward for ‘FOO
’ and find a match, but not the one you expected to find: the ‘FOO
’ you were aiming for occurs later in the buffer. In this event, type another C-s
(isearch-repeat-forward
) to move to the next occurrence of the search string, or C-r
(isearch-repeat-backward
) to move to the previous occurrence. You can repeat these commands any number of times. Alternatively, you can supply a numeric prefix argument of n
to C-s
and C-r
to find the n
th next or previous occurrence. If you overshoot, you can cancel some C-s
commands with DEL
. Similarly, each C-r
(isearch-repeat-backward
) in a backward incremental search repeats the backward search.
If you pause for a little while during incremental search, Emacs highlights all the other possible matches for the search string that are present on the screen. This helps you anticipate where you can get to by typing C-s
or C-r
to repeat the search. The other matches are highlighted differently from the current match, using the customizable face lazy-highlight
(see Faces). If you don’t like this feature, you can disable it by setting isearch-lazy-highlight
to nil
. For other customizations related to highlighting matches, see Search Customizations.
After exiting a search, you can search for the same string again by typing just C-s C-s
. The first C-s
is the key that invokes incremental search, and the second C-s
means to search again for the last search string. Similarly, C-r C-r
searches backward for the last search string. In determining the last search string, it doesn’t matter whether that string was searched for with C-s
or C-r
.
If you are searching forward but you realize you were looking for something before the starting point, type C-r
to switch to a backward search, leaving the search string unchanged. Similarly, C-s
in a backward search switches to a forward search.
If a search is failing and you ask to repeat it by typing another C-s
, it starts again from the beginning of the buffer. Repeating a failing reverse search with C-r
starts again from the end. This is called wrapping around, and ‘Wrapped
’ appears in the search prompt once this has happened. If you keep on going past the original starting point of the search, it changes to ‘Overwrapped
’, which means that you are revisiting matches that you have already seen.
To reuse earlier search strings, use the search ring. The commands M-p
(isearch-ring-retreat
) and M-n
(isearch-ring-advance
) move through the ring to pick a search string to reuse. These commands leave the selected search ring element in the minibuffer, where you can edit it. Type C-s
/C-r
or RET
to accept the string and start searching for it. The number of most recently used search strings saved in the search ring is specified by the variable search-ring-max
, 16 by default.
To edit the current search string in the minibuffer without replacing it with items from the search ring, type M-e
(isearch-edit-string
) or click mouse-1
in the minibuffer. Type RET
, C-s
or C-r
to finish editing the string and search for it. Type C-f
or RIGHT
to add to the search string characters following point from the buffer from which you started the search.