Avoid aborts in cm.c due to too small TTY frame
[emacs.git] / doc / emacs / search.texi
blobc0adab497baaf5f350d03d4ae6787f7da442801e
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 2000-2018 Free Software
3 @c Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Search
6 @chapter Searching and Replacement
7 @cindex searching
8 @cindex finding strings within text
10   Like other editors, Emacs has commands to search for occurrences of
11 a string.  Emacs also has commands to replace occurrences of a string
12 with a different string.  There are also commands that do the same
13 thing, but search for patterns instead of fixed strings.
15   You can also search multiple files under the control of @code{xref}
16 (@pxref{Identifier Search}) or through the Dired @kbd{A} command
17 (@pxref{Operating on Files}), or ask the @code{grep} program to do it
18 (@pxref{Grep Searching}).
20 @menu
21 * Incremental Search::        Search happens as you type the string.
22 * Nonincremental Search::     Specify entire string and then search.
23 * Word Search::               Search for sequence of words.
24 * Symbol Search::             Search for a source code symbol.
25 * Regexp Search::             Search for match for a regexp.
26 * Regexps::                   Syntax of regular expressions.
27 * Regexp Backslash::          Regular expression constructs starting with `\'.
28 * Regexp Example::            A complex regular expression explained.
29 * Lax Search::                Search ignores some distinctions among
30                                 similar characters, like letter-case.
31 * Replace::                   Search, and replace some or all matches.
32 * Other Repeating Search::    Operating on all matches for some regexp.
33 * Search Customizations::     Various search customizations.
34 @end menu
36 @node Incremental Search
37 @section Incremental Search
38 @cindex incremental search
39 @cindex isearch
41   The principal search command in Emacs is @dfn{incremental}: it
42 begins searching as soon as you type the first character of the search
43 string.  As you type in the search string, Emacs shows you where the
44 string (as you have typed it so far) would be found.  When you have
45 typed enough characters to identify the place you want, you can stop.
46 Depending on what you plan to do next, you may or may not need to
47 terminate the search explicitly with @key{RET}.
49 @table @kbd
50 @item C-s
51 Incremental search forward (@code{isearch-forward}).
52 @item C-r
53 Incremental search backward (@code{isearch-backward}).
54 @end table
56 You can also invoke incremental search from the menu bar's
57 @samp{Edit->Search} menu.
59 @menu
60 * Basic Isearch::        Basic incremental search commands.
61 * Repeat Isearch::       Searching for the same string again.
62 * Isearch Yank::         Commands that grab text into the search string
63                            or else edit the search string.
64 * Error in Isearch::     When your string is not found.
65 * Special Isearch::      Special input in incremental search.
66 * Not Exiting Isearch::  Prefix argument and scrolling commands.
67 * Isearch Minibuffer::   Incremental search of the minibuffer history.
68 @end menu
70 @node Basic Isearch
71 @subsection Basics of Incremental Search
73 @table @kbd
74 @item C-s
75 Begin incremental search (@code{isearch-forward}).
76 @item C-r
77 Begin reverse incremental search (@code{isearch-backward}).
78 @end table
80 @kindex C-s
81 @findex isearch-forward
82   @kbd{C-s} (@code{isearch-forward}) starts a forward incremental
83 search.  It reads characters from the keyboard, and moves point just
84 past the end of the next occurrence of those characters in the buffer.
86   For instance, if you type @kbd{C-s} and then @kbd{F}, that puts the
87 cursor after the first @samp{F} that occurs in the buffer after the
88 starting point.  If you then type @kbd{O}, the cursor moves to just
89 after the first @samp{FO}; the @samp{F} in that @samp{FO} might not be
90 the first @samp{F} previously found.  After another @kbd{O}, the
91 cursor moves to just after the first @samp{FOO}.
93 @cindex faces for highlighting search matches
94 @cindex isearch face
95   At each step, Emacs highlights the @dfn{current match}---the buffer
96 text that matches the search string---using the @code{isearch} face
97 (@pxref{Faces}).  @xref{Search Customizations}, for various options
98 that customize this highlighting.  The current search string is also
99 displayed in the echo area.
101   If you make a mistake typing the search string, type @key{DEL}.
102 Each @key{DEL} cancels the last character of the search string.
103 @xref{Error in Isearch}, for more about dealing with unsuccessful
104 search.
106 @cindex exit incremental search
107 @cindex incremental search, exiting
108   When you are satisfied with the place you have reached, type
109 @key{RET}.  This stops searching, leaving the cursor where the search
110 brought it.  Also, any command not specially meaningful in searches
111 stops the searching and is then executed.  Thus, typing @kbd{C-a}
112 exits the search and then moves to the beginning of the line; typing
113 one of the arrow keys exits the search and performs the respective
114 movement command; etc.  @key{RET} is necessary only if the next
115 command you want to type is a printing character, @key{DEL},
116 @key{RET}, or another character that is special within searches
117 (@kbd{C-q}, @kbd{C-w}, @kbd{C-r}, @kbd{C-s}, @kbd{C-y}, @kbd{M-y},
118 @kbd{M-r}, @kbd{M-c}, @kbd{M-e}, and some others described below).
119 You can fine-tune the commands that exit the search; see @ref{Not
120 Exiting Isearch}.
122   As a special exception, entering @key{RET} when the search string is
123 empty launches nonincremental search (@pxref{Nonincremental Search}).
124 (This can be customized; see @ref{Search Customizations}.)
126   To abandon the search and return to the place where you started,
127 type @kbd{@key{ESC} @key{ESC} @key{ESC}} (@code{isearch-cancel}) or
128 @kbd{C-g C-g} (@code{isearch-abort}).
130   When you exit the incremental search, it adds the original value of
131 point to the mark ring, without activating the mark; you can thus use
132 @kbd{C-u C-@key{SPC}} or @kbd{C-x C-x} to return to where you were
133 before beginning the search.  @xref{Mark Ring}.  (Emacs only does this
134 if the mark was not already active; if the mark was active when you
135 started the search, both @kbd{C-u C-@key{SPC}} and @kbd{C-x C-x} will
136 go to the mark.)
138 @kindex C-r
139 @findex isearch-backward
140   To search backwards, use @kbd{C-r} (@code{isearch-backward}) instead
141 of @kbd{C-s} to start the search.  A backward search finds matches
142 that end before the starting point, just as a forward search finds
143 matches that begin after it.
145 @node Repeat Isearch
146 @subsection Repeating Incremental Search
148   Suppose you search forward for @samp{FOO} and find a match, but not
149 the one you expected to find: the @samp{FOO} you were aiming for
150 occurs later in the buffer.  In this event, type another @kbd{C-s} to
151 move to the next occurrence of the search string.  You can repeat this
152 any number of times.  If you overshoot, you can cancel some @kbd{C-s}
153 characters with @key{DEL}.  Similarly, each @kbd{C-r} in a backward
154 incremental search repeats the backward search.
156 @cindex lazy search highlighting
157   If you pause for a little while during incremental search, Emacs
158 highlights all the other possible matches for the search string that
159 are present on the screen.  This helps you anticipate where you can
160 get to by typing @kbd{C-s} or @kbd{C-r} to repeat the search.  The
161 other matches are highlighted differently from the current match,
162 using the customizable face @code{lazy-highlight} (@pxref{Faces}).  If
163 you don't like this feature, you can disable it by setting
164 @code{isearch-lazy-highlight} to @code{nil}.  For other customizations
165 related to highlighting matches, see @ref{Search Customizations}.
167   After exiting a search, you can search for the same string again by
168 typing just @kbd{C-s C-s}.  The first @kbd{C-s} is the key that
169 invokes incremental search, and the second @kbd{C-s} means to search
170 again for the last search string.  Similarly, @kbd{C-r C-r} searches
171 backward for the last search string.  In determining the last search
172 string, it doesn't matter whether that string was searched for with
173 @kbd{C-s} or @kbd{C-r}.
175   If you are searching forward but you realize you were looking for
176 something before the starting point, type @kbd{C-r} to switch to a
177 backward search, leaving the search string unchanged.  Similarly,
178 @kbd{C-s} in a backward search switches to a forward search.
180 @cindex search, wrapping around
181 @cindex search, overwrapped
182 @cindex wrapped search
183 @cindex overwrapped search
184   If a search is failing and you ask to repeat it by typing another
185 @kbd{C-s}, it starts again from the beginning of the buffer.
186 Repeating a failing reverse search with @kbd{C-r} starts again from
187 the end.  This is called @dfn{wrapping around}, and @samp{Wrapped}
188 appears in the search prompt once this has happened.  If you keep on
189 going past the original starting point of the search, it changes to
190 @samp{Overwrapped}, which means that you are revisiting matches that
191 you have already seen.
193 @cindex search ring
194 @kindex M-n @r{(Incremental search)}
195 @kindex M-p @r{(Incremental search)}
196 @vindex search-ring-max
197   To reuse earlier search strings, use the @dfn{search ring}.  The
198 commands @kbd{M-p} and @kbd{M-n} move through the ring to pick a
199 search string to reuse.  These commands leave the selected search ring
200 element in the minibuffer, where you can edit it.  Type
201 @kbd{C-s}/@kbd{C-r} or @key{RET} to accept the string and start
202 searching for it.  The number of most recently used search strings
203 saved in the search ring is specified by the variable
204 @code{search-ring-max}, 16 by default.
206 @cindex incremental search, edit search string
207 @cindex interactively edit search string
208 @kindex M-e @r{(Incremental search)}
209 @kindex mouse-1 @r{in the minibuffer (Incremental Search)}
210   To edit the current search string in the minibuffer without
211 replacing it with items from the search ring, type @kbd{M-e} or click
212 @kbd{mouse-1} in the minibuffer.  Type @key{RET}, @kbd{C-s} or
213 @kbd{C-r} to finish editing the string and search for it.  Type
214 @kbd{C-f} or @kbd{@key{RIGHT}} to add to the search string characters
215 following point from the buffer from which you started the search.
217 @node Isearch Yank
218 @subsection Isearch Yanking
220   In many cases, you will want to use text at or near point as your
221 search string.  The commands described in this subsection let you do
222 that conveniently.
224 @kindex C-w @r{(Incremental search)}
225 @findex isearch-yank-word-or-char
226   @kbd{C-w} (@code{isearch-yank-word-or-char}) appends the next
227 character or word at point to the search string.  This is an easy way
228 to search for another occurrence of the text at point.  (The decision
229 of whether to copy a character or a word is heuristic.)
231 @kindex M-s C-e @r{(Incremental search)}
232 @findex isearch-yank-line
233   Similarly, @kbd{M-s C-e} (@code{isearch-yank-line}) appends the rest
234 of the current line to the search string.  If point is already at the
235 end of a line, it appends the next line.  With a prefix argument
236 @var{n}, it appends the next @var{n} lines.
238 @kindex C-y @r{(Incremental search)}
239 @kindex M-y @r{(Incremental search)}
240 @kindex mouse-2 @r{in the minibuffer (Incremental search)}
241 @findex isearch-yank-kill
242 @findex isearch-yank-pop
243 @findex isearch-yank-x-selection
244   Within incremental search, @kbd{C-y} (@code{isearch-yank-kill})
245 appends the current kill to the search string.  @kbd{M-y}
246 (@code{isearch-yank-pop}), if called after @kbd{C-y}, replaces that
247 appended text with an earlier kill, similar to the usual @kbd{M-y}
248 (@code{yank-pop}) command (@pxref{Yanking}).  Clicking @kbd{mouse-2}
249 in the echo area appends the current X selection (@pxref{Primary
250 Selection}) to the search string (@code{isearch-yank-x-selection}).
252 @kindex C-M-w @r{(Incremental search)}
253 @kindex C-M-y @r{(Incremental search)}
254 @findex isearch-del-char
255 @findex isearch-yank-char
256   @kbd{C-M-w} (@code{isearch-del-char}) deletes the last character
257 from the search string, and @kbd{C-M-y} (@code{isearch-yank-char})
258 appends the character after point to the search string.  An
259 alternative method to add the character after point is to enter the
260 minibuffer with @kbd{M-e} (@pxref{Repeat Isearch}) and type @kbd{C-f}
261 or @kbd{@key{RIGHT}} at the end of the search string in the
262 minibuffer.  Each @kbd{C-f} or @kbd{@key{RIGHT}} you type adds another
263 character following point to the search string.
265   Normally, when the search is case-insensitive, text yanked into the
266 search string is converted to lower case, so that the search remains
267 case-insensitive (@pxref{Lax Search, case folding}).  However, if the
268 value of the variable @code{search-upper-case} (@pxref{Lax Search,
269 search-upper-case}) is other than @code{not-yanks}, that disables this
270 down-casing.
272 @node Error in Isearch
273 @subsection Errors in Incremental Search
275 @cindex isearch-fail face
276   If your string is not found at all, the echo area says @samp{Failing
277 I-Search}, and the cursor moves past the place where Emacs found as
278 much of your string as it could.  Thus, if you search for @samp{FOOT},
279 and there is no @samp{FOOT}, you might see the cursor after the
280 @samp{FOO} in @samp{FOOL}.  In the echo area, the part of the search
281 string that failed to match is highlighted using the face
282 @code{isearch-fail}.
284   At this point, there are several things you can do.  If your string
285 was mistyped, you can use @key{DEL} to erase some of it and correct
286 it, or you can type @kbd{M-e} and edit it.  If you like the place you
287 have found, you can type @key{RET} to remain there.  Or you can type
288 @kbd{C-g}, which removes from the search string the characters that
289 could not be found (the @samp{T} in @samp{FOOT}), leaving those that
290 were found (the @samp{FOO} in @samp{FOOT}).  A second @kbd{C-g} at
291 that point cancels the search entirely, returning point to where it
292 was when the search started.
294 @cindex quitting (in search)
295 @kindex C-g @r{(Incremental search)}
296   The quit command, @kbd{C-g}, does special things during searches;
297 just what it does depends on the status of the search.  If the search
298 has found what you specified and is waiting for input, @kbd{C-g}
299 cancels the entire search, moving the cursor back to where you started
300 the search.  If @kbd{C-g} is typed when there are characters in the
301 search string that have not been found---because Emacs is still
302 searching for them, or because it has failed to find them---then the
303 search string characters which have not been found are discarded from
304 the search string.  With them gone, the search is now successful and
305 waiting for more input, so a second @kbd{C-g} will cancel the entire
306 search.
308 @node Special Isearch
309 @subsection Special Input for Incremental Search
311   In addition to characters described in the previous subsections,
312 some of the other characters you type during incremental search have
313 special effects.  They are described here.
315   To toggle lax space matching (@pxref{Lax Search, lax space
316 matching}), type @kbd{M-s @key{SPC}}.
318   To toggle case sensitivity of the search, type @kbd{M-c} or
319 @kbd{M-s c}.  @xref{Lax Search, case folding}.  If the search string
320 includes upper-case letters, the search is case-sensitive by default.
322   To toggle whether or not the search will consider similar and
323 equivalent characters as a match, type @kbd{M-s '}.  @xref{Lax Search,
324 character folding}.  If the search string includes accented
325 characters, that disables character folding during that search.
327 @cindex invisible text, searching for
328 @kindex M-s i @r{(Incremental search)}
329 @findex isearch-toggle-invisible
330   To toggle whether or not invisible text is searched, type
331 @kbd{M-s i} (@code{isearch-toggle-invisible}).  @xref{Outline Search}.
333 @kindex M-r @r{(Incremental Search)}
334 @kindex M-s r @r{(Incremental Search)}
335 @findex isearch-toggle-regexp
336   To toggle between non-regexp and regexp incremental search, type
337 @kbd{M-r} or @kbd{M-s r} (@code{isearch-toggle-regexp}).
338 @xref{Regexp Search}.
340   To toggle symbol mode, type @kbd{M-s _}.  @xref{Symbol Search}.
342   To search for a newline character, type @kbd{C-j} as part of the
343 search string.
345   To search for non-@acronym{ASCII} characters, use one of the
346 following methods:
348 @itemize @bullet
349 @item
350 Type @kbd{C-q}, followed by a non-graphic character or a sequence of
351 octal digits.  This adds a character to the search string, similar to
352 inserting into a buffer using @kbd{C-q} (@pxref{Inserting Text}).  For
353 example, @kbd{C-q C-s} during incremental search adds the
354 @samp{control-S} character to the search string.
356 @item
357 Type @kbd{C-x 8 @key{RET}}, followed by a Unicode name or code-point
358 in hex.  This adds the specified character into the search string,
359 similar to the usual @code{insert-char} command (@pxref{Inserting
360 Text}).
362 @item
363 @kindex C-^ @r{(Incremental Search)}
364 @findex isearch-toggle-input-method
365 @findex isearch-toggle-specified-input-method
366 Use an input method (@pxref{Input Methods}).  If an input method is
367 enabled in the current buffer when you start the search, the same
368 method will be active in the minibuffer when you type the search
369 string.  While typing the search string, you can toggle the input
370 method with @kbd{C-\} (@code{isearch-toggle-input-method}).  You can
371 also turn on a non-default input method with @kbd{C-^}
372 (@code{isearch-toggle-specified-input-method}), which prompts for the
373 name of the input method.  When an input method is active during
374 incremental search, the search prompt includes the input method
375 mnemonic, like this:
377 @example
378 I-search [@var{im}]:
379 @end example
381 @noindent
382 where @var{im} is the mnemonic of the active input method.  Any input
383 method you enable during incremental search remains enabled in the
384 current buffer afterwards.
385 @end itemize
387 @kindex M-s o @r{(Incremental Search)}
388 @findex isearch-occur
389   Typing @kbd{M-s o} in incremental search invokes
390 @code{isearch-occur}, which runs @code{occur} with the current search
391 string.  @xref{Other Repeating Search, occur}.
393 @kindex M-% @r{(Incremental search)}
394   Typing @kbd{M-%} in incremental search invokes @code{query-replace}
395 or @code{query-replace-regexp} (depending on search mode) with the
396 current search string used as the string to replace.  A negative
397 prefix argument means to replace backward.  @xref{Query Replace}.
399 @kindex M-TAB @r{(Incremental search)}
400   Typing @kbd{M-@key{TAB}} in incremental search invokes
401 @code{isearch-complete}, which attempts to complete the search string
402 using the search ring (the previous search strings you used) as a list
403 of completion alternatives.  @xref{Completion}.  In many operating
404 systems, the @kbd{M-@key{TAB}} key sequence is captured by the window
405 manager; you then need to rebind @code{isearch-complete} to another
406 key sequence if you want to use it (@pxref{Rebinding}).
408 @kindex M-s h r @r{(Incremental Search)}
409 @findex isearch-highlight-regexp
410   You can exit the search while leaving the matches for the last
411 search string highlighted on display.  To this end, type @kbd{M-s h r}
412 (@code{isearch-highlight-regexp}), which will run
413 @code{highlight-regexp} (@pxref{Highlight Interactively}) passing
414 it the regexp derived from the last search string and prompting you
415 for the face to use for highlighting.  To remove the highlighting,
416 type @kbd{M-s h u} (@code{unhighlight-regexp}).
418 @cindex incremental search, help on special keys
419 @kindex C-h C-h @r{(Incremental Search)}
420 @findex isearch-help-map
421 @vindex isearch-mode-map
422   When incremental search is active, you can type @kbd{C-h C-h}
423 (@code{isearch-help-map}) to access interactive help options,
424 including a list of special key bindings.  These key bindings are part
425 of the keymap @code{isearch-mode-map} (@pxref{Keymaps}).
427 @node Not Exiting Isearch
428 @subsection Not Exiting Incremental Search
430 This subsection describes how to control whether typing a command not
431 specifically meaningful in searches exits the search before executing
432 the command.  It also describes two categories of commands which you
433 can type without exiting the current incremental search, even though
434 they are not themselves part of incremental search.
436 @vindex search-exit-option
437   Normally, typing a command that is not bound by the incremental
438 search exits the search before executing the command.  Thus, the
439 command operates on the buffer from which you invoked the search.
440 However, if you customize the variable @code{search-exit-option} to
441 @code{nil}, the characters which you type that are not interpreted by
442 the incremental search are simply appended to the search string.  This
443 is so you could include in the search string control characters, such
444 as @kbd{C-a}, that would normally exit the search and invoke the
445 command bound to them on the buffer.
447 @table @asis
448 @item Prefix Arguments
449 @cindex prefix argument commands, during incremental search
450 @vindex isearch-allow-prefix
451   In incremental search, when you type a command that specifies a
452 prefix argument (@pxref{Arguments}), by default it will apply either
453 to the next action in the search or to the command that exits the
454 search.  In other words, entering a prefix argument will not by itself
455 terminate the search.
457   In previous versions of Emacs, entering a prefix argument always
458 terminated the search.  You can revert to this behavior by setting the
459 variable @code{isearch-allow-prefix} to @code{nil}.
461   When @code{isearch-allow-scroll} is non-@code{nil} (see below),
462 prefix arguments always have the default behavior described above,
463 i.e., they don't terminate the search, even if
464 @code{isearch-allow-prefix} is @code{nil}.
466 @item Scrolling Commands
467 @cindex scrolling commands, during incremental search
468 @vindex isearch-allow-scroll
469   Normally, scrolling commands exit incremental search.  If you change
470 the variable @code{isearch-allow-scroll} to a non-@code{nil} value,
471 that enables the use of the scroll-bar, as well as keyboard scrolling
472 commands like @kbd{C-v}, @kbd{M-v}, and @kbd{C-l} (@pxref{Scrolling}).
473 This applies only to calling these commands via their bound key
474 sequences---typing @kbd{M-x} will still exit the search.  You can give
475 prefix arguments to these commands in the usual way.  This feature
476 won't let you scroll the current match out of visibility, however.
478   The @code{isearch-allow-scroll} feature also affects some other
479 commands, such as @kbd{C-x 2} (@code{split-window-below}) and
480 @kbd{C-x ^} (@code{enlarge-window}), which don't exactly scroll but do
481 affect where the text appears on the screen.  It applies to any
482 command whose name has a non-@code{nil} @code{isearch-scroll}
483 property.  So you can control which commands are affected by changing
484 these properties.
486 @cindex prevent commands from exiting incremental search
487   For example, to make @kbd{C-h l} usable within an incremental search
488 in all future Emacs sessions, use @kbd{C-h c} to find what command it
489 runs (@pxref{Key Help}), which is @code{view-lossage}.  Then you can
490 put the following line in your init file (@pxref{Init File}):
492 @example
493 (put 'view-lossage 'isearch-scroll t)
494 @end example
496 @noindent
497 This feature can be applied to any command that doesn't permanently
498 change point, the buffer contents, the match data, the current buffer,
499 or the selected window and frame.  The command must not itself attempt
500 an incremental search.  This feature is disabled if
501 @code{isearch-allow-scroll} is @code{nil} (which it is by default).
502 @end table
504 @node Isearch Minibuffer
505 @subsection Searching the Minibuffer
506 @cindex minibuffer history, searching
508 If you start an incremental search while the minibuffer is active,
509 Emacs searches the contents of the minibuffer.  Unlike searching an
510 ordinary buffer, the search string is not shown in the echo area,
511 because that is used to display the minibuffer.
513 If an incremental search fails in the minibuffer, it tries searching
514 the minibuffer history.  @xref{Minibuffer History}.  You can visualize
515 the minibuffer and its history as a series of pages, with the
516 earliest history element on the first page and the current minibuffer
517 on the last page.  A forward search, @kbd{C-s}, searches forward to
518 later pages; a reverse search, @kbd{C-r}, searches backwards to
519 earlier pages.  Like in ordinary buffer search, a failing search can
520 wrap around, going from the last page to the first page or vice versa.
522 When the current match is on a history element, that history element
523 is pulled into the minibuffer.  If you exit the incremental search
524 normally (e.g., by typing @key{RET}), it remains in the minibuffer
525 afterwards.  Canceling the search, with @kbd{C-g}, restores the
526 contents of the minibuffer when you began the search.
528 @node Nonincremental Search
529 @section Nonincremental Search
530 @cindex nonincremental search
532   Emacs also has conventional nonincremental search commands, which require
533 you to type the entire search string before searching begins.
535 @table @kbd
536 @item C-s @key{RET} @var{string} @key{RET}
537 Search for @var{string}.
538 @item C-r @key{RET} @var{string} @key{RET}
539 Search backward for @var{string}.
540 @end table
542   To start a nonincremental search, first type @kbd{C-s @key{RET}}.
543 This enters the minibuffer to read the search string; terminate the
544 string with @key{RET}, and then the search takes place.  If the string
545 is not found, the search command signals an error.
547   When you type @kbd{C-s @key{RET}}, the @kbd{C-s} invokes incremental
548 search as usual.  That command is specially programmed to invoke the
549 command for nonincremental search, if the string you specify is empty.
550 (Such an empty argument would otherwise be useless.)  @kbd{C-r
551 @key{RET}} does likewise, invoking the nonincremental
552 backward-searching command.
554   Nonincremental search can also be invoked from the menu bar's
555 @samp{Edit->Search} menu.
557 @findex search-forward
558 @findex search-backward
559   You can also use two simpler commands, @kbd{M-x search-forward} and
560 @kbd{M-x search-backward}.  These commands look for the literal
561 strings you specify, and don't support any of the lax-search features
562 (@pxref{Lax Search}) except case folding.
564 @node Word Search
565 @section Word Search
566 @cindex word search
568   A @dfn{word search} finds a sequence of words without regard to the
569 type of punctuation between them.  For instance, if you enter a search
570 string that consists of two words separated by a single space, the
571 search matches any sequence of those two words separated by one or
572 more spaces, newlines, or other punctuation characters.  This is
573 particularly useful for searching text documents, because you don't
574 have to worry whether the words you are looking for are separated by
575 newlines or spaces.  Note that major modes for programming languages
576 or other specialized modes can modify the definition of a word to suit
577 their syntactic needs.
579 @table @kbd
580 @item M-s w
581   If incremental search is active, toggle word search mode
582 (@code{isearch-toggle-word}); otherwise, begin an incremental forward
583 word search (@code{isearch-forward-word}).
584 @item M-s w @key{RET} @var{words} @key{RET}
585 Search for @var{words}, using a forward nonincremental word search.
586 @item M-s w C-r @key{RET} @var{words} @key{RET}
587 Search backward for @var{words}, using a nonincremental word search.
588 @item M-s M-w
589 Search the Web for the text in region.
590 @end table
592 @kindex M-s w
593 @findex isearch-forward-word
594   To begin a forward incremental word search, type @kbd{M-s w}.  If
595 incremental search is not already active, this runs the command
596 @code{isearch-forward-word}.  If incremental search is already active
597 (whether a forward or backward search), @kbd{M-s w} switches to a word
598 search while keeping the direction of the search and the current
599 search string unchanged.  You can toggle word search back off by
600 typing @kbd{M-s w} again.
602 @findex word-search-forward
603 @findex word-search-backward
604   To begin a nonincremental word search, type @kbd{M-s w @key{RET}}
605 for a forward search, or @kbd{M-s w C-r @key{RET}} for a backward search.
606 These run the commands @code{word-search-forward} and
607 @code{word-search-backward} respectively.
609   Incremental and nonincremental word searches differ slightly in the
610 way they find a match.  In a nonincremental word search, each word in
611 the search string must exactly match a whole word.  In an incremental
612 word search, the matching is more lax: while you are typing the search
613 string, its first and last words need not match whole words.  This is
614 so that the matching can proceed incrementally as you type.  This
615 additional laxity does not apply to the lazy highlight
616 (@pxref{Incremental Search}), which always matches whole words.
617 While you are typing the search string, @samp{Pending} appears in the
618 search prompt until you use a search repeating key like @kbd{C-s}.
620   The word search commands don't perform character folding, and
621 toggling lax whitespace matching (@pxref{Lax Search, lax space
622 matching}) has no effect on them.
624 @kindex M-s M-w
625 @findex eww-search-words
626 @vindex eww-search-prefix
627   To search the Web for the text in region, type @kbd{M-s M-w}.  This
628 command performs an Internet search for the words in region using the
629 search engine whose @acronym{URL} is specified by the variable
630 @code{eww-search-prefix} (@pxref{Basics, EWW, , eww, The Emacs Web
631 Wowser Manual}).  If the region is not active, or doesn't contain any
632 words, this command prompts the user for a URL or keywords to search.
635 @node Symbol Search
636 @section Symbol Search
637 @cindex symbol search
639   A @dfn{symbol search} is much like an ordinary search, except that
640 the boundaries of the search must match the boundaries of a symbol.
641 The meaning of @dfn{symbol} in this context depends on the major mode,
642 and usually refers to a source code token, such as a Lisp symbol in
643 Emacs Lisp mode.  For instance, if you perform an incremental symbol
644 search for the Lisp symbol @code{forward-word}, it would not match
645 @code{isearch-forward-word}.  This feature is thus mainly useful for
646 searching source code.
648 @table @kbd
649 @item M-s _
650 @findex isearch-toggle-symbol
651 If incremental search is active, toggle symbol search mode
652 (@code{isearch-toggle-symbol}); otherwise, begin an incremental
653 forward symbol search (@code{isearch-forward-symbol}).
654 @item M-s .
655 Start a symbol incremental search forward with the symbol found near
656 point added to the search string initially.
657 @item M-s _ @key{RET} @var{symbol} @key{RET}
658 Search forward for @var{symbol}, nonincrementally.
659 @item M-s _ C-r @key{RET} @var{symbol} @key{RET}
660 Search backward for @var{symbol}, nonincrementally.
661 @end table
663 @kindex M-s _
664 @kindex M-s .
665 @findex isearch-forward-symbol
666 @findex isearch-forward-symbol-at-point
667   To begin a forward incremental symbol search, type @kbd{M-s _} (or
668 @kbd{M-s .} if the symbol to search is near point).  If incremental
669 search is not already active, this runs the command
670 @code{isearch-forward-symbol}.  If incremental search is already
671 active, @kbd{M-s _} switches to a symbol search, preserving the
672 direction of the search and the current search string; you can disable
673 symbol search by typing @kbd{M-s _} again.  In incremental symbol
674 search, while you are typing the search string, only the beginning
675 of the search string is required to match the beginning of a symbol,
676 and @samp{Pending} appears in the search prompt until you use a search
677 repeating key like @kbd{C-s}.
679   To begin a nonincremental symbol search, type @kbd{M-s _ @key{RET}}
680 for a forward search, or @kbd{M-s _ C-r @key{RET}} or a backward
681 search.  In nonincremental symbol searches, the beginning and end of
682 the search string are required to match the beginning and end of a
683 symbol, respectively.
685   The symbol search commands don't perform character folding, and
686 toggling lax whitespace matching (@pxref{Lax Search, lax space
687 matching}) has no effect on them.
689 @node Regexp Search
690 @section Regular Expression Search
691 @cindex regexp search
692 @cindex search for a regular expression
694   A @dfn{regular expression} (or @dfn{regexp} for short) is a pattern
695 that denotes a class of alternative strings to match.  Emacs
696 provides both incremental and nonincremental ways to search for a
697 match for a regexp.  The syntax of regular expressions is explained in
698 the next section.
700 @table @kbd
701 @item C-M-s
702 Begin incremental regexp search (@code{isearch-forward-regexp}).
703 @item C-M-r
704 Begin reverse incremental regexp search (@code{isearch-backward-regexp}).
705 @end table
707 @kindex C-M-s
708 @findex isearch-forward-regexp
709 @kindex C-M-r
710 @findex isearch-backward-regexp
711   Incremental search for a regexp is done by typing @kbd{C-M-s}
712 (@code{isearch-forward-regexp}), by invoking @kbd{C-s} with a
713 prefix argument (whose value does not matter), or by typing @kbd{M-r}
714 within a forward incremental search.  This command reads a
715 search string incrementally just like @kbd{C-s}, but it treats the
716 search string as a regexp rather than looking for an exact match
717 against the text in the buffer.  Each time you add text to the search
718 string, you make the regexp longer, and the new regexp is searched
719 for.  To search backward for a regexp, use @kbd{C-M-r}
720 (@code{isearch-backward-regexp}), @kbd{C-r} with a prefix argument,
721 or @kbd{M-r} within a backward incremental search.
723 @vindex regexp-search-ring-max
724   All of the special key sequences in an ordinary incremental search
725 (@pxref{Special Isearch}) do similar things in an incremental regexp
726 search.  For instance, typing @kbd{C-s} immediately after starting the
727 search retrieves the last incremental search regexp used and searches
728 forward for it.  Incremental regexp and non-regexp searches have
729 independent defaults.  They also have separate search rings, which you
730 can access with @kbd{M-p} and @kbd{M-n}.  The maximum number of search
731 regexps saved in the search ring is determined by the value of
732 @code{regexp-search-ring-max}, 16 by default.
734   Unlike ordinary incremental search, incremental regexp search
735 does not use lax space matching by default.  To toggle this feature
736 use @kbd{M-s @key{SPC}} (@code{isearch-toggle-lax-whitespace}).
737 Then any @key{SPC} typed in incremental regexp search will match
738 any sequence of one or more whitespace characters.  The variable
739 @code{search-whitespace-regexp} specifies the regexp for the lax
740 space matching.  @xref{Special Isearch}.
742   Also unlike ordinary incremental search, incremental regexp search
743 cannot use character folding (@pxref{Lax Search}).  (If you toggle
744 character folding during incremental regexp search with @kbd{M-s '},
745 the search becomes a non-regexp search and the search pattern you
746 typed is interpreted as a literal string.)
748   In some cases, adding characters to the regexp in an incremental
749 regexp search can make the cursor move back and start again.  For
750 example, if you have searched for @samp{foo} and you add @samp{\|bar},
751 the cursor backs up in case the first @samp{bar} precedes the first
752 @samp{foo}.  @xref{Regexps}.
754   Forward and backward regexp search are not symmetrical, because
755 regexp matching in Emacs always operates forward, starting with the
756 beginning of the regexp.  Thus, forward regexp search scans forward,
757 trying a forward match at each possible starting position.  Backward
758 regexp search scans backward, trying a forward match at each possible
759 starting position.  These search methods are not mirror images.
761 @findex re-search-forward
762 @findex re-search-backward
763   Nonincremental search for a regexp is done with the commands
764 @code{re-search-forward} and @code{re-search-backward}.  You can
765 invoke these with @kbd{M-x}, or by way of incremental regexp search
766 with @kbd{C-M-s @key{RET}} and @kbd{C-M-r @key{RET}}.  When you invoke
767 these commands with @kbd{M-x}, they search for the exact regexp you
768 specify, and thus don't support any lax-search features (@pxref{Lax
769 Search}) except case folding.
771   If you use the incremental regexp search commands with a prefix
772 argument, they perform ordinary string search, like
773 @code{isearch-forward} and @code{isearch-backward}.  @xref{Incremental
774 Search}.
776 @node Regexps
777 @section Syntax of Regular Expressions
778 @cindex syntax of regexps
779 @cindex regular expression
780 @cindex regexp
782   This section (and this manual in general) describes regular
783 expression features that users typically use.  @xref{Regular
784 Expressions,,, elisp, The Emacs Lisp Reference Manual}, for additional
785 features used mainly in Lisp programs.
787   Regular expressions have a syntax in which a few characters are
788 special constructs and the rest are @dfn{ordinary}.  An ordinary
789 character matches that same character and nothing else.  The special
790 characters are @samp{$^.*+?[\}.  The character @samp{]} is special if
791 it ends a character alternative (see below).  The character @samp{-}
792 is special inside a character alternative.  Any other character
793 appearing in a regular expression is ordinary, unless a @samp{\}
794 precedes it.  (When you use regular expressions in a Lisp program,
795 each @samp{\} must be doubled, see the example near the end of this
796 section.)
798   For example, @samp{f} is not a special character, so it is ordinary, and
799 therefore @samp{f} is a regular expression that matches the string
800 @samp{f} and no other string.  (It does @emph{not} match the string
801 @samp{ff}.)  Likewise, @samp{o} is a regular expression that matches
802 only @samp{o}.  (When case distinctions are being ignored, these regexps
803 also match @samp{F} and @samp{O}, but we consider this a generalization
804 of ``the same string'', rather than an exception.)
806   Any two regular expressions @var{a} and @var{b} can be concatenated.
807 The result is a regular expression which matches a string if @var{a}
808 matches some amount of the beginning of that string and @var{b}
809 matches the rest of the string.  As a trivial example, concatenating
810 the regular expressions @samp{f} and @samp{o} gives the regular
811 expression @samp{fo}, which matches only the string @samp{fo}.  To do
812 something less trivial, you need to use one of the special characters.
813 Here is a list of them.
815 @table @asis
816 @item @kbd{.}@: @r{(Period)}
817 is a special character that matches any single character except a
818 newline.  For example, the regular expressions @samp{a.b} matches any
819 three-character string that begins with @samp{a} and ends with
820 @samp{b}.
822 @item @kbd{*}
823 is not a construct by itself; it is a postfix operator that means to
824 match the preceding regular expression repetitively any number of
825 times, as many times as possible.  Thus, @samp{o*} matches any number
826 of @samp{o}s, including no @samp{o}s.
828 @samp{*} always applies to the @emph{smallest} possible preceding
829 expression.  Thus, @samp{fo*} has a repeating @samp{o}, not a repeating
830 @samp{fo}.  It matches @samp{f}, @samp{fo}, @samp{foo}, and so on.
832 The matcher processes a @samp{*} construct by matching, immediately,
833 as many repetitions as can be found.  Then it continues with the rest
834 of the pattern.  If that fails, backtracking occurs, discarding some
835 of the matches of the @samp{*}-modified construct in case that makes
836 it possible to match the rest of the pattern.  For example, in matching
837 @samp{ca*ar} against the string @samp{caaar}, the @samp{a*} first
838 tries to match all three @samp{a}s; but the rest of the pattern is
839 @samp{ar} and there is only @samp{r} left to match, so this try fails.
840 The next alternative is for @samp{a*} to match only two @samp{a}s.
841 With this choice, the rest of the regexp matches successfully.
843 @item @kbd{+}
844 is a postfix operator, similar to @samp{*} except that it must match
845 the preceding expression at least once.  Thus, @samp{ca+r} matches the
846 strings @samp{car} and @samp{caaaar} but not the string @samp{cr},
847 whereas @samp{ca*r} matches all three strings.
849 @item @kbd{?}
850 is a postfix operator, similar to @samp{*} except that it can match
851 the preceding expression either once or not at all.  Thus, @samp{ca?r}
852 matches @samp{car} or @samp{cr}, and nothing else.
854 @item @kbd{*?}, @kbd{+?}, @kbd{??}
855 @cindex non-greedy regexp matching
856 are non-@dfn{greedy} variants of the operators above.  The normal
857 operators @samp{*}, @samp{+}, @samp{?} match as much as they can, as
858 long as the overall regexp can still match.  With a following
859 @samp{?}, they will match as little as possible.
861 Thus, both @samp{ab*} and @samp{ab*?} can match the string @samp{a}
862 and the string @samp{abbbb}; but if you try to match them both against
863 the text @samp{abbb}, @samp{ab*} will match it all (the longest valid
864 match), while @samp{ab*?}  will match just @samp{a} (the shortest
865 valid match).
867 Non-greedy operators match the shortest possible string starting at a
868 given starting point; in a forward search, though, the earliest
869 possible starting point for match is always the one chosen.  Thus, if
870 you search for @samp{a.*?$} against the text @samp{abbab} followed by
871 a newline, it matches the whole string.  Since it @emph{can} match
872 starting at the first @samp{a}, it does.
874 @item @kbd{\@{@var{n}\@}}
875 is a postfix operator specifying @var{n} repetitions---that is, the
876 preceding regular expression must match exactly @var{n} times in a
877 row.  For example, @samp{x\@{4\@}} matches the string @samp{xxxx} and
878 nothing else.
880 @item @kbd{\@{@var{n},@var{m}\@}}
881 is a postfix operator specifying between @var{n} and @var{m}
882 repetitions---that is, the preceding regular expression must match at
883 least @var{n} times, but no more than @var{m} times.  If @var{m} is
884 omitted, then there is no upper limit, but the preceding regular
885 expression must match at least @var{n} times.@* @samp{\@{0,1\@}} is
886 equivalent to @samp{?}. @* @samp{\@{0,\@}} is equivalent to
887 @samp{*}. @* @samp{\@{1,\@}} is equivalent to @samp{+}.
889 @item @kbd{[ @dots{} ]}
890 is a @dfn{character set}, beginning with @samp{[} and terminated by
891 @samp{]}.
893 In the simplest case, the characters between the two brackets are what
894 this set can match.  Thus, @samp{[ad]} matches either one @samp{a} or
895 one @samp{d}, and @samp{[ad]*} matches any string composed of just
896 @samp{a}s and @samp{d}s (including the empty string).  It follows that
897 @samp{c[ad]*r} matches @samp{cr}, @samp{car}, @samp{cdr},
898 @samp{caddaar}, etc.
900 You can also include character ranges in a character set, by writing the
901 starting and ending characters with a @samp{-} between them.  Thus,
902 @samp{[a-z]} matches any lower-case @acronym{ASCII} letter.  Ranges may be
903 intermixed freely with individual characters, as in @samp{[a-z$%.]},
904 which matches any lower-case @acronym{ASCII} letter or @samp{$}, @samp{%} or
905 period.
907 You can also include certain special @dfn{character classes} in a
908 character set.  A @samp{[:} and balancing @samp{:]} enclose a
909 character class inside a character alternative.  For instance,
910 @samp{[[:alnum:]]} matches any letter or digit.  @xref{Char Classes,,,
911 elisp, The Emacs Lisp Reference Manual}, for a list of character
912 classes.
914 To include a @samp{]} in a character set, you must make it the first
915 character.  For example, @samp{[]a]} matches @samp{]} or @samp{a}.  To
916 include a @samp{-}, write @samp{-} as the first or last character of the
917 set, or put it after a range.  Thus, @samp{[]-]} matches both @samp{]}
918 and @samp{-}.
920 To include @samp{^} in a set, put it anywhere but at the beginning of
921 the set.  (At the beginning, it complements the set---see below.)
923 When you use a range in case-insensitive search, you should write both
924 ends of the range in upper case, or both in lower case, or both should
925 be non-letters.  The behavior of a mixed-case range such as @samp{A-z}
926 is somewhat ill-defined, and it may change in future Emacs versions.
928 @item @kbd{[^ @dots{} ]}
929 @samp{[^} begins a @dfn{complemented character set}, which matches any
930 character except the ones specified.  Thus, @samp{[^a-z0-9A-Z]} matches
931 all characters @emph{except} @acronym{ASCII} letters and digits.
933 @samp{^} is not special in a character set unless it is the first
934 character.  The character following the @samp{^} is treated as if it
935 were first (in other words, @samp{-} and @samp{]} are not special there).
937 A complemented character set can match a newline, unless newline is
938 mentioned as one of the characters not to match.  This is in contrast to
939 the handling of regexps in programs such as @code{grep}.
941 @item @kbd{^}
942 is a special character that matches the empty string, but only at the
943 beginning of a line in the text being matched.  Otherwise it fails to
944 match anything.  Thus, @samp{^foo} matches a @samp{foo} that occurs at
945 the beginning of a line.
947 For historical compatibility reasons, @samp{^} can be used with this
948 meaning only at the beginning of the regular expression, or after
949 @samp{\(} or @samp{\|}.
951 @item @kbd{$}
952 is similar to @samp{^} but matches only at the end of a line.  Thus,
953 @samp{x+$} matches a string of one @samp{x} or more at the end of a line.
955 For historical compatibility reasons, @samp{$} can be used with this
956 meaning only at the end of the regular expression, or before @samp{\)}
957 or @samp{\|}.
959 @item @kbd{\}
960 has two functions: it quotes the special characters (including
961 @samp{\}), and it introduces additional special constructs.
963 Because @samp{\} quotes special characters, @samp{\$} is a regular
964 expression that matches only @samp{$}, and @samp{\[} is a regular
965 expression that matches only @samp{[}, and so on.
967 See the following section for the special constructs that begin
968 with @samp{\}.
969 @end table
971   Note: for historical compatibility, special characters are treated as
972 ordinary ones if they are in contexts where their special meanings make no
973 sense.  For example, @samp{*foo} treats @samp{*} as ordinary since there is
974 no preceding expression on which the @samp{*} can act.  It is poor practice
975 to depend on this behavior; it is better to quote the special character anyway,
976 regardless of where it appears.
978 As a @samp{\} is not special inside a character alternative, it can
979 never remove the special meaning of @samp{-} or @samp{]}.  So you
980 should not quote these characters when they have no special meaning
981 either.  This would not clarify anything, since backslashes can
982 legitimately precede these characters where they @emph{have} special
983 meaning, as in @samp{[^\]} (@code{"[^\\]"} for Lisp string syntax),
984 which matches any single character except a backslash.
986 @node Regexp Backslash
987 @section Backslash in Regular Expressions
989   For the most part, @samp{\} followed by any character matches only
990 that character.  However, there are several exceptions: two-character
991 sequences starting with @samp{\} that have special meanings.  The
992 second character in the sequence is always an ordinary character when
993 used on its own.  Here is a table of @samp{\} constructs.
995 @table @kbd
996 @item \|
997 specifies an alternative.  Two regular expressions @var{a} and @var{b}
998 with @samp{\|} in between form an expression that matches some text if
999 either @var{a} matches it or @var{b} matches it.  It works by trying to
1000 match @var{a}, and if that fails, by trying to match @var{b}.
1002 Thus, @samp{foo\|bar} matches either @samp{foo} or @samp{bar}
1003 but no other string.
1005 @samp{\|} applies to the largest possible surrounding expressions.  Only a
1006 surrounding @samp{\( @dots{} \)} grouping can limit the grouping power of
1007 @samp{\|}.
1009 Full backtracking capability exists to handle multiple uses of @samp{\|}.
1011 @item \( @dots{} \)
1012 is a grouping construct that serves three purposes:
1014 @enumerate
1015 @item
1016 To enclose a set of @samp{\|} alternatives for other operations.
1017 Thus, @samp{\(foo\|bar\)x} matches either @samp{foox} or @samp{barx}.
1019 @item
1020 To enclose a complicated expression for the postfix operators @samp{*},
1021 @samp{+} and @samp{?} to operate on.  Thus, @samp{ba\(na\)*} matches
1022 @samp{bananana}, etc., with any (zero or more) number of @samp{na}
1023 strings.
1025 @item
1026 To record a matched substring for future reference.
1027 @end enumerate
1029 This last application is not a consequence of the idea of a
1030 parenthetical grouping; it is a separate feature that is assigned as a
1031 second meaning to the same @w{@samp{\( @dots{} \)}} construct.  In practice
1032 there is usually no conflict between the two meanings; when there is
1033 a conflict, you can use a shy group, described below.
1035 @item \(?: @dots{} \)
1036 @cindex shy group, in regexp
1037 specifies a @dfn{shy group} that does not record the matched substring;
1038 you can't refer back to it with @samp{\@var{d}} (see below).  This is
1039 useful in mechanically combining regular expressions, so that you can
1040 add groups for syntactic purposes without interfering with the
1041 numbering of the groups that are meant to be referred to.
1043 @item \@var{d}
1044 @cindex back reference, in regexp
1045 matches the same text that matched the @var{d}th occurrence of a
1046 @samp{\( @dots{} \)} construct.  This is called a @dfn{back
1047 reference}.
1049 After the end of a @samp{\( @dots{} \)} construct, the matcher remembers
1050 the beginning and end of the text matched by that construct.  Then,
1051 later on in the regular expression, you can use @samp{\} followed by the
1052 digit @var{d} to mean ``match the same text matched the @var{d}th time
1053 by the @samp{\( @dots{} \)} construct''.
1055 The strings matching the first nine @samp{\( @dots{} \)} constructs
1056 appearing in a regular expression are assigned numbers 1 through 9 in
1057 the order that the open-parentheses appear in the regular expression.
1058 So you can use @samp{\1} through @samp{\9} to refer to the text matched
1059 by the corresponding @samp{\( @dots{} \)} constructs.
1061 For example, @samp{\(.*\)\1} matches any newline-free string that is
1062 composed of two identical halves.  The @samp{\(.*\)} matches the first
1063 half, which may be anything, but the @samp{\1} that follows must match
1064 the same exact text.
1066 If a particular @samp{\( @dots{} \)} construct matches more than once
1067 (which can easily happen if it is followed by @samp{*}), only the last
1068 match is recorded.
1070 @item \`
1071 matches the empty string, but only at the beginning of the string or
1072 buffer (or its accessible portion) being matched against.
1074 @item \'
1075 matches the empty string, but only at the end of the string or buffer
1076 (or its accessible portion) being matched against.
1078 @item \=
1079 matches the empty string, but only at point.
1081 @item \b
1082 matches the empty string, but only at the beginning or
1083 end of a word.  Thus, @samp{\bfoo\b} matches any occurrence of
1084 @samp{foo} as a separate word.  @samp{\bballs?\b} matches
1085 @samp{ball} or @samp{balls} as a separate word.
1087 @samp{\b} matches at the beginning or end of the buffer
1088 regardless of what text appears next to it.
1090 @item \B
1091 matches the empty string, but @emph{not} at the beginning or
1092 end of a word.
1094 @item \<
1095 matches the empty string, but only at the beginning of a word.
1096 @samp{\<} matches at the beginning of the buffer only if a
1097 word-constituent character follows.
1099 @item \>
1100 matches the empty string, but only at the end of a word.  @samp{\>}
1101 matches at the end of the buffer only if the contents end with a
1102 word-constituent character.
1104 @item \w
1105 matches any word-constituent character.  The syntax table determines
1106 which characters these are.  @xref{Syntax Tables,, Syntax Tables,
1107 elisp, The Emacs Lisp Reference Manual}.
1109 @item \W
1110 matches any character that is not a word-constituent.
1112 @item \_<
1113 matches the empty string, but only at the beginning of a symbol.
1114 A symbol is a sequence of one or more symbol-constituent characters.
1115 A symbol-constituent character is a character whose syntax is either
1116 @samp{w} or @samp{_}.  @samp{\_<} matches at the beginning of the
1117 buffer only if a symbol-constituent character follows.
1119 @item \_>
1120 matches the empty string, but only at the end of a symbol.  @samp{\_>}
1121 matches at the end of the buffer only if the contents end with a
1122 symbol-constituent character.
1124 @item \s@var{c}
1125 matches any character whose syntax is @var{c}.  Here @var{c} is a
1126 character that designates a particular syntax class: thus, @samp{w}
1127 for word constituent, @samp{-} or @samp{ } for whitespace, @samp{.}
1128 for ordinary punctuation, etc.  @xref{Syntax Tables,, Syntax Tables,
1129 elisp, The Emacs Lisp Reference Manual}.
1131 @item \S@var{c}
1132 matches any character whose syntax is not @var{c}.
1134 @cindex categories of characters
1135 @cindex characters which belong to a specific language
1136 @findex describe-categories
1137 @item \c@var{c}
1138 matches any character that belongs to the category @var{c}.  For
1139 example, @samp{\cc} matches Chinese characters, @samp{\cg} matches
1140 Greek characters, etc.  For the description of the known categories,
1141 type @kbd{M-x describe-categories @key{RET}}.
1143 @item \C@var{c}
1144 matches any character that does @emph{not} belong to category
1145 @var{c}.
1146 @end table
1148   The constructs that pertain to words and syntax are controlled by
1149 the setting of the syntax table.  @xref{Syntax Tables,, Syntax Tables,
1150 elisp, The Emacs Lisp Reference Manual}.
1152 @node Regexp Example
1153 @section Regular Expression Example
1155   Here is an example of a regexp---similar to the regexp that Emacs
1156 uses, by default, to recognize the end of a sentence, not including
1157 the following space (i.e., the variable @code{sentence-end-base}):
1159 @example
1160 @verbatim
1161 [.?!][]\"')}]*
1162 @end verbatim
1163 @end example
1165 @noindent
1166 This contains two parts in succession: a character set matching
1167 period, @samp{?}, or @samp{!}, and a character set matching
1168 close-brackets, quotes, or parentheses, repeated zero or more times.
1170 @node Lax Search
1171 @section Lax Matching During Searching
1173 @cindex lax search
1174 @cindex character equivalence in search
1175   Normally, you'd want search commands to disregard certain minor
1176 differences between the search string you type and the text being
1177 searched.  For example, sequences of whitespace characters of
1178 different length are usually perceived as equivalent; letter-case
1179 differences usually don't matter; etc.  This is known as
1180 @dfn{character equivalence}.
1182   This section describes the Emacs lax search features, and how to
1183 tailor them to your needs.
1185 @cindex lax space matching in search
1186 @kindex M-s SPC @r{(Incremental search)}
1187 @kindex SPC @r{(Incremental search)}
1188 @findex isearch-toggle-lax-whitespace
1189 @vindex search-whitespace-regexp
1190   By default, search commands perform @dfn{lax space matching}:
1191 each space, or sequence of spaces, matches any sequence of one or more
1192 whitespace characters in the text.  (Incremental regexp search has a
1193 separate default; see @ref{Regexp Search}.)  Hence, @w{@samp{foo bar}}
1194 matches @w{@samp{foo bar}}, @w{@samp{foo@ @ bar}},
1195 @w{@samp{foo@ @ @ bar}}, and so on (but not @samp{foobar}).  More
1196 precisely, Emacs matches each sequence of space characters in the
1197 search string to a regular expression specified by the variable
1198 @code{search-whitespace-regexp}.  For example, to make spaces match
1199 sequences of newlines as well as spaces, set it to
1200 @samp{"[[:space:]\n]+"}.  The default value of this variable depends
1201 on the buffer's major mode; most major modes classify spaces, tabs,
1202 and formfeed characters as whitespace.
1204   If you want whitespace characters to match exactly, you can turn lax
1205 space matching off by typing @kbd{M-s @key{SPC}}
1206 (@code{isearch-toggle-lax-whitespace}) within an incremental search.
1207 Another @kbd{M-s @key{SPC}} turns lax space matching back on.  To
1208 disable lax whitespace matching for all searches, change
1209 @code{search-whitespace-regexp} to @code{nil}; then each space in the
1210 search string matches exactly one space.
1212 @cindex case folding in search
1213 @cindex case-sensitivity and search
1214   Searches in Emacs by default ignore the case of the text they are
1215 searching through, if you specify the search string in lower case.
1216 Thus, if you specify searching for @samp{foo}, then @samp{Foo} and
1217 @samp{foo} also match.  Regexps, and in particular character sets,
1218 behave likewise: @samp{[ab]} matches @samp{a} or @samp{A} or @samp{b}
1219 or @samp{B}.  This feature is known as @dfn{case folding}, and it is
1220 supported in both incremental and non-incremental search modes.
1222 @vindex search-upper-case
1223   An upper-case letter anywhere in the search string makes the search
1224 case-sensitive.  Thus, searching for @samp{Foo} does not find
1225 @samp{foo} or @samp{FOO}.  This applies to regular expression search
1226 as well as to literal string search.  The effect ceases if you delete
1227 the upper-case letter from the search string.  The variable
1228 @code{search-upper-case} controls this: if it is non-@code{nil} (the
1229 default), an upper-case character in the search string make the search
1230 case-sensitive; setting it to @code{nil} disables this effect of
1231 upper-case characters.
1233 @vindex case-fold-search
1234   If you set the variable @code{case-fold-search} to @code{nil}, then
1235 all letters must match exactly, including case.  This is a per-buffer
1236 variable; altering the variable normally affects only the current buffer,
1237 unless you change its default value.  @xref{Locals}.
1238 This variable applies to nonincremental searches also, including those
1239 performed by the replace commands (@pxref{Replace}) and the minibuffer
1240 history matching commands (@pxref{Minibuffer History}).
1242 @kindex M-c @r{(Incremental search)}
1243 @kindex M-s c @r{(Incremental search)}
1244 @findex isearch-toggle-case-fold
1245   Typing @kbd{M-c} or @kbd{M-s c} (@code{isearch-toggle-case-fold})
1246 within an incremental search toggles the case sensitivity of that
1247 search.  The effect does not extend beyond the current incremental
1248 search, but it does override the effect of adding or removing an
1249 upper-case letter in the current search.
1251   Several related variables control case-sensitivity of searching and
1252 matching for specific commands or activities.  For instance,
1253 @code{tags-case-fold-search} controls case sensitivity for
1254 @code{find-tag}.  To find these variables, do @kbd{M-x
1255 apropos-variable @key{RET} case-fold-search @key{RET}}.
1257 @cindex character folding in search
1258 @cindex equivalent character sequences
1259   Case folding disregards case distinctions among characters, making
1260 upper-case characters match lower-case variants, and vice versa.  A
1261 generalization of case folding is @dfn{character folding}, which
1262 disregards wider classes of distinctions among similar characters.
1263 For instance, under character folding the letter @code{a} matches all
1264 of its accented cousins like @code{@"a} and @code{@'a}, i.e., the
1265 match disregards the diacritics that distinguish these
1266 variants.  In addition, @code{a} matches other characters that
1267 resemble it, or have it as part of their graphical representation,
1268 such as @sc{u+249c parenthesized latin small letter a} and @sc{u+2100
1269 account of} (which looks like a small @code{a} over @code{c}).
1270 Similarly, the @acronym{ASCII} double-quote character @code{"} matches
1271 all the other variants of double quotes defined by the Unicode
1272 standard.  Finally, character folding can make a sequence of one or
1273 more characters match another sequence of a different length: for
1274 example, the sequence of two characters @code{ff} matches @sc{u+fb00
1275 latin small ligature ff}.  Character sequences that are not identical,
1276 but match under character folding are known as @dfn{equivalent
1277 character sequences}.
1279 @kindex M-s ' @r{(Incremental Search)}
1280 @findex isearch-toggle-char-fold
1281   Generally, search commands in Emacs do not by default perform
1282 character folding in order to match equivalent character sequences.
1283 You can enable this behavior by customizing the variable
1284 @code{search-default-mode} to @code{char-fold-to-regexp}.
1285 @xref{Search Customizations}.  Within an incremental search, typing
1286 @kbd{M-s '} (@code{isearch-toggle-char-fold}) toggles character
1287 folding, but only for that search.  (Replace commands have a different
1288 default, controlled by a separate option; see @ref{Replacement and Lax
1289 Matches}.)
1291   Like with case folding, typing an explicit variant of a character,
1292 such as @code{@"a}, as part of the search string disables character
1293 folding for that search.  If you delete such a character from the
1294 search string, this effect ceases.
1296 @node Replace
1297 @section Replacement Commands
1298 @cindex replacement
1299 @cindex search-and-replace commands
1300 @cindex string substitution
1301 @cindex global substitution
1303   Emacs provides several commands for performing search-and-replace
1304 operations.  In addition to the simple @kbd{M-x replace-string}
1305 command, there is @kbd{M-%} (@code{query-replace}), which presents
1306 each occurrence of the search pattern and asks you whether to replace
1309   The replace commands normally operate on the text from point to the
1310 end of the buffer.  When the region is active, they operate on it
1311 instead (@pxref{Mark}).  The basic replace commands replace one
1312 @dfn{search string} (or regexp) with one @dfn{replacement string}.  It
1313 is possible to perform several replacements in parallel, using the
1314 command @code{expand-region-abbrevs} (@pxref{Expanding Abbrevs}).
1316 @menu
1317 * Unconditional Replace::   Replacing all matches for a string.
1318 * Regexp Replace::          Replacing all matches for a regexp.
1319 * Replacement and Lax Matches::
1320                             Lax searching for text to replace.
1321 * Query Replace::           How to use querying.
1322 @end menu
1324 @node Unconditional Replace
1325 @subsection Unconditional Replacement
1326 @findex replace-string
1328 @table @kbd
1329 @item M-x replace-string @key{RET} @var{string} @key{RET} @var{newstring} @key{RET}
1330 Replace every occurrence of @var{string} with @var{newstring}.
1331 @end table
1333   To replace every instance of @samp{foo} after point with @samp{bar},
1334 use the command @kbd{M-x replace-string} with the two arguments
1335 @samp{foo} and @samp{bar}.  Replacement happens only in the text after
1336 point, so if you want to cover the whole buffer you must go to the
1337 beginning first.  All occurrences up to the end of the buffer are
1338 replaced; to limit replacement to part of the buffer, activate the
1339 region around that part.  When the region is active, replacement is
1340 limited to the region (@pxref{Mark}).
1342   When @code{replace-string} exits, it leaves point at the last
1343 occurrence replaced.  It adds the prior position of point (where the
1344 @code{replace-string} command was issued) to the mark ring, without
1345 activating the mark; use @kbd{C-u C-@key{SPC}} to move back there.
1346 @xref{Mark Ring}.
1348   A prefix argument restricts replacement to matches that are
1349 surrounded by word boundaries.
1351   @xref{Replacement and Lax Matches}, for details about
1352 case-sensitivity in replace commands.
1354 @node Regexp Replace
1355 @subsection Regexp Replacement
1356 @findex replace-regexp
1358   The @kbd{M-x replace-string} command replaces exact matches for a
1359 single string.  The similar command @kbd{M-x replace-regexp} replaces
1360 any match for a specified regular expression pattern (@pxref{Regexps}).
1362 @table @kbd
1363 @item M-x replace-regexp @key{RET} @var{regexp} @key{RET} @var{newstring} @key{RET}
1364 Replace every match for @var{regexp} with @var{newstring}.
1365 @end table
1367 @cindex back reference, in regexp replacement
1368   In @code{replace-regexp}, the @var{newstring} need not be constant:
1369 it can refer to all or part of what is matched by the @var{regexp}.
1370 @samp{\&} in @var{newstring} stands for the entire match being
1371 replaced.  @samp{\@var{d}} in @var{newstring}, where @var{d} is a
1372 digit starting from 1, stands for whatever matched the @var{d}th
1373 parenthesized grouping in @var{regexp}.  (This is called a ``back
1374 reference''.)  @samp{\#} refers to the count of replacements already
1375 made in this command, as a decimal number.  In the first replacement,
1376 @samp{\#} stands for @samp{0}; in the second, for @samp{1}; and so on.
1377 For example,
1379 @example
1380 M-x replace-regexp @key{RET} c[ad]+r @key{RET} \&-safe @key{RET}
1381 @end example
1383 @noindent
1384 replaces (for example) @samp{cadr} with @samp{cadr-safe} and @samp{cddr}
1385 with @samp{cddr-safe}.
1387 @example
1388 M-x replace-regexp @key{RET} \(c[ad]+r\)-safe @key{RET} \1 @key{RET}
1389 @end example
1391 @noindent
1392 performs the inverse transformation.  To include a @samp{\} in the
1393 text to replace with, you must enter @samp{\\}.
1395   If you want to enter part of the replacement string by hand each
1396 time, use @samp{\?} in the replacement string.  Each replacement will
1397 ask you to edit the replacement string in the minibuffer, putting
1398 point where the @samp{\?} was.
1400   The remainder of this subsection is intended for specialized tasks
1401 and requires knowledge of Lisp.  Most readers can skip it.
1403   You can use Lisp expressions to calculate parts of the
1404 replacement string.  To do this, write @samp{\,} followed by the
1405 expression in the replacement string.  Each replacement calculates the
1406 value of the expression and converts it to text without quoting (if
1407 it's a string, this means using the string's contents), and uses it in
1408 the replacement string in place of the expression itself.  If the
1409 expression is a symbol, one space in the replacement string after the
1410 symbol name goes with the symbol name, so the value replaces them
1411 both.
1413   Inside such an expression, you can use some special sequences.
1414 @samp{\&} and @samp{\@var{n}} refer here, as usual, to the entire
1415 match as a string, and to a submatch as a string.  @var{n} may be
1416 multiple digits, and the value of @samp{\@var{n}} is @code{nil} if
1417 subexpression @var{n} did not match.  You can also use @samp{\#&} and
1418 @samp{\#@var{n}} to refer to those matches as numbers (this is valid
1419 when the match or submatch has the form of a numeral).  @samp{\#} here
1420 too stands for the number of already-completed replacements.
1422   Repeating our example to exchange @samp{x} and @samp{y}, we can thus
1423 do it also this way:
1425 @example
1426 M-x replace-regexp @key{RET} \(x\)\|y @key{RET}
1427 \,(if \1 "y" "x") @key{RET}
1428 @end example
1430   For computing replacement strings for @samp{\,}, the @code{format}
1431 function is often useful (@pxref{Formatting Strings,,, elisp, The Emacs
1432 Lisp Reference Manual}).  For example, to add consecutively numbered
1433 strings like @samp{ABC00042} to columns 73 @w{to 80} (unless they are
1434 already occupied), you can use
1436 @example
1437 M-x replace-regexp @key{RET} ^.\@{0,72\@}$ @key{RET}
1438 \,(format "%-72sABC%05d" \& \#) @key{RET}
1439 @end example
1441 @node Replacement and Lax Matches
1442 @subsection Replace Commands and Lax Matches
1444   This subsection describes the behavior of replace commands with
1445 respect to lax matches (@pxref{Lax Search}) and how to customize it.
1446 In general, replace commands mostly default to stricter matching than
1447 their search counterparts.
1449 @cindex lax space matching in replace commands
1450 @vindex replace-lax-whitespace
1451   Unlike incremental search, the replacement commands do not use lax
1452 space matching (@pxref{Lax Search, lax space matching}) by default.
1453 To enable lax space matching for replacement, change the variable
1454 @code{replace-lax-whitespace} to non-@code{nil}.  (This only affects
1455 how Emacs finds the text to replace, not the replacement text.)
1457 @vindex replace-regexp-lax-whitespace
1458   A companion variable @code{replace-regexp-lax-whitespace} controls
1459 whether @code{query-replace-regexp} uses lax whitespace matching when
1460 searching for patterns.
1462 @cindex case folding in replace commands
1463   If the first argument of a replace command is all lower case, the
1464 command ignores case while searching for occurrences to
1465 replace---provided @code{case-fold-search} is non-@code{nil}.  If
1466 @code{case-fold-search} is set to @code{nil}, case is always significant
1467 in all searches.
1469 @vindex case-replace
1470   In addition, when the @var{newstring} argument is all or partly lower
1471 case, replacement commands try to preserve the case pattern of each
1472 occurrence.  Thus, the command
1474 @example
1475 M-x replace-string @key{RET} foo @key{RET} bar @key{RET}
1476 @end example
1478 @noindent
1479 replaces a lower case @samp{foo} with a lower case @samp{bar}, an
1480 all-caps @samp{FOO} with @samp{BAR}, and a capitalized @samp{Foo} with
1481 @samp{Bar}.  (These three alternatives---lower case, all caps, and
1482 capitalized, are the only ones that @code{replace-string} can
1483 distinguish.)
1485   If upper-case letters are used in the replacement string, they remain
1486 upper case every time that text is inserted.  If upper-case letters are
1487 used in the first argument, the second argument is always substituted
1488 exactly as given, with no case conversion.  Likewise, if either
1489 @code{case-replace} or @code{case-fold-search} is set to @code{nil},
1490 replacement is done without case conversion.
1492 @cindex character folding in replace commands
1493   The replacement commands by default do not use character folding
1494 (@pxref{Lax Search, character folding}) when looking for the text to
1495 replace.  To enable character folding for matching in
1496 @code{query-replace} and @code{replace-string}, set the variable
1497 @code{replace-char-fold} to a non-@code{nil} value.  (This
1498 setting does not affect the replacement text, only how Emacs finds the
1499 text to replace.  It also doesn't affect @code{replace-regexp}.)
1501 @node Query Replace
1502 @subsection Query Replace
1503 @cindex query replace
1505 @table @kbd
1506 @item M-% @var{string} @key{RET} @var{newstring} @key{RET}
1507 Replace some occurrences of @var{string} with @var{newstring}.
1508 @item C-M-% @var{regexp} @key{RET} @var{newstring} @key{RET}
1509 Replace some matches for @var{regexp} with @var{newstring}.
1510 @end table
1512 @kindex M-%
1513 @findex query-replace
1514   If you want to change only some of the occurrences of @samp{foo} to
1515 @samp{bar}, not all of them, use @kbd{M-%} (@code{query-replace}).
1516 This command finds occurrences of @samp{foo} one by one, displays each
1517 occurrence and asks you whether to replace it.  Aside from querying,
1518 @code{query-replace} works just like @code{replace-string}
1519 (@pxref{Unconditional Replace}).  In particular, it preserves case
1520 provided @code{case-replace} is non-@code{nil}, as it normally is
1521 (@pxref{Replacement and Lax Matches}).  A numeric argument means to
1522 consider only occurrences that are bounded by word-delimiter
1523 characters.  A negative prefix argument replaces backward.
1525 @kindex C-M-%
1526 @findex query-replace-regexp
1527   @kbd{C-M-%} performs regexp search and replace (@code{query-replace-regexp}).
1528 It works like @code{replace-regexp} except that it queries
1529 like @code{query-replace}.
1531 @vindex query-replace-from-to-separator
1532   You can reuse earlier replacements with these commands.  When
1533 @code{query-replace} or @code{query-replace-regexp} prompts for the
1534 search string, use @kbd{M-p} and @kbd{M-n} to show previous
1535 replacements in the form @samp{@var{from} -> @var{to}}, where
1536 @var{from} is the search pattern, @var{to} is its replacement, and the
1537 separator between them is determined by the value of the variable
1538 @code{query-replace-from-to-separator}.  Type @key{RET} to select the
1539 desired replacement.  If the value of this variable is @code{nil},
1540 replacements are not added to the command history, and cannot be
1541 reused.
1543 @cindex faces for highlighting query replace
1544 @cindex query-replace face
1545 @cindex lazy-highlight face, in replace
1546 @vindex query-replace-highlight
1547 @vindex query-replace-lazy-highlight
1548 @vindex query-replace-show-replacement
1549   These commands highlight the current match using the face
1550 @code{query-replace}.  You can disable this highlight by setting the
1551 variable @code{query-replace-highlight} to @code{nil}.  They highlight
1552 other matches using @code{lazy-highlight} just like incremental search
1553 (@pxref{Incremental Search}); this can be disabled by setting
1554 @code{query-replace-lazy-highlight} to @code{nil}.  By default,
1555 @code{query-replace-regexp} will show the substituted replacement
1556 string for the current match in the minibuffer.  If you want to keep
1557 special sequences @samp{\&} and @samp{\@var{n}} unexpanded, customize
1558 @code{query-replace-show-replacement} variable.
1560 @vindex query-replace-skip-read-only
1561   The variable @code{query-replace-skip-read-only}, if set
1562 non-@code{nil}, will cause replacement commands to ignore matches in
1563 read-only text.  The default is not to ignore them.
1565   The characters you can type when you are shown a match for the string
1566 or regexp are:
1568 @ignore @c Not worth it.
1569 @kindex SPC @r{(query-replace)}
1570 @kindex DEL @r{(query-replace)}
1571 @kindex , @r{(query-replace)}
1572 @kindex RET @r{(query-replace)}
1573 @kindex . @r{(query-replace)}
1574 @kindex ! @r{(query-replace)}
1575 @kindex ^ @r{(query-replace)}
1576 @kindex C-r @r{(query-replace)}
1577 @kindex C-w @r{(query-replace)}
1578 @kindex C-l @r{(query-replace)}
1579 @end ignore
1581 @c WideCommands
1582 @table @kbd
1583 @item @key{SPC}
1584 @itemx y
1585 to replace the occurrence with @var{newstring}.
1587 @item @key{DEL}
1588 @itemx @key{Delete}
1589 @itemx @key{BACKSPACE}
1590 @itemx n
1591 to skip to the next occurrence without replacing this one.
1593 @item , @r{(Comma)}
1594 to replace this occurrence and display the result.  You are then asked
1595 for another input character to say what to do next.  Since the
1596 replacement has already been made, @key{DEL} and @key{SPC} are
1597 equivalent in this situation; both move to the next occurrence.
1599 You can type @kbd{C-r} at this point (see below) to alter the replaced
1600 text.  You can also type @kbd{C-x u} to undo the replacement; this exits
1601 the @code{query-replace}, so if you want to do further replacement you
1602 must use @kbd{C-x @key{ESC} @key{ESC} @key{RET}} to restart
1603 (@pxref{Repetition}).
1605 @item @key{RET}
1606 @itemx q
1607 to exit without doing any more replacements.
1609 @item .@: @r{(Period)}
1610 to replace this occurrence and then exit without searching for more
1611 occurrences.
1613 @item !
1614 to replace all remaining occurrences without asking again.
1616 @item ^
1617 to go back to the position of the previous occurrence (or what used to
1618 be an occurrence), in case you changed it by mistake or want to
1619 reexamine it.
1621 @item u
1622 to undo the last replacement and go back to where that replacement was
1623 made.
1625 @item U
1626 to undo all the replacements and go back to where the first
1627 replacement was made.
1629 @item C-r
1630 to enter a recursive editing level, in case the occurrence needs to be
1631 edited rather than just replaced with @var{newstring}.  When you are
1632 done, exit the recursive editing level with @kbd{C-M-c} to proceed to
1633 the next occurrence.  @xref{Recursive Edit}.
1635 @item C-w
1636 to delete the occurrence, and then enter a recursive editing level as in
1637 @kbd{C-r}.  Use the recursive edit to insert text to replace the deleted
1638 occurrence of @var{string}.  When done, exit the recursive editing level
1639 with @kbd{C-M-c} to proceed to the next occurrence.
1641 @item e
1642 to edit the replacement string in the minibuffer.  When you exit the
1643 minibuffer by typing @key{RET}, the minibuffer contents replace the
1644 current occurrence of the pattern.  They also become the new
1645 replacement string for any further occurrences.
1647 @item C-l
1648 to redisplay the screen.  Then you must type another character to
1649 specify what to do with this occurrence.
1651 @item Y @r{(Upper-case)}
1652 to replace all remaining occurrences in all remaining buffers in
1653 multi-buffer replacements (like the Dired @key{Q} command that performs
1654 query replace on selected files).  It answers this question and all
1655 subsequent questions in the series with ``yes'', without further
1656 user interaction.
1658 @item N @r{(Upper-case)}
1659 to skip to the next buffer in multi-buffer replacements without
1660 replacing remaining occurrences in the current buffer.  It answers
1661 this question ``no'', gives up on the questions for the current buffer,
1662 and continues to the next buffer in the sequence.
1664 @item C-h
1665 @itemx ?
1666 @itemx @key{F1}
1667 to display a message summarizing these options.  Then you must type
1668 another character to specify what to do with this occurrence.
1669 @end table
1671   Aside from this, any other character exits the @code{query-replace},
1672 and is then reread as part of a key sequence.  Thus, if you type
1673 @kbd{C-k}, it exits the @code{query-replace} and then kills to end of
1674 line.  In particular, @kbd{C-g} simply exits the @code{query-replace}.
1676   To restart a @code{query-replace} once it is exited, use @kbd{C-x
1677 @key{ESC} @key{ESC}}, which repeats the @code{query-replace} because it
1678 used the minibuffer to read its arguments.  @xref{Repetition, C-x @key{ESC}
1679 @key{ESC}}.
1681 @cindex invisible text, and query-replace
1682   The option @code{search-invisible} determines how @code{query-replace}
1683 treats invisible text.  @xref{Outline Search}.
1685   @xref{Operating on Files}, for the Dired @kbd{Q} command which
1686 performs query replace on selected files.  See also @ref{Transforming
1687 File Names}, for Dired commands to rename, copy, or link files by
1688 replacing regexp matches in file names.
1690 @node Other Repeating Search
1691 @section Other Search-and-Loop Commands
1693   Here are some other commands that find matches for a regular
1694 expression.  They all ignore case in matching, if the pattern contains
1695 no upper-case letters and @code{case-fold-search} is non-@code{nil}.
1696 Aside from @code{multi-occur} and @code{multi-occur-in-matching-buffers},
1697 which always search the whole buffer, all operate on the text from point
1698 to the end of the buffer, or on the region if it is active.
1700 @table @kbd
1701 @findex multi-isearch-buffers
1702 @cindex isearch multiple buffers
1703 @cindex multiple-buffer isearch
1704 @item M-x multi-isearch-buffers
1705 Prompt for one or more buffer names, ending with @key{RET}; then,
1706 begin a multi-buffer incremental search in those buffers.  (If the
1707 search fails in one buffer, the next @kbd{C-s} tries searching the
1708 next specified buffer, and so forth.)  With a prefix argument, prompt
1709 for a regexp and begin a multi-buffer incremental search in buffers
1710 matching that regexp.
1712 @findex multi-isearch-buffers-regexp
1713 @item M-x multi-isearch-buffers-regexp
1714 This command is just like @code{multi-isearch-buffers}, except it
1715 performs an incremental regexp search.
1717 @findex multi-isearch-files
1718 @cindex isearch multiple files
1719 @cindex multiple-file isearch
1720 @item M-x multi-isearch-files
1721 Prompt for one or more file names, ending with @key{RET}; then,
1722 begin a multi-file incremental search in those files.  (If the
1723 search fails in one file, the next @kbd{C-s} tries searching the
1724 next specified file, and so forth.)  With a prefix argument, prompt
1725 for a regexp and begin a multi-file incremental search in files
1726 matching that regexp.
1728 @findex multi-isearch-files-regexp
1729 @item M-x multi-isearch-files-regexp
1730 This command is just like @code{multi-isearch-files}, except it
1731 performs an incremental regexp search.
1733 In some modes that set the buffer-local variable
1734 @code{multi-isearch-next-buffer-function} (e.g., in Change Log mode)
1735 a multi-file incremental search is activated automatically.
1737 @cindex Occur mode
1738 @cindex mode, Occur
1739 @cindex match (face name)
1740 @vindex list-matching-lines-default-context-lines
1741 @vindex list-matching-lines-jump-to-current-line
1742 @c Too long.
1743 @c @cindex list-matching-lines-current-line-face (face name)
1744 @kindex M-s o
1745 @findex occur
1746 @item M-x occur
1747 @itemx M-s o
1748 Prompt for a regexp, and display a list showing each line in the
1749 buffer that contains a match for it.  If you type @kbd{M-n} at the
1750 prompt, you can reuse search strings from previous incremental
1751 searches.  The text that matched is highlighted using the @code{match}
1752 face.  A numeric argument @var{n} specifies that @var{n} lines of
1753 context are to be displayed before and after each matching line.
1755 The default number of context lines is specified by the variable
1756 @code{list-matching-lines-default-context-lines}.  When
1757 @code{list-matching-lines-jump-to-current-line} is non-@code{nil} the
1758 current line is shown highlighted with face
1759 @code{list-matching-lines-current-line-face} and the point is set at
1760 the first match after such line.
1762 You can also run @kbd{M-s o} when an incremental search is active;
1763 this uses the current search string.
1765 Note that matches for the regexp you type are extended to include
1766 complete lines, and a match that starts before the previous match ends
1767 is not considered a match.
1769 @kindex RET @r{(Occur mode)}
1770 @kindex o @r{(Occur mode)}
1771 @kindex C-o @r{(Occur mode)}
1772 In the @file{*Occur*} buffer, you can click on each entry, or move
1773 point there and type @key{RET}, to visit the corresponding position in
1774 the buffer that was searched.  @kbd{o} and @kbd{C-o} display the match
1775 in another window; @kbd{C-o} does not select it.  Alternatively, you
1776 can use the @kbd{C-x `} (@code{next-error}) command to visit the
1777 occurrences one by one (@pxref{Compilation Mode}).
1779 @cindex Occur Edit mode
1780 @cindex mode, Occur Edit
1781 Typing @kbd{e} in the @file{*Occur*} buffer switches to Occur Edit
1782 mode, in which edits made to the entries are also applied to the text
1783 in the originating buffer.  Type @kbd{C-c C-c} to return to Occur
1784 mode.
1786 @findex list-matching-lines
1787 The command @kbd{M-x list-matching-lines} is a synonym for @kbd{M-x
1788 occur}.
1790 @findex multi-occur
1791 @item M-x multi-occur
1792 This command is just like @code{occur}, except it is able to search
1793 through multiple buffers.  It asks you to specify the buffer names one
1794 by one.
1796 @findex multi-occur-in-matching-buffers
1797 @item M-x multi-occur-in-matching-buffers
1798 This command is similar to @code{multi-occur}, except the buffers to
1799 search are specified by a regular expression that matches visited file
1800 names.  With a prefix argument, it uses the regular expression to
1801 match buffer names instead.
1803 @findex how-many
1804 @item M-x how-many
1805 Prompt for a regexp, and print the number of matches for it in the
1806 buffer after point.  If the region is active, this operates on the
1807 region instead.
1809 @findex flush-lines
1810 @item M-x flush-lines
1811 Prompt for a regexp, and delete each line that contains a match for
1812 it, operating on the text after point.  This command deletes the
1813 current line if it contains a match starting after point.  If the
1814 region is active, it operates on the region instead; if a line
1815 partially contained in the region contains a match entirely contained
1816 in the region, it is deleted.
1818 If a match is split across lines, @code{flush-lines} deletes all those
1819 lines.  It deletes the lines before starting to look for the next
1820 match; hence, it ignores a match starting on the same line at which
1821 another match ended.
1823 @findex keep-lines
1824 @item M-x keep-lines
1825 Prompt for a regexp, and delete each line that @emph{does not} contain
1826 a match for it, operating on the text after point.  If point is not at
1827 the beginning of a line, this command always keeps the current line.
1828 If the region is active, the command operates on the region instead;
1829 it never deletes lines that are only partially contained in the region
1830 (a newline that ends a line counts as part of that line).
1832 If a match is split across lines, this command keeps all those lines.
1833 @end table
1835 @node Search Customizations
1836 @section Tailoring Search to Your Needs
1837 @cindex search customizations
1839   This section describes miscellaneous search-related customizations
1840 not described elsewhere.
1842 @cindex default search mode
1843 @cindex search mode, default
1844   The default search mode for the incremental search is specified by
1845 the variable @code{search-default-mode}.  It can be @code{nil},
1846 @code{t}, or a function.  If it is @code{nil}, the default mode is to
1847 do literal searches without character folding, but with case folding
1848 and lax-whitespace matches as determined by @code{case-fold-search}
1849 and @code{search-whitespace-regexp}, respectively (@pxref{Lax
1850 Search}).  If the value is @code{t}, incremental search defaults to
1851 regexp searches.  The default value specifies a function that only
1852 performs case folding and lax-whitespace matching.
1854 @vindex search-highlight
1855   The current match of an on-going incremental search is highlighted
1856 using the @code{isearch} face.  This highlighting can be disabled by
1857 setting the variable @code{search-highlight} to @code{nil}.
1859 @cindex lazy highlighting customizations
1860 @vindex isearch-lazy-highlight
1861 @cindex lazy-highlight face
1862   The other matches for the search string that are visible on display
1863 are highlighted using the @code{lazy-highlight} face.  Setting the
1864 variable @code{isearch-lazy-highlight} to @code{nil} disables this
1865 highlighting.  Here are some other variables that customize the lazy
1866 highlighting:
1868 @table @code
1869 @item lazy-highlight-initial-delay
1870 Time in seconds to wait before highlighting visible matches.
1872 @item lazy-highlight-interval
1873 Time in seconds between highlighting successive matches.
1875 @item lazy-highlight-max-at-a-time
1876 The maximum number of matches to highlight before checking for input.
1877 A large number can take some time to highlight, so if you want to
1878 continue searching and type @kbd{C-s} or @kbd{C-r} during that time,
1879 Emacs will not respond until it finishes highlighting all those
1880 matches.  Thus, smaller values make Emacs more responsive.
1881 @end table
1883 @vindex search-nonincremental-instead
1884   Normally, entering @key{RET} within incremental search when the
1885 search string is empty launches a nonincremental search.  (Actually,
1886 it lets you edit the search string, and the next @key{RET} does the
1887 search.)  However, if you customize the variable
1888 @code{search-nonincremental-instead} to @code{nil}, typing @key{RET}
1889 will always exit the incremental search, even if the search string is
1890 empty.
1892 @vindex isearch-hide-immediately
1893   By default, incremental search and query-replace commands match
1894 invisible text, but hide any such matches as soon as the current match
1895 moves off the invisible text.  If you customize the variable
1896 @code{isearch-hide-immediately} to @code{nil}, any invisible text
1897 where matches were found stays on display until the search or the
1898 replace command exits.
1900 @cindex search display on slow terminals
1901 @vindex search-slow-speed
1902 @vindex search-slow-window-lines
1903   Searching incrementally on slow terminals, such as displays
1904 connected to remote machines over slow connection, could be annoying
1905 due to the need to redraw large portions of the display as the search
1906 proceeds.  Emacs provides a special display mode for slow terminals,
1907 whereby search pops up a separate small window and displays the text
1908 surrounding the match in that window.  Small windows display faster,
1909 so the annoying effect of slow speed is alleviated.  The variable
1910 @code{search-slow-speed} determines the baud rate threshold below
1911 which Emacs will use this display mode.  The variable
1912 @code{search-slow-window-lines} controls the number of lines in the
1913 window Emacs pops up for displaying the search results; the default is
1914 1 line.  Normally, this window will pop up at the bottom of the window
1915 that displays the buffer where you start searching, but if the value
1916 of @code{search-slow-window-lines} is negative, that means to put the
1917 window at the top and give it the number of lines that is the absolute
1918 value of @code{search-slow-window-lines}.