GNU Emacs

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 nth 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 Text 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 Tailoring Search to Your Needs.

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.

When you change the direction of a search, the first command you type will, by default, remain on the same match, and the cursor will move to the other end of the match. To move to another match immediately, customize the variable isearch-repeat-on-direction-change to t.

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.

You can control what happens when there are no more matches by customizing the isearch-wrap-pause user option. If it is t (the default), signal an error. (Repeating the search will wrap around.) If no, issue a ding and wrap immediately after reaching the last match. If no-ding, wrap immediately, but don’t ding. Finally, if nil, never wrap, but just stop at the last match.

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.