aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--doc/emacs/search.texi586
1 files changed, 288 insertions, 298 deletions
diff --git a/doc/emacs/search.texi b/doc/emacs/search.texi
index 979d6670f57..98838724fa3 100644
--- a/doc/emacs/search.texi
+++ b/doc/emacs/search.texi
@@ -7,20 +7,13 @@
7@cindex searching 7@cindex searching
8@cindex finding strings within text 8@cindex finding strings within text
9 9
10 Like other editors, Emacs has commands for searching for occurrences of 10 Like other editors, Emacs has commands to search for occurrences of
11a string. The principal search command is unusual in that it is 11a string. Emacs also has commands to replace occurrences of a string
12@dfn{incremental}; it begins to search before you have finished typing the 12with a different string. There are also commands that do the same
13search string. There are also nonincremental search commands more like 13thing, but search for patterns instead of fixed strings.
14those of other editors. 14
15 15 You can also search multiple files under the control of a tags table
16 Besides the usual @code{replace-string} command that finds all 16(@pxref{Tags Search}) or through the Dired @kbd{A} command
17occurrences of one string and replaces them with another, Emacs has a
18more flexible replacement command called @code{query-replace}, which
19asks interactively which occurrences to replace. There are also
20commands to find and operate on all matches for a pattern.
21
22 You can also search multiple files under control of a tags
23table (@pxref{Tags Search}) or through the Dired @kbd{A} command
24(@pxref{Operating on Files}), or ask the @code{grep} program to do it 17(@pxref{Operating on Files}), or ask the @code{grep} program to do it
25(@pxref{Grep Searching}). 18(@pxref{Grep Searching}).
26 19
@@ -43,12 +36,13 @@ table (@pxref{Tags Search}) or through the Dired @kbd{A} command
43@cindex incremental search 36@cindex incremental search
44@cindex isearch 37@cindex isearch
45 38
46 An incremental search begins searching as soon as you type the first 39 The principal search command in Emacs is @dfn{incremental}: it
47character of the search string. As you type in the search string, Emacs 40begins searching as soon as you type the first character of the search
48shows you where the string (as you have typed it so far) would be 41string. As you type in the search string, Emacs shows you where the
49found. When you have typed enough characters to identify the place you 42string (as you have typed it so far) would be found. When you have
50want, you can stop. Depending on what you plan to do next, you may or 43typed enough characters to identify the place you want, you can stop.
51may not need to terminate the search explicitly with @key{RET}. 44Depending on what you plan to do next, you may or may not need to
45terminate the search explicitly with @key{RET}.
52 46
53@table @kbd 47@table @kbd
54@item C-s 48@item C-s
@@ -62,68 +56,106 @@ Incremental search backward (@code{isearch-backward}).
62* Repeat Isearch:: Searching for the same string again. 56* Repeat Isearch:: Searching for the same string again.
63* Error in Isearch:: When your string is not found. 57* Error in Isearch:: When your string is not found.
64* Special Isearch:: Special input in incremental search. 58* Special Isearch:: Special input in incremental search.
65* Non-ASCII Isearch:: How to search for non-ASCII characters.
66* Isearch Yank:: Commands that grab text into the search string 59* Isearch Yank:: Commands that grab text into the search string
67 or else edit the search string. 60 or else edit the search string.
68* Highlight Isearch:: Isearch highlights the other possible matches.
69* Isearch Scroll:: Scrolling during an incremental search. 61* Isearch Scroll:: Scrolling during an incremental search.
62* Isearch Minibuffer:: Incremental search of the minibuffer history.
70* Slow Isearch:: Incremental search features for slow terminals. 63* Slow Isearch:: Incremental search features for slow terminals.
71@end menu 64@end menu
72 65
73@node Basic Isearch 66@node Basic Isearch
74@subsection Basics of Incremental Search 67@subsection Basics of Incremental Search
75 68
69@table @kbd
70@item C-s
71Begin incremental search (@code{isearch-forward}).
72@item C-r
73Begin reverse incremental search (@code{isearch-backward}).
74@end table
75
76@kindex C-s 76@kindex C-s
77@findex isearch-forward 77@findex isearch-forward
78 @kbd{C-s} starts a forward incremental search. It reads characters 78 @kbd{C-s} (@code{isearch-forward}) starts a forward incremental
79from the keyboard, and moves point past the next occurrence of those 79search. It reads characters from the keyboard, and moves point just
80characters. If you type @kbd{C-s} and then @kbd{F}, that puts the 80past the end of the next occurrence of those characters in the buffer.
81cursor after the first @samp{F} (the first following the starting point, since 81
82this is a forward search). Then if you type an @kbd{O}, you will see 82 For instance, if you type @kbd{C-s} and then @kbd{F}, that puts the
83the cursor move to just after the first @samp{FO} (the @samp{F} in that 83cursor after the first @samp{F} that occurs in the buffer after the
84@samp{FO} may or may not be the first @samp{F}). After another 84starting point. Then if you then type @kbd{O}, the cursor moves to
85@kbd{O}, the cursor moves to just after the first @samp{FOO} after the place 85just after the first @samp{FO}; the @samp{F} in that @samp{FO} might
86where you started the search. At each step, the buffer text that 86not be the first @samp{F} previously found. After another @kbd{O},
87matches the search string is highlighted, if the terminal can do that; 87the cursor moves to just after the first @samp{FOO}.
88the current search string is always displayed in the echo area. 88
89 89@cindex faces for highlighting search matches
90 If you make a mistake in typing the search string, you can cancel 90 At each step, Emacs highlights the @dfn{current match}---the buffer
91characters with @key{DEL}. Each @key{DEL} cancels the last character of 91text that matches the search string---using the @code{isearch} face
92search string. This does not happen until Emacs is ready to read another 92(@pxref{Faces}). The current search string is also displayed in the
93input character; first it must either find, or fail to find, the character 93echo area.
94you want to erase. If you do not want to wait for this to happen, use 94
95@kbd{C-g} as described below. 95 If you make a mistake typing the search string, type @key{DEL}.
96 96Each @key{DEL} cancels the last character of the search string.
97 When you are satisfied with the place you have reached, you can type 97
98@key{RET}, which stops searching, leaving the cursor where the search 98 When you are satisfied with the place you have reached, type
99@key{RET}. This stops searching, leaving the cursor where the search
99brought it. Also, any command not specially meaningful in searches 100brought it. Also, any command not specially meaningful in searches
100stops the searching and is then executed. Thus, typing @kbd{C-a} 101stops the searching and is then executed. Thus, typing @kbd{C-a}
101would exit the search and then move to the beginning of the line. 102exits the search and then moves to the beginning of the line.
102@key{RET} is necessary only if the next command you want to type is a 103@key{RET} is necessary only if the next command you want to type is a
103printing character, @key{DEL}, @key{RET}, or another character that is 104printing character, @key{DEL}, @key{RET}, or another character that is
104special within searches (@kbd{C-q}, @kbd{C-w}, @kbd{C-r}, @kbd{C-s}, 105special within searches (@kbd{C-q}, @kbd{C-w}, @kbd{C-r}, @kbd{C-s},
105@kbd{C-y}, @kbd{M-y}, @kbd{M-r}, @kbd{M-c}, @kbd{M-e}, and some other 106@kbd{C-y}, @kbd{M-y}, @kbd{M-r}, @kbd{M-c}, @kbd{M-e}, and some others
106meta-characters). 107described below).
108
109 As a special exception, entering @key{RET} when the search string is
110empty launches nonincremental search (@pxref{Nonincremental Search}).
107 111
108 When you exit the incremental search, it adds the original value of 112 When you exit the incremental search, it adds the original value of
109point to the mark ring, without activating the mark. You can thus use 113point to the mark ring, without activating the mark; you can thus use
110@kbd{C-@key{SPC}} to return to where you were before beginning the 114@kbd{C-u C-@key{SPC}} to return to where you were before beginning the
111search. @xref{Mark Ring}. It only does this if the mark was not 115search. @xref{Mark Ring}. It only does this if the mark was not
112already active. 116already active.
113 117
118@kindex C-r
119@findex isearch-backward
120 To search backwards, use @kbd{C-r} (@code{isearch-backward}) instead
121of @kbd{C-s} to start the search. A backward search finds matches
122that end before the starting point, just as a forward search finds
123matches that begin after it.
124
114@node Repeat Isearch 125@node Repeat Isearch
115@subsection Repeating Incremental Search 126@subsection Repeating Incremental Search
116 127
117 Sometimes you search for @samp{FOO} and find one, but not the one you 128 Suppose you search forward for @samp{FOO} and find a match, but not
118expected to find. There was a second @samp{FOO} that you forgot 129the one you expected to find: the @samp{FOO} you were aiming for
119about, before the one you were aiming for. In this event, type 130occurs later in the buffer. In this event, type another @kbd{C-s} to
120another @kbd{C-s} to move to the next occurrence of the search string. 131move to the next occurrence of the search string. You can repeat this
121You can repeat this any number of times. If you overshoot, you can 132any number of times. If you overshoot, you can cancel some @kbd{C-s}
122cancel some @kbd{C-s} characters with @key{DEL}. 133characters with @key{DEL}. Similarly, each @kbd{C-r} in a backward
134incremental search repeats the backward search.
135
136@cindex lazy search highlighting
137@vindex isearch-lazy-highlight
138 If you pause for a little while during incremental search, Emacs
139highlights all the other possible matches for the search string that
140are present on the screen. This helps you anticipate where you can
141get to by typing @kbd{C-s} or @kbd{C-r} to repeat the search. The
142other matches are highlighted differently from the current match,
143using the customizable face @code{lazy-highlight} (@pxref{Faces}). If
144you don't like this feature, you can disable it by setting
145@code{isearch-lazy-highlight} to @code{nil}.
146
147 After exiting a search, you can search for the same string again by
148typing just @kbd{C-s C-s}. The first @kbd{C-s} is the key that
149invokes incremental search, and the second @kbd{C-s} means ``search
150again.'' Similarly, @kbd{C-r C-r} searches backward for the last
151search string. In determining the last search string, it doesn't
152matter whether the string was searched for with @kbd{C-s} or
153@kbd{C-r}.
123 154
124 After you exit a search, you can search for the same string again by 155 If you are searching forward but you realize you were looking for
125typing just @kbd{C-s C-s}: the first @kbd{C-s} is the key that invokes 156something before the starting point, type @kbd{C-r} to switch to a
126incremental search, and the second @kbd{C-s} means ``search again.'' 157backward search, leaving the search string unchanged. Similarly,
158@kbd{C-s} in a backward search switches to a forward search.
127 159
128 If a search is failing and you ask to repeat it by typing another 160 If a search is failing and you ask to repeat it by typing another
129@kbd{C-s}, it starts again from the beginning of the buffer. 161@kbd{C-s}, it starts again from the beginning of the buffer.
@@ -134,97 +166,75 @@ going past the original starting point of the search, it changes to
134@samp{Overwrapped}, which means that you are revisiting matches that 166@samp{Overwrapped}, which means that you are revisiting matches that
135you have already seen. 167you have already seen.
136 168
169@cindex search ring
137 To reuse earlier search strings, use the @dfn{search ring}. The 170 To reuse earlier search strings, use the @dfn{search ring}. The
138commands @kbd{M-p} and @kbd{M-n} move through the ring to pick a search 171commands @kbd{M-p} and @kbd{M-n} move through the ring to pick a
139string to reuse. These commands leave the selected search ring element 172search string to reuse. These commands leave the selected search ring
140in the minibuffer, where you can edit it. To edit the current search 173element in the minibuffer, where you can edit it. To edit the current
141string in the minibuffer without replacing it with items from the 174search string in the minibuffer without replacing it with items from
142search ring, type @kbd{M-e}. Type @kbd{C-s} or @kbd{C-r} 175the search ring, type @kbd{M-e}. Type @kbd{C-s} or @kbd{C-r} to
143to terminate editing the string and search for it. 176terminate editing the string and search for it.
144
145 You can change to searching backwards with @kbd{C-r}. For instance,
146if you are searching forward but you realize you were looking for
147something above the starting point, you can do this. Repeated
148@kbd{C-r} keeps looking for more occurrences backwards. A @kbd{C-s}
149starts going forwards again. @kbd{C-r} in a search can be canceled
150with @key{DEL}.
151
152@kindex C-r
153@findex isearch-backward
154 If you know initially that you want to search backwards, you can use
155@kbd{C-r} instead of @kbd{C-s} to start the search, because @kbd{C-r}
156as a key runs a command (@code{isearch-backward}) to search backward.
157A backward search finds matches that end before the starting point,
158just as a forward search finds matches that begin after it.
159 177
160@node Error in Isearch 178@node Error in Isearch
161@subsection Errors in Incremental Search 179@subsection Errors in Incremental Search
162 180
163 If your string is not found at all, the echo area says @samp{Failing 181 If your string is not found at all, the echo area says @samp{Failing
164I-Search}. The cursor is after the place where Emacs found as much of your 182I-Search}. The cursor is after the place where Emacs found as much of
165string as it could. Thus, if you search for @samp{FOOT}, and there is no 183your string as it could. Thus, if you search for @samp{FOOT}, and
166@samp{FOOT}, you might see the cursor after the @samp{FOO} in @samp{FOOL}. 184there is no @samp{FOOT}, you might see the cursor after the @samp{FOO}
167At this point there are several things you can do. If your string was 185in @samp{FOOL}. In the echo area, the part of the search string that
168mistyped, you can rub some of it out and correct it. If you like the place 186failed to match is highlighted using the customizable face
169you have found, you can type @key{RET} or some other Emacs command to 187@code{isearch-fail}.
170remain there. Or you can type @kbd{C-g}, which 188
171removes from the search string the characters that could not be found (the 189 At this point, there are several things you can do. If your string
172@samp{T} in @samp{FOOT}), leaving those that were found (the @samp{FOO} in 190was mistyped, you can use @key{DEL} to erase some of it and correct
191it. If you like the place you have found, you can type @key{RET} to
192remain there. Or you can type @kbd{C-g}, which removes from the
193search string the characters that could not be found (the @samp{T} in
194@samp{FOOT}), leaving those that were found (the @samp{FOO} in
173@samp{FOOT}). A second @kbd{C-g} at that point cancels the search 195@samp{FOOT}). A second @kbd{C-g} at that point cancels the search
174entirely, returning point to where it was when the search started. 196entirely, returning point to where it was when the search started.
175 197
176@cindex quitting (in search) 198@cindex quitting (in search)
177 The @kbd{C-g} ``quit'' character does special things during searches; 199 The quit command, @kbd{C-g}, does special things during searches;
178just what it does depends on the status of the search. If the search has 200just what it does depends on the status of the search. If the search
179found what you specified and is waiting for input, @kbd{C-g} cancels the 201has found what you specified and is waiting for input, @kbd{C-g}
180entire search. The cursor moves back to where you started the search. If 202cancels the entire search, moving the cursor back to where you started
181@kbd{C-g} is typed when there are characters in the search string that have 203the search. If @kbd{C-g} is typed when there are characters in the
182not been found---because Emacs is still searching for them, or because it 204search string that have not been found---because Emacs is still
183has failed to find them---then the search string characters which have not 205searching for them, or because it has failed to find them---then the
184been found are discarded from the search string. With them gone, the 206search string characters which have not been found are discarded from
185search is now successful and waiting for more input, so a second @kbd{C-g} 207the search string. With them gone, the search is now successful and
186will cancel the entire search. 208waiting for more input, so a second @kbd{C-g} will cancel the entire
209search.
187 210
188@node Special Isearch 211@node Special Isearch
189@subsection Special Input for Incremental Search 212@subsection Special Input for Incremental Search
190 213
191 An upper-case letter in the search string makes the search 214 Some of the characters you type during incremental search have
192case-sensitive. If you delete the upper-case character from the search 215special effects.
193string, it ceases to have this effect. @xref{Search Case}. 216
194 217 If the search string you entered contains only lower-case letters,
195 To search for a newline, type @kbd{C-j}. To search for another 218the search is case-insensitive; as long as an upper-case letter exists
196control character, such as control-S or carriage return, you must quote 219in the search string, the search becomes case-sensitive. If you
197it by typing @kbd{C-q} first. This function of @kbd{C-q} is analogous 220delete the upper-case character from the search string, it ceases to
198to its use for insertion (@pxref{Inserting Text}): it causes the 221have this effect. @xref{Search Case}.
199following character to be treated the way any ``ordinary'' character is 222
200treated in the same context. You can also specify a character by its 223 To search for a newline character, type @kbd{C-j}.
201octal code: enter @kbd{C-q} followed by a sequence of octal digits. 224
202 225 To search for other control characters, such as @key{control-S},
203 @kbd{M-%} typed in incremental search invokes @code{query-replace} 226quote it by typing @kbd{C-q} first (@pxref{Inserting Text}). To
204or @code{query-replace-regexp} (depending on search mode) with the 227search for non-@acronym{ASCII} characters, you can either use
205current search string used as the string to replace. @xref{Query 228@kbd{C-q} and enter its octal code, or use an input method
206Replace}. 229(@pxref{Input Methods}). If an input method is enabled in the current
207 230buffer when you start the search, you can use it in the search string
208 Entering @key{RET} when the search string is empty launches 231also. While typing the search string, you can toggle the input method
209nonincremental search (@pxref{Nonincremental Search}). 232with the command @kbd{C-\} (@code{isearch-toggle-input-method}). You
210 233can also turn on a non-default input method with @kbd{C-^}
211@vindex isearch-mode-map 234(@code{isearch-toggle-specified-input-method}), which prompts for the
212 To customize the special characters that incremental search understands, 235name of the input method. When an input method is active during
213alter their bindings in the keymap @code{isearch-mode-map}. For a list 236incremental search, the search prompt includes the input method
214of bindings, look at the documentation of @code{isearch-mode} with 237mnemonic, like this:
215@kbd{C-h f isearch-mode @key{RET}}.
216
217@node Non-ASCII Isearch
218@subsection Isearch for Non-@acronym{ASCII} Characters
219@cindex searching for non-@acronym{ASCII} characters
220@cindex input method, during incremental search
221
222 To enter non-@acronym{ASCII} characters in an incremental search,
223you can use @kbd{C-q} (see the previous section), but it is easier to
224use an input method (@pxref{Input Methods}). If an input method is
225enabled in the current buffer when you start the search, you can use
226it in the search string also. Emacs indicates that by including the
227input method mnemonic in its prompt, like this:
228 238
229@example 239@example
230I-search [@var{im}]: 240I-search [@var{im}]:
@@ -233,33 +243,37 @@ I-search [@var{im}]:
233@noindent 243@noindent
234@findex isearch-toggle-input-method 244@findex isearch-toggle-input-method
235@findex isearch-toggle-specified-input-method 245@findex isearch-toggle-specified-input-method
236where @var{im} is the mnemonic of the active input method. 246where @var{im} is the mnemonic of the active input method. Any input
247method you enable during incremental search remains enabled in the
248current buffer afterwards.
237 249
238 You can toggle (enable or disable) the input method while you type 250 @kbd{M-%} typed in incremental search invokes @code{query-replace}
239the search string with @kbd{C-\} (@code{isearch-toggle-input-method}). 251or @code{query-replace-regexp} (depending on search mode) with the
240You can turn on a certain (non-default) input method with @kbd{C-^} 252current search string used as the string to replace. @xref{Query
241(@code{isearch-toggle-specified-input-method}), which prompts for the 253Replace}.
242name of the input method. The input method you enable during 254
243incremental search remains enabled in the current buffer afterwards. 255@vindex isearch-mode-map
256 When incremental search is active, you can type @kbd{C-h C-h} to
257access interactive help options, including a list of special
258keybindings. These keybindings are part of the keymap
259@code{isearch-mode-map} (@pxref{Keymaps}).
244 260
245@node Isearch Yank 261@node Isearch Yank
246@subsection Isearch Yanking 262@subsection Isearch Yanking
247 263
248 The characters @kbd{C-w} and @kbd{C-y} can be used in incremental 264 Within incremental search, you can use @kbd{C-w} and @kbd{C-y} to grab
249search to grab text from the buffer into the search string. This 265text from the buffer into the search string. This makes it convenient
250makes it convenient to search for another occurrence of text at point. 266to search for another occurrence of text at point.
251@kbd{C-w} copies the character or word after point as part of the 267
252search string, advancing point over it. (The decision, whether to 268 @kbd{C-w} copies the character or word after point and adds it to
253copy a character or a word, is heuristic.) Another @kbd{C-s} to 269the search string, advancing point over it. (The decision, whether to
254repeat the search will then search for a string including that 270copy a character or a word, is heuristic.)
255character or word.
256 271
257 @kbd{C-y} is similar to @kbd{C-w} but copies all the rest of the 272 @kbd{C-y} is similar to @kbd{C-w} but copies all the rest of the
258current line into the search string. If point is already at the end 273current line into the search string. If point is already at the end
259of a line, it grabs the entire next line. Both @kbd{C-y} and 274of a line, it grabs the entire next line. If the search is currently
260@kbd{C-w} convert the text they copy to lower case if the search is 275case-insensitive, both @kbd{C-y} and @kbd{C-w} convert the text they
261currently not case-sensitive; this is so the search remains 276copy to lower case, so that the search remains case-insensitive.
262case-insensitive.
263 277
264 @kbd{C-M-w} and @kbd{C-M-y} modify the search string by only one 278 @kbd{C-M-w} and @kbd{C-M-y} modify the search string by only one
265character at a time: @kbd{C-M-w} deletes the last character from the 279character at a time: @kbd{C-M-w} deletes the last character from the
@@ -269,28 +283,9 @@ after point into the search string is to enter the minibuffer by
269@kbd{M-e} and to type @kbd{C-f} at the end of the search string in the 283@kbd{M-e} and to type @kbd{C-f} at the end of the search string in the
270minibuffer. 284minibuffer.
271 285
272 The character @kbd{M-y} copies text from the kill ring into the search 286 The character @kbd{M-y} copies text from the kill ring into the
273string. It uses the same text that @kbd{C-y} as a command would yank. 287search string. It uses the same text that @kbd{C-y} would yank.
274@kbd{Mouse-2} in the echo area does the same. 288@kbd{Mouse-2} in the echo area does the same. @xref{Yanking}.
275@xref{Yanking}.
276
277@node Highlight Isearch
278@subsection Lazy Search Highlighting
279@cindex lazy search highlighting
280@vindex isearch-lazy-highlight
281
282 When you pause for a little while during incremental search, it
283highlights all other possible matches for the search string. This
284makes it easier to anticipate where you can get to by typing @kbd{C-s}
285or @kbd{C-r} to repeat the search. The short delay before highlighting
286other matches helps indicate which match is the current one.
287If you don't like this feature, you can turn it off by setting
288@code{isearch-lazy-highlight} to @code{nil}.
289
290@cindex faces for highlighting search matches
291 You can control how this highlighting looks by customizing the faces
292@code{isearch} (used for the current match) and @code{lazy-highlight}
293(for all the other matches).
294 289
295@node Isearch Scroll 290@node Isearch Scroll
296@subsection Scrolling During Incremental Search 291@subsection Scrolling During Incremental Search
@@ -331,6 +326,30 @@ change point, the buffer contents, the match data, the current buffer,
331or the selected window and frame. The command must not itself attempt 326or the selected window and frame. The command must not itself attempt
332an incremental search. 327an incremental search.
333 328
329@node Isearch Minibuffer
330@subsection Searching the Minibuffer
331@cindex minibuffer history, searching
332
333If you start an incremental search while the minibuffer is active,
334Emacs searches the contents of the minibuffer. Unlike searching an
335ordinary buffer, the search string is not shown in the echo area,
336because that is used to display the minibuffer.
337
338If an incremental search fails in the minibuffer, it tries searching
339the minibuffer history. @xref{Minibuffer History}. You can visualize
340the minibuffer and its history as a series of ``pages'', with the
341earliest history element on the first page and the current minibuffer
342on the last page. A forward search, @kbd{C-s}, searches forward to
343later pages; a reverse search, @kbd{C-r}, searches backwards to
344earlier pages. Like in ordinary buffer search, a failing search can
345wrap around, going from the last page to the first page or vice versa.
346
347When the current match is on a history element, that history element
348is pulled into the minibuffer. If you exit the incremental search
349normally (e.g. by typing @key{RET}), it remains in the minibuffer
350afterwards. Cancelling the search, with @kbd{C-g}, restores the
351contents of the minibuffer when you began the search.
352
334@node Slow Isearch 353@node Slow Isearch
335@subsection Slow Terminal Incremental Search 354@subsection Slow Terminal Incremental Search
336 355
@@ -369,88 +388,79 @@ Search for @var{string}.
369Search backward for @var{string}. 388Search backward for @var{string}.
370@end table 389@end table
371 390
372 To do a nonincremental search, first type @kbd{C-s @key{RET}}. This 391 To start a nonincremental search, first type @kbd{C-s @key{RET}}.
373enters the minibuffer to read the search string; terminate the string 392This enters the minibuffer to read the search string; terminate the
374with @key{RET}, and then the search takes place. If the string is not 393string with @key{RET}, and then the search takes place. If the string
375found, the search command signals an error. 394is not found, the search command signals an error.
376 395
377 When you type @kbd{C-s @key{RET}}, the @kbd{C-s} invokes incremental 396 When you type @kbd{C-s @key{RET}}, the @kbd{C-s} invokes incremental
378search as usual. That command is specially programmed to invoke 397search as usual. That command is specially programmed to invoke
379nonincremental search, @code{search-forward}, if the string you 398nonincremental search, @code{search-forward}, if the string you
380specify is empty. (Such an empty argument would otherwise be 399specify is empty. (Such an empty argument would otherwise be
381useless.) But it does not call @code{search-forward} right away. First 400useless.) @kbd{C-r @key{RET}} does likewise, for a reverse
382it checks the next input character to see if is @kbd{C-w}, 401incremental search.
383which specifies a word search.
384@ifnottex
385@xref{Word Search}.
386@end ifnottex
387@kbd{C-r @key{RET}} does likewise, for a reverse incremental search.
388 402
389@findex search-forward 403@findex search-forward
390@findex search-backward 404@findex search-backward
391 Forward and backward nonincremental searches are implemented by the 405 Forward and backward nonincremental searches are implemented by the
392commands @code{search-forward} and @code{search-backward}. These 406commands @code{search-forward} and @code{search-backward}. These
393commands may be bound to keys in the usual manner. The feature that you 407commands may be bound to other keys in the usual manner.
394can get to them via the incremental search commands exists for
395historical reasons, and to avoid the need to find separate key sequences
396for them.
397 408
398@node Word Search 409@node Word Search
399@section Word Search 410@section Word Search
400@cindex word search 411@cindex word search
401 412
402 Word search searches for a sequence of words without regard to how the 413 A @dfn{word search} finds a sequence of words without regard to the
403words are separated. More precisely, you type a string of many words, 414type of punctuation between them. For instance, if you enter a search
404using single spaces to separate them, and the string can be found even 415string that consists of two words separated by a single space, the
405if there are multiple spaces, newlines, or other punctuation characters 416search matches any sequence of those two words separated by one or
406between these words. 417more spaces, newlines, or other punctuation characters. This is
407 418particularly useful for searching text documents, because you don't
408 Word search is useful for editing a printed document made with a text 419have to worry whether the words you are looking for are separated by
409formatter. If you edit while looking at the printed, formatted version, 420newlines or spaces.
410you can't tell where the line breaks are in the source file. With word
411search, you can search without having to know them.
412 421
413@table @kbd 422@table @kbd
414@item C-s @key{RET} C-w @var{words} @key{RET} 423@item M-s w
415Search for @var{words}, ignoring details of punctuation. 424If incremental search is active, toggle word search mode
416@item C-r @key{RET} C-w @var{words} @key{RET} 425(@code{isearch-toggle-word}); otherwise, begin an incremental forward
417Search backward for @var{words}, ignoring details of punctuation. 426word search (@code{isearch-forward-word}).
427@item C-s @key{RET} C-w
428Begin a non-incremental forward word search.
429@item C-r @key{RET} C-w
430Begin a non-incremental backward word search.
418@end table 431@end table
419 432
420 Word search as a special case of nonincremental search is invoked 433 To begin a forward incremental word search, type @kbd{M-s w}. If
421with @kbd{C-s @key{RET} C-w}. This is followed by the search string, 434incremental search is not already active, this runs the command
422which must always be terminated with @key{RET}. Being nonincremental, 435@code{isearch-forward-word}. If incremental search is already active
423this search does not start until the argument is terminated. It works 436(whether a forward or backward search), @kbd{M-s w} switches to a word
424by constructing a regular expression and searching for that; see 437search while keeping the direction of the search and the current
425@ref{Regexp Search}. 438search string unchanged. You can toggle word search back off by
426 439typing @kbd{M-s w} again.
427 Use @kbd{C-r @key{RET} C-w} to do backward word search.
428
429 You can also invoke word search with @kbd{C-s M-e C-w} or @kbd{C-r
430M-e C-w} followed by the search string and terminated with @key{RET},
431@kbd{C-s} or @kbd{C-r}. This puts word search into incremental mode
432where you can use all keys available for incremental search. However,
433when you type more words in incremental word search, it will fail
434until you type complete words.
435 440
436@findex word-search-forward 441@findex word-search-forward
437@findex word-search-backward 442@findex word-search-backward
438 Forward and backward word searches are implemented by the commands 443 To begin a nonincremental word search, type @kbd{C-s @key{RET} C-w}
439@code{word-search-forward} and @code{word-search-backward}. These 444for a forward search or @kbd{C-r @key{RET} C-w} for a forward search.
440commands may be bound to keys in the usual manner. They are available 445These run the commands @code{word-search-forward} and
441via the incremental search commands both for historical reasons and 446@code{word-search-backward} respectively.
442to avoid the need to find separate key sequences for them. 447
448 A nonincremental word search differs slightly from the incremental
449version in the way it finds a match: the last word in the search
450string must be an exact match for a whole word. In an incremental
451word search, the last word in the search string can match part of a
452word; this allows the matching to proceed incrementally as you type.
443 453
444@node Regexp Search 454@node Regexp Search
445@section Regular Expression Search 455@section Regular Expression Search
446@cindex regular expression 456@cindex regular expression
447@cindex regexp 457@cindex regexp
448 458
449 A @dfn{regular expression} (@dfn{regexp}, for short) is a pattern 459 A @dfn{regular expression} (or @dfn{regexp} for short) is a pattern
450that denotes a class of alternative strings to match, possibly 460that denotes a class of alternative strings to match. GNU Emacs
451infinitely many. GNU Emacs provides both incremental and 461provides both incremental and nonincremental ways to search for a
452nonincremental ways to search for a match for a regexp. The syntax of 462match for a regexp. The syntax of regular expressions is explained in
453regular expressions is explained in the following section. 463the following section.
454 464
455@kindex C-M-s 465@kindex C-M-s
456@findex isearch-forward-regexp 466@findex isearch-forward-regexp
@@ -468,12 +478,12 @@ for. To search backward for a regexp, use @kbd{C-M-r}
468(@code{isearch-backward-regexp}), @kbd{C-r} with a prefix argument, 478(@code{isearch-backward-regexp}), @kbd{C-r} with a prefix argument,
469or @kbd{M-r} within a backward incremental search. 479or @kbd{M-r} within a backward incremental search.
470 480
471 All of the control characters that do special things within an 481 All of the special key sequences in an ordinary incremental search
472ordinary incremental search have the same function in incremental regexp 482do similar things in an incremental regexp search. For instance,
473search. Typing @kbd{C-s} or @kbd{C-r} immediately after starting the 483typing @kbd{C-s} immediately after starting the search retrieves the
474search retrieves the last incremental search regexp used; that is to 484last incremental search regexp used and searches forward for it.
475say, incremental regexp and non-regexp searches have independent 485Incremental regexp and non-regexp searches have independent defaults.
476defaults. They also have separate search rings that you can access with 486They also have separate search rings, which you can access with
477@kbd{M-p} and @kbd{M-n}. 487@kbd{M-p} and @kbd{M-n}.
478 488
479@vindex search-whitespace-regexp 489@vindex search-whitespace-regexp
@@ -483,10 +493,11 @@ match just a space, type @kbd{C-q @key{SPC}}. You can control what a
483bare space matches by setting the variable 493bare space matches by setting the variable
484@code{search-whitespace-regexp} to the desired regexp. 494@code{search-whitespace-regexp} to the desired regexp.
485 495
486 In some cases, adding characters to the regexp in an incremental regexp 496 In some cases, adding characters to the regexp in an incremental
487search can make the cursor move back and start again. For example, if 497regexp search can make the cursor move back and start again. For
488you have searched for @samp{foo} and you add @samp{\|bar}, the cursor 498example, if you have searched for @samp{foo} and you add @samp{\|bar},
489backs up in case the first @samp{bar} precedes the first @samp{foo}. 499the cursor backs up in case the first @samp{bar} precedes the first
500@samp{foo}. @xref{Regexps}.
490 501
491 Forward and backward regexp search are not symmetrical, because 502 Forward and backward regexp search are not symmetrical, because
492regexp matching in Emacs always operates forward, starting with the 503regexp matching in Emacs always operates forward, starting with the
@@ -498,10 +509,9 @@ starting position. These search methods are not mirror images.
498@findex re-search-forward 509@findex re-search-forward
499@findex re-search-backward 510@findex re-search-backward
500 Nonincremental search for a regexp is done by the functions 511 Nonincremental search for a regexp is done by the functions
501@code{re-search-forward} and @code{re-search-backward}. You can invoke 512@code{re-search-forward} and @code{re-search-backward}. You can
502these with @kbd{M-x}, or bind them to keys, or invoke them by way of 513invoke these with @kbd{M-x}, or by way of incremental regexp search
503incremental regexp search with @kbd{C-M-s @key{RET}} and @kbd{C-M-r 514with @kbd{C-M-s @key{RET}} and @kbd{C-M-r @key{RET}}.
504@key{RET}}.
505 515
506 If you use the incremental regexp search commands with a prefix 516 If you use the incremental regexp search commands with a prefix
507argument, they perform ordinary string search, like 517argument, they perform ordinary string search, like
@@ -881,43 +891,30 @@ setting of the syntax table (@pxref{Syntax}).
881@node Regexp Example 891@node Regexp Example
882@section Regular Expression Example 892@section Regular Expression Example
883 893
884 Here is a complicated regexp---a simplified version of the regexp 894 Here is an example of a regexp---the regexp that Emacs uses, by
885that Emacs uses, by default, to recognize the end of a sentence 895default, to recognize the end of a sentence, not including the
886together with any whitespace that follows. We show its Lisp syntax to 896following space (i.e., the variable @code{sentence-end-base}):
887distinguish the spaces from the tab characters. In Lisp syntax, the
888string constant begins and ends with a double-quote. @samp{\"} stands
889for a double-quote as part of the regexp, @samp{\\} for a backslash as
890part of the regexp, @samp{\t} for a tab, and @samp{\n} for a newline.
891 897
892@example 898@example
893"[.?!][]\"')]*\\($\\| $\\|\t\\| \\)[ \t\n]*" 899@verbatim
900[.?!][]\"'””)}]*
901@end verbatim
894@end example 902@end example
895 903
896@noindent 904@noindent
897This contains four parts in succession: a character set matching 905This contains two parts in succession: a character set matching
898period, @samp{?}, or @samp{!}; a character set matching 906period, @samp{?}, or @samp{!}, and a character set matching
899close-brackets, quotes, or parentheses, repeated zero or more times; a 907close-brackets, quotes, or parentheses, repeated zero or more times.
900set of alternatives within backslash-parentheses that matches either
901end-of-line, a space at the end of a line, a tab, or two spaces; and a
902character set matching whitespace characters, repeated any number of
903times.
904
905 To enter the same regexp in incremental search, you would type
906@key{TAB} to enter a tab, and @kbd{C-j} to enter a newline. You would
907also type single backslashes as themselves, instead of doubling them
908for Lisp syntax. In commands that use ordinary minibuffer input to
909read a regexp, you would quote the @kbd{C-j} by preceding it with a
910@kbd{C-q} to prevent @kbd{C-j} from exiting the minibuffer.
911 908
912@node Search Case 909@node Search Case
913@section Searching and Case 910@section Searching and Case
914 911
915 Incremental searches in Emacs normally ignore the case of the text 912 Searches in Emacs normally ignore the case of the text they are
916they are searching through, if you specify the text in lower case. 913searching through, if you specify the text in lower case. Thus, if
917Thus, if you specify searching for @samp{foo}, then @samp{Foo} and 914you specify searching for @samp{foo}, then @samp{Foo} and @samp{foo}
918@samp{foo} are also considered a match. Regexps, and in particular 915are also considered a match. Regexps, and in particular character
919character sets, are included: @samp{[ab]} would match @samp{a} or 916sets, are included: @samp{[ab]} would match @samp{a} or @samp{A} or
920@samp{A} or @samp{b} or @samp{B}.@refill 917@samp{b} or @samp{B}.@refill
921 918
922 An upper-case letter anywhere in the incremental search string makes 919 An upper-case letter anywhere in the incremental search string makes
923the search case-sensitive. Thus, searching for @samp{Foo} does not find 920the search case-sensitive. Thus, searching for @samp{Foo} does not find
@@ -928,7 +925,8 @@ upper-case letter from the search string.
928 Typing @kbd{M-c} within an incremental search toggles the case 925 Typing @kbd{M-c} within an incremental search toggles the case
929sensitivity of that search. The effect does not extend beyond the 926sensitivity of that search. The effect does not extend beyond the
930current incremental search to the next one, but it does override the 927current incremental search to the next one, but it does override the
931effect of including an upper-case letter in the current search. 928effect of adding or removing an upper-case letter in the current
929search.
932 930
933@vindex case-fold-search 931@vindex case-fold-search
934@vindex default-case-fold-search 932@vindex default-case-fold-search
@@ -954,18 +952,17 @@ apropos-variable @key{RET} case-fold-search @key{RET}}.
954@cindex string substitution 952@cindex string substitution
955@cindex global substitution 953@cindex global substitution
956 954
957 Global search-and-replace operations are not needed often in Emacs, 955 Emacs provides several commands for performing search-and-replace
958but they are available. In addition to the simple @kbd{M-x 956operations. In addition to the simple @kbd{M-x replace-string}
959replace-string} command which replaces all occurrences, 957command, there is @kbd{M-%} (@code{query-replace}), which presents
960there is @kbd{M-%} (@code{query-replace}), which presents each occurrence 958each occurrence of the pattern and asks you whether to replace it.
961of the pattern and asks you whether to replace it.
962 959
963 The replace commands normally operate on the text from point to the 960 The replace commands normally operate on the text from point to the
964end of the buffer. When the mark is active, they operate on the 961end of the buffer. When the mark is active, they operate on the
965region instead (@pxref{Mark}). The basic replace commands replace one 962region instead (@pxref{Mark}). The basic replace commands replace one
966string (or regexp) with one replacement string. It is possible to 963@dfn{search string} (or regexp) with one @dfn{replacement string}. It
967perform several replacements in parallel using the command 964is possible to perform several replacements in parallel, using the
968@code{expand-region-abbrevs} (@pxref{Expanding Abbrevs}). 965command @code{expand-region-abbrevs} (@pxref{Expanding Abbrevs}).
969 966
970@menu 967@menu
971* Unconditional Replace:: Replacing all matches for a string. 968* Unconditional Replace:: Replacing all matches for a string.
@@ -988,10 +985,9 @@ use the command @kbd{M-x replace-string} with the two arguments
988@samp{foo} and @samp{bar}. Replacement happens only in the text after 985@samp{foo} and @samp{bar}. Replacement happens only in the text after
989point, so if you want to cover the whole buffer you must go to the 986point, so if you want to cover the whole buffer you must go to the
990beginning first. All occurrences up to the end of the buffer are 987beginning first. All occurrences up to the end of the buffer are
991replaced; to limit replacement to part of the buffer, narrow to that 988replaced; to limit replacement to part of the buffer, activate the
992part of the buffer before doing the replacement (@pxref{Narrowing}). 989region around that part. When the region is active, replacement is
993When the region is active, replacement is limited to the region 990limited to the region (@pxref{Mark}).
994(@pxref{Mark}).
995 991
996 When @code{replace-string} exits, it leaves point at the last 992 When @code{replace-string} exits, it leaves point at the last
997occurrence replaced. It adds the prior position of point (where the 993occurrence replaced. It adds the prior position of point (where the
@@ -999,24 +995,12 @@ occurrence replaced. It adds the prior position of point (where the
999activating the mark; use @kbd{C-u C-@key{SPC}} to move back there. 995activating the mark; use @kbd{C-u C-@key{SPC}} to move back there.
1000@xref{Mark Ring}. 996@xref{Mark Ring}.
1001 997
1002 A numeric argument restricts replacement to matches that are surrounded 998 A prefix argument restricts replacement to matches that are
1003by word boundaries. The argument's value doesn't matter. 999surrounded by word boundaries. The argument's value doesn't matter.
1004 1000
1005 @xref{Replacement and Case}, for details about case-sensitivity in 1001 @xref{Replacement and Case}, for details about case-sensitivity in
1006replace commands. 1002replace commands.
1007 1003
1008 What if you want to exchange @samp{x} and @samp{y}: replace every @samp{x} with a @samp{y} and vice versa? You can do it this way:
1009
1010@example
1011M-x replace-string @key{RET} x @key{RET} @@TEMP@@ @key{RET}
1012M-< M-x replace-string @key{RET} y @key{RET} x @key{RET}
1013M-< M-x replace-string @key{RET} @@TEMP@@ @key{RET} y @key{RET}
1014@end example
1015
1016@noindent
1017This works provided the string @samp{@@TEMP@@} does not appear
1018in your text.
1019
1020@node Regexp Replace, Replacement and Case, Unconditional Replace, Replace 1004@node Regexp Replace, Replacement and Case, Unconditional Replace, Replace
1021@subsection Regexp Replacement 1005@subsection Regexp Replacement
1022@findex replace-regexp 1006@findex replace-regexp
@@ -1316,6 +1300,12 @@ does not select it.
1316After using @kbd{M-x occur}, you can use @code{next-error} to visit 1300After using @kbd{M-x occur}, you can use @code{next-error} to visit
1317the occurrences found, one by one. @ref{Compilation Mode}. 1301the occurrences found, one by one. @ref{Compilation Mode}.
1318 1302
1303@kindex M-s o
1304@item M-s o
1305Run @code{occur} using the search string of the last incremental
1306string search. You can also run @kbd{M-s o} which an incremental
1307search is active; this uses the current search string.
1308
1319@item M-x list-matching-lines 1309@item M-x list-matching-lines
1320Synonym for @kbd{M-x occur}. 1310Synonym for @kbd{M-x occur}.
1321 1311