comparison man/search.texi @ 38127:eb0f8509d107

Rewrite the texts that explain basic incremental search, basic nonincremental search, regexp incremental search, and query-replace. Other local cleanups.
author Richard M. Stallman <rms@gnu.org>
date Wed, 20 Jun 2001 10:46:24 +0000
parents 0ee6a3d3764e
children d8ab01673218
comparison
equal deleted inserted replaced
38126:64a45ba0553f 38127:eb0f8509d107
12 @dfn{incremental}; it begins to search before you have finished typing the 12 @dfn{incremental}; it begins to search before you have finished typing the
13 search string. There are also nonincremental search commands more like 13 search string. There are also nonincremental search commands more like
14 those of other editors. 14 those of other editors.
15 15
16 Besides the usual @code{replace-string} command that finds all 16 Besides the usual @code{replace-string} command that finds all
17 occurrences of one string and replaces them with another, Emacs has a fancy 17 occurrences of one string and replaces them with another, Emacs has a
18 replacement command called @code{query-replace} which asks interactively 18 more flexible replacement command called @code{query-replace}, which
19 which occurrences to replace. 19 asks interactively which occurrences to replace.
20 20
21 @menu 21 @menu
22 * Incremental Search:: Search happens as you type the string. 22 * Incremental Search:: Search happens as you type the string.
23 * Nonincremental Search:: Specify entire string and then search. 23 * Nonincremental Search:: Specify entire string and then search.
24 * Word Search:: Search for sequence of words. 24 * Word Search:: Search for sequence of words.
48 Incremental search backward (@code{isearch-backward}). 48 Incremental search backward (@code{isearch-backward}).
49 @end table 49 @end table
50 50
51 @kindex C-s 51 @kindex C-s
52 @findex isearch-forward 52 @findex isearch-forward
53 @kbd{C-s} starts an incremental search. @kbd{C-s} reads characters from 53 @kbd{C-s} starts a forward incremental search. It reads characters
54 the keyboard and positions the cursor at the first occurrence of the 54 from the keyboard, and moves point past the next occurrence of those
55 characters that you have typed. If you type @kbd{C-s} and then @kbd{F}, 55 characters. If you type @kbd{C-s} and then @kbd{F}, that puts the
56 the cursor moves right after the first @samp{F}. Type an @kbd{O}, and see 56 cursor after the first @samp{F} (following the starting point, since
57 the cursor move to after the first @samp{FO}. After another @kbd{O}, the 57 this is a forward search). Then if you type an @kbd{O}, you will see
58 cursor is after the first @samp{FOO} after the place where you started the 58 the cursor move just after the first @samp{FO} (the @samp{F} in that
59 search. At each step, the buffer text that matches the search string is 59 @samp{FO} may or may not be the first @samp{F}). After another
60 highlighted, if the terminal can do that; at each step, the current search 60 @kbd{O}, the cursor moves after the first @samp{FOO} after the place
61 string is updated in the echo area. 61 where you started the search. At each step, the buffer text that
62 matches the search string is highlighted, if the terminal can do that;
63 the current search string is always displayed in the echo area.
62 64
63 If you make a mistake in typing the search string, you can cancel 65 If you make a mistake in typing the search string, you can cancel
64 characters with @key{DEL}. Each @key{DEL} cancels the last character of 66 characters with @key{DEL}. Each @key{DEL} cancels the last character of
65 search string. This does not happen until Emacs is ready to read another 67 search string. This does not happen until Emacs is ready to read another
66 input character; first it must either find, or fail to find, the character 68 input character; first it must either find, or fail to find, the character
76 printing character, @key{DEL}, @key{RET}, or another character that is 78 printing character, @key{DEL}, @key{RET}, or another character that is
77 special within searches (@kbd{C-q}, @kbd{C-w}, @kbd{C-r}, @kbd{C-s}, 79 special within searches (@kbd{C-q}, @kbd{C-w}, @kbd{C-r}, @kbd{C-s},
78 @kbd{C-y}, @kbd{M-y}, @kbd{M-r}, @kbd{M-s}, and some other 80 @kbd{C-y}, @kbd{M-y}, @kbd{M-r}, @kbd{M-s}, and some other
79 meta-characters). 81 meta-characters).
80 82
81 Sometimes you search for @samp{FOO} and find it, but not the one you 83 Sometimes you search for @samp{FOO} and find one, but not the one you
82 expected to find. There was a second @samp{FOO} that you forgot 84 expected to find. There was a second @samp{FOO} that you forgot
83 about, before the one you were aiming for. In this event, type 85 about, before the one you were aiming for. In this event, type
84 another @kbd{C-s} to move to the next occurrence of the search string. 86 another @kbd{C-s} to move to the next occurrence of the search string.
85 You can repeat this any number of times. If you overshoot, you can 87 You can repeat this any number of times. If you overshoot, you can
86 cancel some @kbd{C-s} characters with @key{DEL}. 88 cancel some @kbd{C-s} characters with @key{DEL}.
100 string as it could. Thus, if you search for @samp{FOOT}, and there is no 102 string as it could. Thus, if you search for @samp{FOOT}, and there is no
101 @samp{FOOT}, you might see the cursor after the @samp{FOO} in @samp{FOOL}. 103 @samp{FOOT}, you might see the cursor after the @samp{FOO} in @samp{FOOL}.
102 At this point there are several things you can do. If your string was 104 At this point there are several things you can do. If your string was
103 mistyped, you can rub some of it out and correct it. If you like the place 105 mistyped, you can rub some of it out and correct it. If you like the place
104 you have found, you can type @key{RET} or some other Emacs command to 106 you have found, you can type @key{RET} or some other Emacs command to
105 ``accept what the search offered.'' Or you can type @kbd{C-g}, which 107 remain there. Or you can type @kbd{C-g}, which
106 removes from the search string the characters that could not be found (the 108 removes from the search string the characters that could not be found (the
107 @samp{T} in @samp{FOOT}), leaving those that were found (the @samp{FOO} in 109 @samp{T} in @samp{FOOT}), leaving those that were found (the @samp{FOO} in
108 @samp{FOOT}). A second @kbd{C-g} at that point cancels the search 110 @samp{FOOT}). A second @kbd{C-g} at that point cancels the search
109 entirely, returning point to where it was when the search started. 111 entirely, returning point to where it was when the search started.
110 112
139 toggle (enable or disable) the input method while you type the search 141 toggle (enable or disable) the input method while you type the search
140 string with @kbd{C-\} (@code{isearch-toggle-input-method}). You can 142 string with @kbd{C-\} (@code{isearch-toggle-input-method}). You can
141 turn on a certain (non-default) input method with @kbd{C-^} 143 turn on a certain (non-default) input method with @kbd{C-^}
142 (@code{isearch-toggle-specified-input-method}), which prompts for the 144 (@code{isearch-toggle-specified-input-method}), which prompts for the
143 name of the input method. Note that the input method you turn on 145 name of the input method. Note that the input method you turn on
144 during incremental search is turned on in the current buffer as well. 146 during incremental search remains enabled in the current buffer as well.
145 147
146 If a search is failing and you ask to repeat it by typing another 148 If a search is failing and you ask to repeat it by typing another
147 @kbd{C-s}, it starts again from the beginning of the buffer. 149 @kbd{C-s}, it starts again from the beginning of the buffer.
148 Repeating a failing reverse search with @kbd{C-r} starts again from 150 Repeating a failing reverse search with @kbd{C-r} starts again from
149 the end. This is called @dfn{wrapping around}, and @samp{Wrapped} 151 the end. This is called @dfn{wrapping around}, and @samp{Wrapped}
258 @end table 260 @end table
259 261
260 To do a nonincremental search, first type @kbd{C-s @key{RET}}. This 262 To do a nonincremental search, first type @kbd{C-s @key{RET}}. This
261 enters the minibuffer to read the search string; terminate the string 263 enters the minibuffer to read the search string; terminate the string
262 with @key{RET}, and then the search takes place. If the string is not 264 with @key{RET}, and then the search takes place. If the string is not
263 found, the search command gets an error. 265 found, the search command signals an error.
264 266
265 The way @kbd{C-s @key{RET}} works is that the @kbd{C-s} invokes 267 When you type @kbd{C-s @key{RET}}, the @kbd{C-s} invokes incremental
266 incremental search, which is specially programmed to invoke nonincremental 268 search as usual. That command is specially programmed to invoke
267 search if the argument you give it is empty. (Such an empty argument would 269 nonincremental search, @code{search-forward}, if the string you
268 otherwise be useless.) @kbd{C-r @key{RET}} also works this way. 270 specify is empty. (Such an empty argument would otherwise be
269 271 useless.) But it does not call @code{search-forward} right away. First
270 However, nonincremental searches performed using @kbd{C-s @key{RET}} do 272 it checks the next input character to see if is @kbd{C-w},
271 not call @code{search-forward} right away. The first thing done is to see 273 which specifies a word search.
272 if the next character is @kbd{C-w}, which requests a word search.
273 @ifinfo 274 @ifinfo
274 @xref{Word Search}. 275 @xref{Word Search}.
275 @end ifinfo 276 @end ifinfo
277 @kbd{C-r @key{RET}} does likewise, for a reverse incremental search.
276 278
277 @findex search-forward 279 @findex search-forward
278 @findex search-backward 280 @findex search-backward
279 Forward and backward nonincremental searches are implemented by the 281 Forward and backward nonincremental searches are implemented by the
280 commands @code{search-forward} and @code{search-backward}. These 282 commands @code{search-forward} and @code{search-backward}. These
281 commands may be bound to keys in the usual manner. The feature that you 283 commands may be bound to keys in the usual manner. The feature that you
282 can get to them via the incremental search commands exists for 284 can get to them via the incremental search commands exists for
283 historical reasons, and to avoid the need to find suitable key sequences 285 historical reasons, and to avoid the need to find key sequences
284 for them. 286 for them.
285 287
286 @node Word Search, Regexp Search, Nonincremental Search, Search 288 @node Word Search, Regexp Search, Nonincremental Search, Search
287 @section Word Search 289 @section Word Search
288 @cindex word search 290 @cindex word search
316 318
317 @findex word-search-forward 319 @findex word-search-forward
318 @findex word-search-backward 320 @findex word-search-backward
319 Forward and backward word searches are implemented by the commands 321 Forward and backward word searches are implemented by the commands
320 @code{word-search-forward} and @code{word-search-backward}. These 322 @code{word-search-forward} and @code{word-search-backward}. These
321 commands may be bound to keys in the usual manner. The feature that you 323 commands may be bound to keys in the usual manner. They are available
322 can get to them via the incremental search commands exists for historical 324 via the incremental search commands both for historical reasons and
323 reasons, and to avoid the need to find suitable key sequences for them. 325 to avoid the need to find suitable key sequences for them.
324 326
325 @node Regexp Search, Regexps, Word Search, Search 327 @node Regexp Search, Regexps, Word Search, Search
326 @section Regular Expression Search 328 @section Regular Expression Search
327 @cindex regular expression 329 @cindex regular expression
328 @cindex regexp 330 @cindex regexp
329 331
330 A @dfn{regular expression} (@dfn{regexp}, for short) is a pattern that 332 A @dfn{regular expression} (@dfn{regexp}, for short) is a pattern
331 denotes a class of alternative strings to match, possibly infinitely 333 that denotes a class of alternative strings to match, possibly
332 many. In GNU Emacs, you can search for the next match for a regexp 334 infinitely many. GNU Emacs provides both incremental and
333 either incrementally or not. 335 nonincremental ways to search for a match for a regexp.
334 336
335 @kindex C-M-s 337 @kindex C-M-s
336 @findex isearch-forward-regexp 338 @findex isearch-forward-regexp
337 @kindex C-M-r 339 @kindex C-M-r
338 @findex isearch-backward-regexp 340 @findex isearch-backward-regexp
339 Incremental search for a regexp is done by typing @kbd{C-M-s} 341 Incremental search for a regexp is done by typing @kbd{C-M-s}
340 (@code{isearch-forward-regexp}). This command reads a search string 342 (@code{isearch-forward-regexp}), or by invoking @kbd{C-s} with a
341 incrementally just like @kbd{C-s}, but it treats the search string as a 343 prefix argument (whose value does not matter). This command reads a
342 regexp rather than looking for an exact match against the text in the 344 search string incrementally just like @kbd{C-s}, but it treats the
343 buffer. Each time you add text to the search string, you make the 345 search string as a regexp rather than looking for an exact match
344 regexp longer, and the new regexp is searched for. Invoking @kbd{C-s} 346 against the text in the buffer. Each time you add text to the search
345 with a prefix argument (its value does not matter) is another way to do 347 string, you make the regexp longer, and the new regexp is searched
346 a forward incremental regexp search. To search backward for a regexp, 348 for. To search backward for a regexp, use @kbd{C-M-r}
347 use @kbd{C-M-r} (@code{isearch-backward-regexp}), or @kbd{C-r} with a 349 (@code{isearch-backward-regexp}), or @kbd{C-r} with a prefix argument.
348 prefix argument.
349 350
350 All of the control characters that do special things within an 351 All of the control characters that do special things within an
351 ordinary incremental search have the same function in incremental regexp 352 ordinary incremental search have the same function in incremental regexp
352 search. Typing @kbd{C-s} or @kbd{C-r} immediately after starting the 353 search. Typing @kbd{C-s} or @kbd{C-r} immediately after starting the
353 search retrieves the last incremental search regexp used; that is to 354 search retrieves the last incremental search regexp used; that is to
778 @cindex replacement 779 @cindex replacement
779 @cindex search-and-replace commands 780 @cindex search-and-replace commands
780 @cindex string substitution 781 @cindex string substitution
781 @cindex global substitution 782 @cindex global substitution
782 783
783 Global search-and-replace operations are not needed as often in Emacs 784 Global search-and-replace operations are not needed often in Emacs,
784 as they are in other editors@footnote{In some editors, 785 but they are available. In addition to the simple @kbd{M-x
785 search-and-replace operations are the only convenient way to make a 786 replace-string} command which is like that found in most editors,
786 single change in the text.}, but they are available. In addition to the 787 there is a @kbd{M-x query-replace} command which finds each occurrence
787 simple @kbd{M-x replace-string} command which is like that found in most 788 of the pattern and asks you whether to replace it.
788 editors, there is a @kbd{M-x query-replace} command which asks you, for
789 each occurrence of the pattern, whether to replace it.
790 789
791 The replace commands normally operate on the text from point to the 790 The replace commands normally operate on the text from point to the
792 end of the buffer; however, in Transient Mark mode, when the mark is 791 end of the buffer; however, in Transient Mark mode, when the mark is
793 active, they operate on the region. The replace commands all replace 792 active, they operate on the region. The replace commands all replace
794 one string (or regexp) with one replacement string. It is possible to 793 one string (or regexp) with one replacement string. It is possible to