Next: , Previous: , Up: Search   [Contents][Index]

12.1 Incremental Search

An incremental search begins searching as soon as you type the first character of the search string. As you type in the search string, Emacs shows you where the string (as you have typed it so far) is found. When you have typed enough characters to identify the place you want, you can stop. Depending on what you do next, you may or may not need to terminate the search explicitly with a RET.


Incremental search forward (isearch-forward).


Incremental search backward (isearch-backward).

C-s starts an incremental search. C-s reads characters from the keyboard and positions the cursor at the first occurrence of the characters that you have typed. If you type C-s and then F, the cursor moves right after the first ‘F’. Type an O, and see the cursor move to after the first ‘FO’. After another O, the cursor is after the first ‘FOO’ after the place where you started the search. Meanwhile, the search string ‘FOO’ has been echoed in the echo area.

The echo area display ends with three dots when actual searching is going on. When search is waiting for more input, the three dots are removed. (On slow terminals, the three dots are not displayed.)

If you make a mistake in typing the search string, you can erase characters with DEL. Each DEL cancels the last character of the search string. This does not happen until Emacs is ready to read another input character; first it must either find, or fail to find, the character you want to erase. If you do not want to wait for this to happen, use C-g as described below.

When you are satisfied with the place you have reached, you can type RET (or C-m), which stops searching, leaving the cursor where the search brought it. Any command not specially meaningful in searches also stops the search and is then executed. Thus, typing C-a exits the search and then moves to the beginning of the line. RET is necessary only if the next command you want to type is a printing character, DEL, ESC, or another control character that is special within searches (C-q, C-w, C-r, C-s, or C-y).

Sometimes you search for ‘FOO’ and find it, but were actually looking for a different occurrence of it. To move to the next occurrence of the search string, type another C-s. Do this as often as necessary. If you overshoot, you can cancel some C-s characters with DEL.

After you exit 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 “search again”.

If the specified string is not found at all, the echo area displays the text ‘Failing I-Search’. The cursor is after the place where Emacs found as much of your string as it could. Thus, if you search for ‘FOOT’, and there is no ‘FOOT’, the cursor may be after the ‘FOO’ in ‘FOOL’. At this point there are several things you can do. If you mistyped the search string, correct it. If you like the place you have found, you can type RET or some other Emacs command to “accept what the search offered”. Or you can type C-g, which removes from the search string the characters that could not be found (the ‘T’ in ‘FOOT’), leaving those that were found (the ‘FOO’ in ‘FOOT’). A second C-g at that point cancels the search entirely, returning point to where it was when the search started.

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 backward search with C-r starts again from the end. This is called wrapping around. ‘Wrapped’ appears in the search prompt once this has happened.

The C-g “quit” character does special things during searches; just what it does depends on the status of the search. If the search has found what you specified and is waiting for input, C-g cancels the entire search. The cursor moves back to where you started the search. If C-g is typed when there are characters in the search string that have not been found—because Emacs is still searching for them, or because it has failed to find them—then the search string characters which have not been found are discarded from the search string. The search is now successful and waiting for more input, so a second C-g cancels the entire search.

To search for a control character such as C-s or DEL or ESC, you must quote it by typing C-q first. This function of C-q is analogous to its meaning as an Emacs command: it causes the following character to be treated the way a graphic character would normally be treated in the same context.

To search backwards, you can use C-r instead of C-s to start the search; C-r is the key that runs the command (isearch-backward) to search backward. You can also use C-r to change from searching forward to searching backwards. Do this if a search fails because the place you started was too far down in the file. Repeated C-r keeps looking for more occurrences backwards. C-s starts going forward again. You can cancel C-r in a search with DEL.

The characters C-y and C-w can be used in incremental search to grab text from the buffer into the search string. This makes it convenient to search for another occurrence of text at point. C-w copies the word after point as part of the search string, advancing point over that word. Another C-s to repeat the search will then search for a string including that word. C-y is similar to C-w but copies the rest of the current line into the search string.

The characters M-p and M-n can be used in an incremental search to recall things which you have searched for in the past. A list of the last 16 things you have searched for is retained, and M-p and M-n let you cycle through that ring.

The character M-TAB does completion on the elements in the search history ring. For example, if you know that you have recently searched for the string POTATOE, you could type C-s P O M-TAB. If you had searched for other strings beginning with PO then you would be shown a list of them, and would need to type more to select one.

You can change any of the special characters in incremental search via the normal keybinding mechanism: simply add a binding to the isearch-mode-map. For example, to make the character C-b mean “search backwards” while in isearch-mode, do this:

(define-key isearch-mode-map "\C-b" 'isearch-repeat-backward)

These are the default bindings of isearch-mode:


Delete a character from the incremental search string (isearch-delete-char).


Exit incremental search (isearch-exit).


Quote special characters for incremental search (isearch-quote-char).


Repeat incremental search forward (isearch-repeat-forward).


Repeat incremental search backward (isearch-repeat-backward).


Pull rest of line from buffer into search string (isearch-yank-line).


Pull next word from buffer into search string (isearch-yank-word).


Cancels input back to what has been found successfully, or aborts the isearch (isearch-abort).


Recall the previous element in the isearch history ring (isearch-ring-retreat).


Recall the next element in the isearch history ring (isearch-ring-advance).


Do completion on the elements in the isearch history ring (isearch-complete).

Any other character which is normally inserted into a buffer when typed is automatically added to the search string in isearch-mode.

12.1.1 Slow Terminal Incremental Search

Incremental search on a slow terminal uses a modified style of display that is designed to take less time. Instead of redisplaying the buffer at each place the search gets to, it creates a new single-line window and uses that to display the line the search has found. The single-line window appears as soon as point gets outside of the text that is already on the screen.

When the search is terminated, the single-line window is removed. Only at this time the window in which the search was done is redisplayed to show its new value of point.

The three dots at the end of the search string, normally used to indicate that searching is going on, are not displayed in slow style display.

The slow terminal style of display is used when the terminal baud rate is less than or equal to the value of the variable search-slow-speed, initially 1200.

The number of lines to use in slow terminal search display is controlled by the variable search-slow-window-lines. Its normal value is 1.

Next: , Previous: , Up: Search   [Contents][Index]