GNU Emacs

15.12 Tailoring Search to Your Needs

This section describes miscellaneous search-related customizations not described elsewhere.

The default search mode for the incremental search is specified by the variable search-default-mode. It can be nil, t, or a function. If it is nil, the default mode is to do literal searches without character folding, but with case folding and lax-whitespace matches as determined by case-fold-search and search-whitespace-regexp, respectively (see Lax Matching During Searching). If the value is t, incremental search defaults to regexp searches. The default value specifies a function that only performs case folding and lax-whitespace matching.

The current match of an on-going incremental search is highlighted using the isearch face. This highlighting can be disabled by setting the variable search-highlight to nil.

When searching for regular expressions (with C-M-s, for instance), subexpressions receive special highlighting depending on the search-highlight-submatches variable. If this variable’s value is nil, no special highlighting is done, but if the value is non- nil, text that matches ‘ \( … \)’ constructs (a.k.a. “subexpressions”) in the regular expression will be highlighted with distinct faces. By default, two distinct faces are defined, named isearch-group-1 and isearch-group-2. With these two faces, odd-numbered subexpressions will be highlighted using the isearch-group-1 face and even-numbered subexpressions will be highlighted using the isearch-group-2 face. For instance, when searching for ‘ foo-\([0-9]+\)\([a-z]+\)’, the part matched by ‘ [0-9]+’ will be highlighted with the isearch-group-1 face, and the part matched by ‘ [a-z]+’ will be highlighted using isearch-group-2. If you define additional faces using the same numbering scheme, i.e. isearch-group-3, isearch-group-4, …, then the face isearch-group-M will be used to highlight the M’th, N+M’th, 2N+M’th, … subexpressions, where N is the total number of faces of the form isearch-group-M.

The other matches for the search string that are visible on display are highlighted using the lazy-highlight face. Setting the variable isearch-lazy-highlight to nil disables this highlighting. Here are some other variables that customize the lazy highlighting:


Time in seconds to wait before highlighting visible matches. Applies only if the search string is less than lazy-highlight-no-delay-length characters long.


For search strings at least as long as the value of this variable, lazy highlighting of matches starts immediately.


Time in seconds between highlighting successive matches.


The maximum number of matches to highlight before checking for input. A large number can take some time to highlight, so if you want to continue searching and type C-s or C-r during that time, Emacs will not respond until it finishes highlighting all those matches. Thus, smaller values make Emacs more responsive.


Show the current match number and the total number of matches in the search prompt.

lazy-count-prefix-format lazy-count-suffix-format

These two variables determine the format of showing the current and the total number of matches for isearch-lazy-count.

Normally, entering RET within incremental search when the search string is empty launches a nonincremental search. (Actually, it lets you edit the search string, and the next RET does the search.) However, if you customize the variable search-nonincremental-instead to nil, typing RET will always exit the incremental search, even if the search string is empty.

By default, incremental search and query-replace commands match invisible text, but hide any such matches as soon as the current match moves off the invisible text. If you customize the variable isearch-hide-immediately to nil, any invisible text where matches were found stays on display until the search or the replace command exits.

Searching incrementally on slow terminals, such as displays connected to remote machines over slow connection, could be annoying due to the need to redraw large portions of the display as the search proceeds. Emacs provides a special display mode for slow terminals, whereby search pops up a separate small window and displays the text surrounding the match in that window. Small windows display faster, so the annoying effect of slow speed is alleviated. The variable search-slow-speed determines the baud rate threshold below which Emacs will use this display mode. The variable search-slow-window-lines controls the number of lines in the window Emacs pops up for displaying the search results; the default is 1 line. Normally, this window will pop up at the bottom of the window that displays the buffer where you start searching, but if the value of search-slow-window-lines is negative, that means to put the window at the top and give it the number of lines that is the absolute value of search-slow-window-lines.