1 @node Search, Fixit, Display, Top
2 @chapter Searching and Replacement
5 Like other editors, Emacs has commands for searching for occurrences of
6 a string. The principal search command is unusual in that it is
7 @dfn{incremental}: it begins to search before you have finished typing the
8 search string. There are also non-incremental search commands more like
9 those of other editors.
11 Besides the usual @code{replace-string} command that finds all
12 occurrences of one string and replaces them with another, Emacs has a fancy
13 replacement command called @code{query-replace} which asks interactively
14 which occurrences to replace.
17 * Incremental Search:: Search happens as you type the string.
18 * Non-Incremental Search:: Specify entire string and then search.
19 * Word Search:: Search for sequence of words.
20 * Regexp Search:: Search for match for a regexp.
21 * Regexps:: Syntax of regular expressions.
22 * Search Case:: To ignore case while searching, or not.
23 * Replace:: Search, and replace some or all matches.
24 * Other Repeating Search:: Operating on all matches for some regexp.
27 @node Incremental Search, Non-Incremental Search, Search, Search
28 @section Incremental Search
30 An incremental search begins searching as soon as you type the first
31 character of the search string. As you type in the search string, Emacs
32 shows you where the string (as you have typed it so far) is found.
33 When you have typed enough characters to identify the place you want, you
34 can stop. Depending on what you do next, you may or may not need to
35 terminate the search explicitly with a @key{RET}.
40 Incremental search forward (@code{isearch-forward}).
42 Incremental search backward (@code{isearch-backward}).
47 @findex isearch-forward
48 @findex isearch-backward
49 @kbd{C-s} starts an incremental search. @kbd{C-s} reads characters from
50 the keyboard and positions the cursor at the first occurrence of the
51 characters that you have typed. If you type @kbd{C-s} and then @kbd{F},
52 the cursor moves right after the first @samp{F}. Type an @kbd{O}, and see
53 the cursor move to after the first @samp{FO}. After another @kbd{O}, the
54 cursor is after the first @samp{FOO} after the place where you started the
55 search. Meanwhile, the search string @samp{FOO} has been echoed in the
58 The echo area display ends with three dots when actual searching is going
59 on. When search is waiting for more input, the three dots are removed.
60 (On slow terminals, the three dots are not displayed.)
62 If you make a mistake in typing the search string, you can erase
63 characters with @key{DEL}. Each @key{DEL} cancels the last character of the
64 search string. This does not happen until Emacs is ready to read another
65 input character; first it must either find, or fail to find, the character
66 you want to erase. If you do not want to wait for this to happen, use
67 @kbd{C-g} as described below.@refill
69 When you are satisfied with the place you have reached, you can type
70 @key{RET} (or @key{C-m}), which stops searching, leaving the cursor where
71 the search brought it. Any command not specially meaningful in searches also
72 stops the search and is then executed. Thus, typing @kbd{C-a} exits the
73 search and then moves to the beginning of the line. @key{RET} is necessary
74 only if the next command you want to type is a printing character,
75 @key{DEL}, @key{ESC}, or another control character that is special
76 within searches (@kbd{C-q}, @kbd{C-w}, @kbd{C-r}, @kbd{C-s}, or @kbd{C-y}).
78 Sometimes you search for @samp{FOO} and find it, but were actually
79 looking for a different occurrence of it. To move to the next occurrence
80 of the search string, type another @kbd{C-s}. Do this as often as
81 necessary. If you overshoot, you can cancel some @kbd{C-s}
82 characters with @key{DEL}.
84 After you exit a search, you can search for the same string again by
85 typing just @kbd{C-s C-s}: the first @kbd{C-s} is the key that invokes
86 incremental search, and the second @kbd{C-s} means ``search again''.
88 If the specified string is not found at all, the echo area displays
89 the text @samp{Failing I-Search}. The cursor is after the place where
90 Emacs found as much of your string as it could. Thus, if you search for
91 @samp{FOOT}, and there is no @samp{FOOT}, the cursor may be after the
92 @samp{FOO} in @samp{FOOL}. At this point there are several things you
93 can do. If you mistyped the search string, correct it. If you like the
94 place you have found, you can type @key{RET} or some other Emacs command
95 to ``accept what the search offered''. Or you can type @kbd{C-g}, which
96 removes from the search string the characters that could not be found
97 (the @samp{T} in @samp{FOOT}), leaving those that were found (the
98 @samp{FOO} in @samp{FOOT}). A second @kbd{C-g} at that point cancels
99 the search entirely, returning point to where it was when the search
102 If a search is failing and you ask to repeat it by typing another
103 @kbd{C-s}, it starts again from the beginning of the buffer. Repeating
104 a failing backward search with @kbd{C-r} starts again from the end. This
105 is called @dfn{wrapping around}. @samp{Wrapped} appears in the search
106 prompt once this has happened.
108 @cindex quitting (in search)
109 The @kbd{C-g} ``quit'' character does special things during searches;
110 just what it does depends on the status of the search. If the search has
111 found what you specified and is waiting for input, @kbd{C-g} cancels the
112 entire search. The cursor moves back to where you started the search. If
113 @kbd{C-g} is typed when there are characters in the search string that have
114 not been found---because Emacs is still searching for them, or because it
115 has failed to find them---then the search string characters which have not
116 been found are discarded from the search string. The
117 search is now successful and waiting for more input, so a second @kbd{C-g}
118 cancels the entire search.
120 To search for a control character such as @kbd{C-s} or @key{DEL} or
121 @key{ESC}, you must quote it by typing @kbd{C-q} first. This function
122 of @kbd{C-q} is analogous to its meaning as an Emacs command: it causes
123 the following character to be treated the way a graphic character would
124 normally be treated in the same context.
126 To search backwards, you can use @kbd{C-r} instead of @kbd{C-s} to
127 start the search; @kbd{C-r} is the key that runs the command
128 (@code{isearch-backward}) to search backward. You can also use
129 @kbd{C-r} to change from searching forward to searching backwards. Do
130 this if a search fails because the place you started was too far down in the
131 file. Repeated @kbd{C-r} keeps looking for more occurrences backwards.
132 @kbd{C-s} starts going forward again. You can cancel @kbd{C-r} in a
133 search with @key{DEL}.
135 The characters @kbd{C-y} and @kbd{C-w} can be used in incremental search
136 to grab text from the buffer into the search string. This makes it
137 convenient to search for another occurrence of text at point. @kbd{C-w}
138 copies the word after point as part of the search string, advancing
139 point over that word. Another @kbd{C-s} to repeat the search will then
140 search for a string including that word. @kbd{C-y} is similar to @kbd{C-w}
141 but copies the rest of the current line into the search string.
143 The characters @kbd{M-p} and @kbd{M-n} can be used in an incremental
144 search to recall things which you have searched for in the past. A
145 list of the last 16 things you have searched for is retained, and
146 @kbd{M-p} and @kbd{M-n} let you cycle through that ring.
148 The character @kbd{M-@key{TAB}} does completion on the elements in
149 the search history ring. For example, if you know that you have
150 recently searched for the string @code{POTATOE}, you could type
151 @kbd{C-s P O M-@key{TAB}}. If you had searched for other strings
152 beginning with @code{PO} then you would be shown a list of them, and
153 would need to type more to select one.
155 You can change any of the special characters in incremental search via
156 the normal keybinding mechanism: simply add a binding to the
157 @code{isearch-mode-map}. For example, to make the character
158 @kbd{C-b} mean ``search backwards'' while in isearch-mode, do this:
161 (define-key isearch-mode-map "\C-b" 'isearch-repeat-backward)
164 These are the default bindings of isearch-mode:
166 @findex isearch-delete-char
168 @findex isearch-quote-char
169 @findex isearch-repeat-forward
170 @findex isearch-repeat-backward
171 @findex isearch-yank-line
172 @findex isearch-yank-word
173 @findex isearch-abort
174 @findex isearch-ring-retreat
175 @findex isearch-ring-advance
176 @findex isearch-complete
178 @kindex DEL (isearch-mode)
179 @kindex RET (isearch-mode)
180 @kindex C-q (isearch-mode)
181 @kindex C-s (isearch-mode)
182 @kindex C-r (isearch-mode)
183 @kindex C-y (isearch-mode)
184 @kindex C-w (isearch-mode)
185 @kindex C-g (isearch-mode)
186 @kindex M-p (isearch-mode)
187 @kindex M-n (isearch-mode)
188 @kindex M-TAB (isearch-mode)
192 Delete a character from the incremental search string (@code{isearch-delete-char}).
194 Exit incremental search (@code{isearch-exit}).
196 Quote special characters for incremental search (@code{isearch-quote-char}).
198 Repeat incremental search forward (@code{isearch-repeat-forward}).
200 Repeat incremental search backward (@code{isearch-repeat-backward}).
202 Pull rest of line from buffer into search string (@code{isearch-yank-line}).
204 Pull next word from buffer into search string (@code{isearch-yank-word}).
206 Cancels input back to what has been found successfully, or aborts the
207 isearch (@code{isearch-abort}).
209 Recall the previous element in the isearch history ring
210 (@code{isearch-ring-retreat}).
212 Recall the next element in the isearch history ring
213 (@code{isearch-ring-advance}).
215 Do completion on the elements in the isearch history ring
216 (@code{isearch-complete}).
220 Any other character which is normally inserted into a buffer when typed
221 is automatically added to the search string in isearch-mode.
223 @subsection Slow Terminal Incremental Search
225 Incremental search on a slow terminal uses a modified style of display
226 that is designed to take less time. Instead of redisplaying the buffer at
227 each place the search gets to, it creates a new single-line window and uses
228 that to display the line the search has found. The single-line window
229 appears as soon as point gets outside of the text that is already
232 When the search is terminated, the single-line window is removed. Only
233 at this time the window in which the search was done is redisplayed to show
234 its new value of point.
236 The three dots at the end of the search string, normally used to indicate
237 that searching is going on, are not displayed in slow style display.
239 @vindex search-slow-speed
240 The slow terminal style of display is used when the terminal baud rate is
241 less than or equal to the value of the variable @code{search-slow-speed},
244 @vindex search-slow-window-lines
245 The number of lines to use in slow terminal search display is controlled
246 by the variable @code{search-slow-window-lines}. Its normal value is 1.
248 @node Non-Incremental Search, Word Search, Incremental Search, Search
249 @section Non-Incremental Search
250 @cindex non-incremental search
252 Emacs also has conventional non-incremental search commands, which require
253 you type the entire search string before searching begins.
256 @item C-s @key{RET} @var{string} @key{RET}
257 Search for @var{string}.
258 @item C-r @key{RET} @var{string} @key{RET}
259 Search backward for @var{string}.
262 To do a non-incremental search, first type @kbd{C-s @key{RET}}
263 (or @kbd{C-s C-m}). This enters the minibuffer to read the search string.
264 Terminate the string with @key{RET} to start the search. If the string
265 is not found, the search command gets an error.
267 By default, @kbd{C-s} invokes incremental search, but if you give it an
268 empty argument, which would otherwise be useless, it invokes non-incremental
269 search. Therefore, @kbd{C-s @key{RET}} invokes non-incremental search.
270 @kbd{C-r @key{RET}} also works this way.
272 @findex search-forward
273 @findex search-backward
274 Forward and backward non-incremental searches are implemented by the
275 commands @code{search-forward} and @code{search-backward}. You can bind
276 these commands to keys. The reason that incremental
277 search is programmed to invoke them as well is that @kbd{C-s @key{RET}}
278 is the traditional sequence of characters used in Emacs to invoke
279 non-incremental search.
281 Non-incremental searches performed using @kbd{C-s @key{RET}} do
282 not call @code{search-forward} right away. They first check
283 if the next character is @kbd{C-w}, which requests a word search.
288 @node Word Search, Regexp Search, Non-Incremental Search, Search
292 Word search looks for a sequence of words without regard to how the
293 words are separated. More precisely, you type a string of many words,
294 using single spaces to separate them, and the string is found even if
295 there are multiple spaces, newlines or other punctuation between the words.
297 Word search is useful in editing documents formatted by text formatters.
298 If you edit while looking at the printed, formatted version, you can't tell
299 where the line breaks are in the source file. Word search, allows you
300 to search without having to know the line breaks.
303 @item C-s @key{RET} C-w @var{words} @key{RET}
304 Search for @var{words}, ignoring differences in punctuation.
305 @item C-r @key{RET} C-w @var{words} @key{RET}
306 Search backward for @var{words}, ignoring differences in punctuation.
309 Word search is a special case of non-incremental search. It is invoked
310 with @kbd{C-s @key{RET} C-w} followed by the search string, which
311 must always be terminated with another @key{RET}. Being non-incremental, this
312 search does not start until the argument is terminated. It works by
313 constructing a regular expression and searching for that. @xref{Regexp
316 You can do a backward word search with @kbd{C-r @key{RET} C-w}.
318 @findex word-search-forward
319 @findex word-search-backward
320 Forward and backward word searches are implemented by the commands
321 @code{word-search-forward} and @code{word-search-backward}. You can
322 bind these commands to keys. The reason that incremental
323 search is programmed to invoke them as well is that @kbd{C-s @key{RET} C-w}
324 is the traditional Emacs sequence of keys for word search.
326 @node Regexp Search, Regexps, Word Search, Search
327 @section Regular Expression Search
328 @cindex regular expression
331 A @dfn{regular expression} (@dfn{regexp}, for short) is a pattern that
332 denotes a (possibly infinite) set of strings. Searching for matches
333 for a regexp is a powerful operation that editors on Unix systems have
334 traditionally offered.
336 To gain a thorough understanding of regular expressions and how to use
337 them to best advantage, we recommend that you study @cite{Mastering
338 Regular Expressions, by Jeffrey E.F. Friedl, O'Reilly and Associates,
339 1997}. (It's known as the "Hip Owls" book, because of the picture on its
340 cover.) You might also read the manuals to @ref{(gawk)Top},
341 @ref{(ed)Top}, @cite{sed}, @cite{grep}, @ref{(perl)Top},
342 @ref{(regex)Top}, @ref{(rx)Top}, @cite{pcre}, and @ref{(flex)Top}, which
343 also make good use of regular expressions.
345 The SXEmacs regular expression syntax most closely resembles that of
346 @cite{ed}, or @cite{grep}, the GNU versions of which all utilize the GNU
347 @cite{regex} library. SXEmacs' version of @cite{regex} has recently been
348 extended with some Perl--like capabilities, described in the next
351 In SXEmacs, you can search for the next match for a regexp either
352 incrementally or not.
356 @findex isearch-forward-regexp
357 @findex isearch-backward-regexp
358 Incremental search for a regexp is done by typing @kbd{M-C-s}
359 (@code{isearch-forward-regexp}). This command reads a search string
360 incrementally just like @kbd{C-s}, but it treats the search string as a
361 regexp rather than looking for an exact match against the text in the
362 buffer. Each time you add text to the search string, you make the regexp
363 longer, and the new regexp is searched for. A reverse regexp search command
364 @code{isearch-backward-regexp} also exists, bound to @kbd{M-C-r}.
366 All of the control characters that do special things within an ordinary
367 incremental search have the same functionality in incremental regexp search.
368 Typing @kbd{C-s} or @kbd{C-r} immediately after starting a search
369 retrieves the last incremental search regexp used:
370 incremental regexp and non-regexp searches have independent defaults.
372 @findex re-search-forward
373 @findex re-search-backward
374 Non-incremental search for a regexp is done by the functions
375 @code{re-search-forward} and @code{re-search-backward}. You can invoke
376 them with @kbd{M-x} or bind them to keys. You can also call
377 @code{re-search-forward} by way of incremental regexp search with
378 @kbd{M-C-s @key{RET}}; similarly for @code{re-search-backward} with
379 @kbd{M-C-r @key{RET}}.
381 @node Regexps, Search Case, Regexp Search, Search
382 @section Syntax of Regular Expressions
384 Regular expressions have a syntax in which a few characters are
385 special constructs and the rest are @dfn{ordinary}. An ordinary
386 character is a simple regular expression that matches that character and
387 nothing else. The special characters are @samp{.}, @samp{*}, @samp{+},
388 @samp{?}, @samp{[}, @samp{]}, @samp{^}, @samp{$}, and @samp{\}; no new
389 special characters will be defined in the future. Any other character
390 appearing in a regular expression is ordinary, unless a @samp{\}
393 For example, @samp{f} is not a special character, so it is ordinary, and
394 therefore @samp{f} is a regular expression that matches the string
395 @samp{f} and no other string. (It does @emph{not} match the string
396 @samp{ff}.) Likewise, @samp{o} is a regular expression that matches
397 only @samp{o}.@refill
399 Any two regular expressions @var{a} and @var{b} can be concatenated. The
400 result is a regular expression that matches a string if @var{a} matches
401 some amount of the beginning of that string and @var{b} matches the rest of
404 As a simple example, we can concatenate the regular expressions @samp{f}
405 and @samp{o} to get the regular expression @samp{fo}, which matches only
406 the string @samp{fo}. Still trivial. To do something more powerful, you
407 need to use one of the special characters. Here is a list of them:
411 @item .@: @r{(Period)}
412 @cindex @samp{.} in regexp
413 is a special character that matches any single character except a newline.
414 Using concatenation, we can make regular expressions like @samp{a.b}, which
415 matches any three-character string that begins with @samp{a} and ends with
419 @cindex @samp{*} in regexp
420 is not a construct by itself; it is a quantifying suffix operator that
421 means to repeat the preceding regular expression as many times as
422 possible. In @samp{fo*}, the @samp{*} applies to the @samp{o}, so
423 @samp{fo*} matches one @samp{f} followed by any number of @samp{o}s.
424 The case of zero @samp{o}s is allowed: @samp{fo*} does match
427 @samp{*} always applies to the @emph{smallest} possible preceding
428 expression. Thus, @samp{fo*} has a repeating @samp{o}, not a
429 repeating @samp{fo}.@refill
431 The matcher processes a @samp{*} construct by matching, immediately, as
432 many repetitions as can be found; it is "greedy". Then it continues
433 with the rest of the pattern. If that fails, backtracking occurs,
434 discarding some of the matches of the @samp{*}-modified construct in
435 case that makes it possible to match the rest of the pattern. For
436 example, in matching @samp{ca*ar} against the string @samp{caaar}, the
437 @samp{a*} first tries to match all three @samp{a}s; but the rest of the
438 pattern is @samp{ar} and there is only @samp{r} left to match, so this
439 try fails. The next alternative is for @samp{a*} to match only two
440 @samp{a}s. With this choice, the rest of the regexp matches
443 Nested repetition operators can be extremely slow if they specify
444 backtracking loops. For example, it could take hours for the regular
445 expression @samp{\(x+y*\)*a} to match the sequence
446 @samp{xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxz}. The slowness is because
447 Emacs must try each imaginable way of grouping the 35 @samp{x}'s before
448 concluding that none of them can work. To make sure your regular
449 expressions run fast, check nested repetitions carefully.
452 @cindex @samp{+} in regexp
453 is a quantifying suffix operator similar to @samp{*} except that the
454 preceding expression must match at least once. It is also "greedy".
455 So, for example, @samp{ca+r} matches the strings @samp{car} and
456 @samp{caaaar} but not the string @samp{cr}, whereas @samp{ca*r} matches
460 @cindex @samp{?} in regexp
461 is a quantifying suffix operator similar to @samp{*}, except that the
462 preceding expression can match either once or not at all. For example,
463 @samp{ca?r} matches @samp{car} or @samp{cr}, but does not match anything
467 @cindex @samp{*?} in regexp
468 works just like @samp{*}, except that rather than matching the longest
469 match, it matches the shortest match. @samp{*?} is known as a
470 @dfn{non-greedy} quantifier, a regexp construct borrowed from Perl.
471 @c Did perl get this from somewhere? What's the real history of *? ?
473 This construct is very useful for when you want