| 1 |
This is ../info/emacs, produced by makeinfo version 4.3 from emacs.texi. |
|---|
| 2 |
|
|---|
| 3 |
This is the Fourteenth edition of the `GNU Emacs Manual', updated |
|---|
| 4 |
for Emacs version 21.3. |
|---|
| 5 |
|
|---|
| 6 |
INFO-DIR-SECTION Emacs |
|---|
| 7 |
START-INFO-DIR-ENTRY |
|---|
| 8 |
* Emacs: (emacs). The extensible self-documenting text editor. |
|---|
| 9 |
END-INFO-DIR-ENTRY |
|---|
| 10 |
|
|---|
| 11 |
Published by the Free Software Foundation 59 Temple Place, Suite 330 |
|---|
| 12 |
Boston, MA 02111-1307 USA |
|---|
| 13 |
|
|---|
| 14 |
Copyright (C) |
|---|
| 15 |
1985,1986,1987,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002 |
|---|
| 16 |
Free Software Foundation, Inc. |
|---|
| 17 |
|
|---|
| 18 |
Permission is granted to copy, distribute and/or modify this document |
|---|
| 19 |
under the terms of the GNU Free Documentation License, Version 1.1 or |
|---|
| 20 |
any later version published by the Free Software Foundation; with the |
|---|
| 21 |
Invariant Sections being "The GNU Manifesto", "Distribution" and "GNU |
|---|
| 22 |
GENERAL PUBLIC LICENSE", with the Front-Cover texts being "A GNU |
|---|
| 23 |
Manual," and with the Back-Cover Texts as in (a) below. A copy of the |
|---|
| 24 |
license is included in the section entitled "GNU Free Documentation |
|---|
| 25 |
License." |
|---|
| 26 |
|
|---|
| 27 |
(a) The FSF's Back-Cover Text is: "You have freedom to copy and |
|---|
| 28 |
modify this GNU Manual, like GNU software. Copies published by the Free |
|---|
| 29 |
Software Foundation raise funds for GNU development." |
|---|
| 30 |
|
|---|
| 31 |
|
|---|
| 32 |
File: emacs, Node: Incremental Search, Next: Nonincremental Search, Prev: Search, Up: Search |
|---|
| 33 |
|
|---|
| 34 |
Incremental Search |
|---|
| 35 |
================== |
|---|
| 36 |
|
|---|
| 37 |
An incremental search begins searching as soon as you type the first |
|---|
| 38 |
character of the search string. As you type in the search string, Emacs |
|---|
| 39 |
shows you where the string (as you have typed it so far) would be |
|---|
| 40 |
found. When you have typed enough characters to identify the place you |
|---|
| 41 |
want, you can stop. Depending on what you plan to do next, you may or |
|---|
| 42 |
may not need to terminate the search explicitly with <RET>. |
|---|
| 43 |
|
|---|
| 44 |
`C-s' |
|---|
| 45 |
Incremental search forward (`isearch-forward'). |
|---|
| 46 |
|
|---|
| 47 |
`C-r' |
|---|
| 48 |
Incremental search backward (`isearch-backward'). |
|---|
| 49 |
|
|---|
| 50 |
`C-s' starts a forward incremental search. It reads characters from |
|---|
| 51 |
the keyboard, and moves point past the next occurrence of those |
|---|
| 52 |
characters. If you type `C-s' and then `F', that puts the cursor after |
|---|
| 53 |
the first `F' (the first following the starting point, since this is a |
|---|
| 54 |
forward search). Then if you type an `O', you will see the cursor move |
|---|
| 55 |
just after the first `FO' (the `F' in that `FO' may or may not be the |
|---|
| 56 |
first `F'). After another `O', the cursor moves after the first `FOO' |
|---|
| 57 |
after the place where you started the search. At each step, the buffer |
|---|
| 58 |
text that matches the search string is highlighted, if the terminal can |
|---|
| 59 |
do that; the current search string is always displayed in the echo area. |
|---|
| 60 |
|
|---|
| 61 |
If you make a mistake in typing the search string, you can cancel |
|---|
| 62 |
characters with <DEL>. Each <DEL> cancels the last character of search |
|---|
| 63 |
string. This does not happen until Emacs is ready to read another |
|---|
| 64 |
input character; first it must either find, or fail to find, the |
|---|
| 65 |
character you want to erase. If you do not want to wait for this to |
|---|
| 66 |
happen, use `C-g' as described below. |
|---|
| 67 |
|
|---|
| 68 |
When you are satisfied with the place you have reached, you can type |
|---|
| 69 |
<RET>, which stops searching, leaving the cursor where the search |
|---|
| 70 |
brought it. Also, any command not specially meaningful in searches |
|---|
| 71 |
stops the searching and is then executed. Thus, typing `C-a' would |
|---|
| 72 |
exit the search and then move to the beginning of the line. <RET> is |
|---|
| 73 |
necessary only if the next command you want to type is a printing |
|---|
| 74 |
character, <DEL>, <RET>, or another character that is special within |
|---|
| 75 |
searches (`C-q', `C-w', `C-r', `C-s', `C-y', `M-y', `M-r', `M-s', and |
|---|
| 76 |
some other meta-characters). |
|---|
| 77 |
|
|---|
| 78 |
Sometimes you search for `FOO' and find one, but not the one you |
|---|
| 79 |
expected to find. There was a second `FOO' that you forgot about, |
|---|
| 80 |
before the one you were aiming for. In this event, type another `C-s' |
|---|
| 81 |
to move to the next occurrence of the search string. You can repeat |
|---|
| 82 |
this any number of times. If you overshoot, you can cancel some `C-s' |
|---|
| 83 |
characters with <DEL>. |
|---|
| 84 |
|
|---|
| 85 |
After you exit a search, you can search for the same string again by |
|---|
| 86 |
typing just `C-s C-s': the first `C-s' is the key that invokes |
|---|
| 87 |
incremental search, and the second `C-s' means "search again." |
|---|
| 88 |
|
|---|
| 89 |
To reuse earlier search strings, use the "search ring". The |
|---|
| 90 |
commands `M-p' and `M-n' move through the ring to pick a search string |
|---|
| 91 |
to reuse. These commands leave the selected search ring element in the |
|---|
| 92 |
minibuffer, where you can edit it. Type `C-s' or `C-r' to terminate |
|---|
| 93 |
editing the string and search for it. |
|---|
| 94 |
|
|---|
| 95 |
If your string is not found at all, the echo area says `Failing |
|---|
| 96 |
I-Search'. The cursor is after the place where Emacs found as much of |
|---|
| 97 |
your string as it could. Thus, if you search for `FOOT', and there is |
|---|
| 98 |
no `FOOT', you might see the cursor after the `FOO' in `FOOL'. At this |
|---|
| 99 |
point there are several things you can do. If your string was |
|---|
| 100 |
mistyped, you can rub some of it out and correct it. If you like the |
|---|
| 101 |
place you have found, you can type <RET> or some other Emacs command to |
|---|
| 102 |
remain there. Or you can type `C-g', which removes from the search |
|---|
| 103 |
string the characters that could not be found (the `T' in `FOOT'), |
|---|
| 104 |
leaving those that were found (the `FOO' in `FOOT'). A second `C-g' at |
|---|
| 105 |
that point cancels the search entirely, returning point to where it was |
|---|
| 106 |
when the search started. |
|---|
| 107 |
|
|---|
| 108 |
An upper-case letter in the search string makes the search |
|---|
| 109 |
case-sensitive. If you delete the upper-case character from the search |
|---|
| 110 |
string, it ceases to have this effect. *Note Search Case::. |
|---|
| 111 |
|
|---|
| 112 |
To search for a newline, type `C-j'. To search for another control |
|---|
| 113 |
character, such as control-S or carriage return, you must quote it by |
|---|
| 114 |
typing `C-q' first. This function of `C-q' is analogous to its use for |
|---|
| 115 |
insertion (*note Inserting Text::): it causes the following character |
|---|
| 116 |
to be treated the way any "ordinary" character is treated in the same |
|---|
| 117 |
context. You can also specify a character by its octal code: enter |
|---|
| 118 |
`C-q' followed by a sequence of octal digits. |
|---|
| 119 |
|
|---|
| 120 |
To search for non-ASCII characters, you must use an input method |
|---|
| 121 |
(*note Input Methods::). If an input method is enabled in the current |
|---|
| 122 |
buffer when you start the search, you can use it while you type the |
|---|
| 123 |
search string also. Emacs indicates that by including the input method |
|---|
| 124 |
mnemonic in its prompt, like this: |
|---|
| 125 |
|
|---|
| 126 |
I-search [IM]: |
|---|
| 127 |
|
|---|
| 128 |
where IM is the mnemonic of the active input method. You can toggle |
|---|
| 129 |
(enable or disable) the input method while you type the search string |
|---|
| 130 |
with `C-\' (`isearch-toggle-input-method'). You can turn on a certain |
|---|
| 131 |
(non-default) input method with `C-^' |
|---|
| 132 |
(`isearch-toggle-specified-input-method'), which prompts for the name |
|---|
| 133 |
of the input method. The input method you enable during incremental |
|---|
| 134 |
search remains enabled in the current buffer afterwards. |
|---|
| 135 |
|
|---|
| 136 |
If a search is failing and you ask to repeat it by typing another |
|---|
| 137 |
`C-s', it starts again from the beginning of the buffer. Repeating a |
|---|
| 138 |
failing reverse search with `C-r' starts again from the end. This is |
|---|
| 139 |
called "wrapping around", and `Wrapped' appears in the search prompt |
|---|
| 140 |
once this has happened. If you keep on going past the original |
|---|
| 141 |
starting point of the search, it changes to `Overwrapped', which means |
|---|
| 142 |
that you are revisiting matches that you have already seen. |
|---|
| 143 |
|
|---|
| 144 |
The `C-g' "quit" character does special things during searches; just |
|---|
| 145 |
what it does depends on the status of the search. If the search has |
|---|
| 146 |
found what you specified and is waiting for input, `C-g' cancels the |
|---|
| 147 |
entire search. The cursor moves back to where you started the search. |
|---|
| 148 |
If `C-g' is typed when there are characters in the search string that |
|---|
| 149 |
have not been found--because Emacs is still searching for them, or |
|---|
| 150 |
because it has failed to find them--then the search string characters |
|---|
| 151 |
which have not been found are discarded from the search string. With |
|---|
| 152 |
them gone, the search is now successful and waiting for more input, so |
|---|
| 153 |
a second `C-g' will cancel the entire search. |
|---|
| 154 |
|
|---|
| 155 |
You can change to searching backwards with `C-r'. If a search fails |
|---|
| 156 |
because the place you started was too late in the file, you should do |
|---|
| 157 |
this. Repeated `C-r' keeps looking for more occurrences backwards. A |
|---|
| 158 |
`C-s' starts going forwards again. `C-r' in a search can be canceled |
|---|
| 159 |
with <DEL>. |
|---|
| 160 |
|
|---|
| 161 |
If you know initially that you want to search backwards, you can use |
|---|
| 162 |
`C-r' instead of `C-s' to start the search, because `C-r' as a key runs |
|---|
| 163 |
a command (`isearch-backward') to search backward. A backward search |
|---|
| 164 |
finds matches that are entirely before the starting point, just as a |
|---|
| 165 |
forward search finds matches that begin after it. |
|---|
| 166 |
|
|---|
| 167 |
The characters `C-y' and `C-w' can be used in incremental search to |
|---|
| 168 |
grab text from the buffer into the search string. This makes it |
|---|
| 169 |
convenient to search for another occurrence of text at point. `C-w' |
|---|
| 170 |
copies the word after point as part of the search string, advancing |
|---|
| 171 |
point over that word. Another `C-s' to repeat the search will then |
|---|
| 172 |
search for a string including that word. `C-y' is similar to `C-w' but |
|---|
| 173 |
copies all the rest of the current line into the search string. Both |
|---|
| 174 |
`C-y' and `C-w' convert the text they copy to lower case if the search |
|---|
| 175 |
is currently not case-sensitive; this is so the search remains |
|---|
| 176 |
case-insensitive. |
|---|
| 177 |
|
|---|
| 178 |
The character `M-y' copies text from the kill ring into the search |
|---|
| 179 |
string. It uses the same text that `C-y' as a command would yank. |
|---|
| 180 |
`Mouse-2' in the echo area does the same. *Note Yanking::. |
|---|
| 181 |
|
|---|
| 182 |
When you exit the incremental search, it sets the mark to where point |
|---|
| 183 |
_was_, before the search. That is convenient for moving back there. |
|---|
| 184 |
In Transient Mark mode, incremental search sets the mark without |
|---|
| 185 |
activating it, and does so only if the mark is not already active. |
|---|
| 186 |
|
|---|
| 187 |
When you pause for a little while during incremental search, it |
|---|
| 188 |
highlights all other possible matches for the search string. This |
|---|
| 189 |
makes it easier to anticipate where you can get to by typing `C-s' or |
|---|
| 190 |
`C-r' to repeat the search. The short delay before highlighting other |
|---|
| 191 |
matches helps indicate which match is the current one. If you don't |
|---|
| 192 |
like this feature, you can turn it off by setting |
|---|
| 193 |
`isearch-lazy-highlight' to `nil'. |
|---|
| 194 |
|
|---|
| 195 |
You can control how this highlighting looks by customizing the faces |
|---|
| 196 |
`isearch' (used for the current match) and |
|---|
| 197 |
`isearch-lazy-highlight-face' (for all the other matches). |
|---|
| 198 |
|
|---|
| 199 |
To customize the special characters that incremental search |
|---|
| 200 |
understands, alter their bindings in the keymap `isearch-mode-map'. |
|---|
| 201 |
For a list of bindings, look at the documentation of `isearch-mode' with |
|---|
| 202 |
`C-h f isearch-mode <RET>'. |
|---|
| 203 |
|
|---|
| 204 |
Slow Terminal Incremental Search |
|---|
| 205 |
-------------------------------- |
|---|
| 206 |
|
|---|
| 207 |
Incremental search on a slow terminal uses a modified style of |
|---|
| 208 |
display that is designed to take less time. Instead of redisplaying |
|---|
| 209 |
the buffer at each place the search gets to, it creates a new |
|---|
| 210 |
single-line window and uses that to display the line that the search |
|---|
| 211 |
has found. The single-line window comes into play as soon as point |
|---|
| 212 |
moves outside of the text that is already on the screen. |
|---|
| 213 |
|
|---|
| 214 |
When you terminate the search, the single-line window is removed. |
|---|
| 215 |
Emacs then redisplays the window in which the search was done, to show |
|---|
| 216 |
its new position of point. |
|---|
| 217 |
|
|---|
| 218 |
The slow terminal style of display is used when the terminal baud |
|---|
| 219 |
rate is less than or equal to the value of the variable |
|---|
| 220 |
`search-slow-speed', initially 1200. See `baud-rate' in *Note Display |
|---|
| 221 |
Custom::. |
|---|
| 222 |
|
|---|
| 223 |
The number of lines to use in slow terminal search display is |
|---|
| 224 |
controlled by the variable `search-slow-window-lines'. Its normal |
|---|
| 225 |
value is 1. |
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
File: emacs, Node: Nonincremental Search, Next: Word Search, Prev: Incremental Search, Up: Search |
|---|
| 229 |
|
|---|
| 230 |
Nonincremental Search |
|---|
| 231 |
===================== |
|---|
| 232 |
|
|---|
| 233 |
Emacs also has conventional nonincremental search commands, which |
|---|
| 234 |
require you to type the entire search string before searching begins. |
|---|
| 235 |
|
|---|
| 236 |
`C-s <RET> STRING <RET>' |
|---|
| 237 |
Search for STRING. |
|---|
| 238 |
|
|---|
| 239 |
`C-r <RET> STRING <RET>' |
|---|
| 240 |
Search backward for STRING. |
|---|
| 241 |
|
|---|
| 242 |
To do a nonincremental search, first type `C-s <RET>'. This enters |
|---|
| 243 |
the minibuffer to read the search string; terminate the string with |
|---|
| 244 |
<RET>, and then the search takes place. If the string is not found, |
|---|
| 245 |
the search command signals an error. |
|---|
| 246 |
|
|---|
| 247 |
When you type `C-s <RET>', the `C-s' invokes incremental search as |
|---|
| 248 |
usual. That command is specially programmed to invoke nonincremental |
|---|
| 249 |
search, `search-forward', if the string you specify is empty. (Such an |
|---|
| 250 |
empty argument would otherwise be useless.) But it does not call |
|---|
| 251 |
`search-forward' right away. First it checks the next input character |
|---|
| 252 |
to see if is `C-w', which specifies a word search. *Note Word Search::. |
|---|
| 253 |
`C-r <RET>' does likewise, for a reverse incremental search. |
|---|
| 254 |
|
|---|
| 255 |
Forward and backward nonincremental searches are implemented by the |
|---|
| 256 |
commands `search-forward' and `search-backward'. These commands may be |
|---|
| 257 |
bound to keys in the usual manner. The feature that you can get to |
|---|
| 258 |
them via the incremental search commands exists for historical reasons, |
|---|
| 259 |
and to avoid the need to find key sequences for them. |
|---|
| 260 |
|
|---|
| 261 |
|
|---|
| 262 |
File: emacs, Node: Word Search, Next: Regexp Search, Prev: Nonincremental Search, Up: Search |
|---|
| 263 |
|
|---|
| 264 |
Word Search |
|---|
| 265 |
=========== |
|---|
| 266 |
|
|---|
| 267 |
Word search searches for a sequence of words without regard to how |
|---|
| 268 |
the words are separated. More precisely, you type a string of many |
|---|
| 269 |
words, using single spaces to separate them, and the string can be |
|---|
| 270 |
found even if there are multiple spaces, newlines, or other punctuation |
|---|
| 271 |
characters between these words. |
|---|
| 272 |
|
|---|
| 273 |
Word search is useful for editing a printed document made with a text |
|---|
| 274 |
formatter. If you edit while looking at the printed, formatted version, |
|---|
| 275 |
you can't tell where the line breaks are in the source file. With word |
|---|
| 276 |
search, you can search without having to know them. |
|---|
| 277 |
|
|---|
| 278 |
`C-s <RET> C-w WORDS <RET>' |
|---|
| 279 |
Search for WORDS, ignoring details of punctuation. |
|---|
| 280 |
|
|---|
| 281 |
`C-r <RET> C-w WORDS <RET>' |
|---|
| 282 |
Search backward for WORDS, ignoring details of punctuation. |
|---|
| 283 |
|
|---|
| 284 |
Word search is a special case of nonincremental search and is invoked |
|---|
| 285 |
with `C-s <RET> C-w'. This is followed by the search string, which |
|---|
| 286 |
must always be terminated with <RET>. Being nonincremental, this |
|---|
| 287 |
search does not start until the argument is terminated. It works by |
|---|
| 288 |
constructing a regular expression and searching for that; see *Note |
|---|
| 289 |
Regexp Search::. |
|---|
| 290 |
|
|---|
| 291 |
Use `C-r <RET> C-w' to do backward word search. |
|---|
| 292 |
|
|---|
| 293 |
Forward and backward word searches are implemented by the commands |
|---|
| 294 |
`word-search-forward' and `word-search-backward'. These commands may |
|---|
| 295 |
be bound to keys in the usual manner. They are available via the |
|---|
| 296 |
incremental search commands both for historical reasons and to avoid |
|---|
| 297 |
the need to find suitable key sequences for them. |
|---|
| 298 |
|
|---|
| 299 |
|
|---|
| 300 |
File: emacs, Node: Regexp Search, Next: Regexps, Prev: Word Search, Up: Search |
|---|
| 301 |
|
|---|
| 302 |
Regular Expression Search |
|---|
| 303 |
========================= |
|---|
| 304 |
|
|---|
| 305 |
A "regular expression" ("regexp", for short) is a pattern that |
|---|
| 306 |
denotes a class of alternative strings to match, possibly infinitely |
|---|
| 307 |
many. GNU Emacs provides both incremental and nonincremental ways to |
|---|
| 308 |
search for a match for a regexp. |
|---|
| 309 |
|
|---|
| 310 |
Incremental search for a regexp is done by typing `C-M-s' |
|---|
| 311 |
(`isearch-forward-regexp'), or by invoking `C-s' with a prefix argument |
|---|
| 312 |
(whose value does not matter). This command reads a search string |
|---|
| 313 |
incrementally just like `C-s', but it treats the search string as a |
|---|
| 314 |
regexp rather than looking for an exact match against the text in the |
|---|
| 315 |
buffer. Each time you add text to the search string, you make the |
|---|
| 316 |
regexp longer, and the new regexp is searched for. To search backward |
|---|
| 317 |
for a regexp, use `C-M-r' (`isearch-backward-regexp'), or `C-r' with a |
|---|
| 318 |
prefix argument. |
|---|
| 319 |
|
|---|
| 320 |
All of the control characters that do special things within an |
|---|
| 321 |
ordinary incremental search have the same function in incremental regexp |
|---|
| 322 |
search. Typing `C-s' or `C-r' immediately after starting the search |
|---|
| 323 |
retrieves the last incremental search regexp used; that is to say, |
|---|
| 324 |
incremental regexp and non-regexp searches have independent defaults. |
|---|
| 325 |
They also have separate search rings that you can access with `M-p' and |
|---|
| 326 |
`M-n'. |
|---|
| 327 |
|
|---|
| 328 |
If you type <SPC> in incremental regexp search, it matches any |
|---|
| 329 |
sequence of whitespace characters, including newlines. If you want to |
|---|
| 330 |
match just a space, type `C-q <SPC>'. |
|---|
| 331 |
|
|---|
| 332 |
Note that adding characters to the regexp in an incremental regexp |
|---|
| 333 |
search can make the cursor move back and start again. For example, if |
|---|
| 334 |
you have searched for `foo' and you add `\|bar', the cursor backs up in |
|---|
| 335 |
case the first `bar' precedes the first `foo'. |
|---|
| 336 |
|
|---|
| 337 |
Nonincremental search for a regexp is done by the functions |
|---|
| 338 |
`re-search-forward' and `re-search-backward'. You can invoke these |
|---|
| 339 |
with `M-x', or bind them to keys, or invoke them by way of incremental |
|---|
| 340 |
regexp search with `C-M-s <RET>' and `C-M-r <RET>'. |
|---|
| 341 |
|
|---|
| 342 |
If you use the incremental regexp search commands with a prefix |
|---|
| 343 |
argument, they perform ordinary string search, like `isearch-forward' |
|---|
| 344 |
and `isearch-backward'. *Note Incremental Search::. |
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
File: emacs, Node: Regexps, Next: Search Case, Prev: Regexp Search, Up: Search |
|---|
| 348 |
|
|---|
| 349 |
Syntax of Regular Expressions |
|---|
| 350 |
============================= |
|---|
| 351 |
|
|---|
| 352 |
Regular expressions have a syntax in which a few characters are |
|---|
| 353 |
special constructs and the rest are "ordinary". An ordinary character |
|---|
| 354 |
is a simple regular expression which matches that same character and |
|---|
| 355 |
nothing else. The special characters are `$', `^', `.', `*', `+', `?', |
|---|
| 356 |
`[', `]' and `\'. Any other character appearing in a regular |
|---|
| 357 |
expression is ordinary, unless a `\' precedes it. (When you use regular |
|---|
| 358 |
expressions in a Lisp program, each `\' must be doubled, see the |
|---|
| 359 |
example near the end of this section.) |
|---|
| 360 |
|
|---|
| 361 |
For example, `f' is not a special character, so it is ordinary, and |
|---|
| 362 |
therefore `f' is a regular expression that matches the string `f' and |
|---|
| 363 |
no other string. (It does _not_ match the string `ff'.) Likewise, `o' |
|---|
| 364 |
is a regular expression that matches only `o'. (When case distinctions |
|---|
| 365 |
are being ignored, these regexps also match `F' and `O', but we |
|---|
| 366 |
consider this a generalization of "the same string," rather than an |
|---|
| 367 |
exception.) |
|---|
| 368 |
|
|---|
| 369 |
Any two regular expressions A and B can be concatenated. The result |
|---|
| 370 |
is a regular expression which matches a string if A matches some amount |
|---|
| 371 |
of the beginning of that string and B matches the rest of the string. |
|---|
| 372 |
|
|---|
| 373 |
As a simple example, we can concatenate the regular expressions `f' |
|---|
| 374 |
and `o' to get the regular expression `fo', which matches only the |
|---|
| 375 |
string `fo'. Still trivial. To do something nontrivial, you need to |
|---|
| 376 |
use one of the special characters. Here is a list of them. |
|---|
| 377 |
|
|---|
| 378 |
`. (Period)' |
|---|
| 379 |
is a special character that matches any single character except a |
|---|
| 380 |
newline. Using concatenation, we can make regular expressions |
|---|
| 381 |
like `a.b', which matches any three-character string that begins |
|---|
| 382 |
with `a' and ends with `b'. |
|---|
| 383 |
|
|---|
| 384 |
`*' |
|---|
| 385 |
is not a construct by itself; it is a postfix operator that means |
|---|
| 386 |
to match the preceding regular expression repetitively as many |
|---|
| 387 |
times as possible. Thus, `o*' matches any number of `o's |
|---|
| 388 |
(including no `o's). |
|---|
| 389 |
|
|---|
| 390 |
`*' always applies to the _smallest_ possible preceding |
|---|
| 391 |
expression. Thus, `fo*' has a repeating `o', not a repeating |
|---|
| 392 |
`fo'. It matches `f', `fo', `foo', and so on. |
|---|
| 393 |
|
|---|
| 394 |
The matcher processes a `*' construct by matching, immediately, as |
|---|
| 395 |
many repetitions as can be found. Then it continues with the rest |
|---|
| 396 |
of the pattern. If that fails, backtracking occurs, discarding |
|---|
| 397 |
some of the matches of the `*'-modified construct in case that |
|---|
| 398 |
makes it possible to match the rest of the pattern. For example, |
|---|
| 399 |
in matching `ca*ar' against the string `caaar', the `a*' first |
|---|
| 400 |
tries to match all three `a's; but the rest of the pattern is `ar' |
|---|
| 401 |
and there is only `r' left to match, so this try fails. The next |
|---|
| 402 |
alternative is for `a*' to match only two `a's. With this choice, |
|---|
| 403 |
the rest of the regexp matches successfully. |
|---|
| 404 |
|
|---|
| 405 |
`+' |
|---|
| 406 |
is a postfix operator, similar to `*' except that it must match |
|---|
| 407 |
the preceding expression at least once. So, for example, `ca+r' |
|---|
| 408 |
matches the strings `car' and `caaaar' but not the string `cr', |
|---|
| 409 |
whereas `ca*r' matches all three strings. |
|---|
| 410 |
|
|---|
| 411 |
`?' |
|---|
| 412 |
is a postfix operator, similar to `*' except that it can match the |
|---|
| 413 |
preceding expression either once or not at all. For example, |
|---|
| 414 |
`ca?r' matches `car' or `cr'; nothing else. |
|---|
| 415 |
|
|---|
| 416 |
`*?, +?, ??' |
|---|
| 417 |
are non-greedy variants of the operators above. The normal |
|---|
| 418 |
operators `*', `+', `?' are "greedy" in that they match as much as |
|---|
| 419 |
they can, as long as the overall regexp can still match. With a |
|---|
| 420 |
following `?', they are non-greedy: they will match as little as |
|---|
| 421 |
possible. |
|---|
| 422 |
|
|---|
| 423 |
Thus, both `ab*' and `ab*?' can match the string `a' and the |
|---|
| 424 |
string `abbbb'; but if you try to match them both against the text |
|---|
| 425 |
`abbb', `ab*' will match it all (the longest valid match), while |
|---|
| 426 |
`ab*?' will match just `a' (the shortest valid match). |
|---|
| 427 |
|
|---|
| 428 |
`\{N\}' |
|---|
| 429 |
is a postfix operator that specifies repetition N times--that is, |
|---|
| 430 |
the preceding regular expression must match exactly N times in a |
|---|
| 431 |
row. For example, `x\{4\}' matches the string `xxxx' and nothing |
|---|
| 432 |
else. |
|---|
| 433 |
|
|---|
| 434 |
`\{N,M\}' |
|---|
| 435 |
is a postfix operator that specifies repetition between N and M |
|---|
| 436 |
times--that is, the preceding regular expression must match at |
|---|
| 437 |
least N times, but no more than M times. If M is omitted, then |
|---|
| 438 |
there is no upper limit, but the preceding regular expression must |
|---|
| 439 |
match at least N times. |
|---|
| 440 |
`\{0,1\}' is equivalent to `?'. |
|---|
| 441 |
`\{0,\}' is equivalent to `*'. |
|---|
| 442 |
`\{1,\}' is equivalent to `+'. |
|---|
| 443 |
|
|---|
| 444 |
`[ ... ]' |
|---|
| 445 |
is a "character set", which begins with `[' and is terminated by |
|---|
| 446 |
`]'. In the simplest case, the characters between the two |
|---|
| 447 |
brackets are what this set can match. |
|---|
| 448 |
|
|---|
| 449 |
Thus, `[ad]' matches either one `a' or one `d', and `[ad]*' |
|---|
| 450 |
matches any string composed of just `a's and `d's (including the |
|---|
| 451 |
empty string), from which it follows that `c[ad]*r' matches `cr', |
|---|
| 452 |
`car', `cdr', `caddaar', etc. |
|---|
| 453 |
|
|---|
| 454 |
You can also include character ranges in a character set, by |
|---|
| 455 |
writing the starting and ending characters with a `-' between |
|---|
| 456 |
them. Thus, `[a-z]' matches any lower-case ASCII letter. Ranges |
|---|
| 457 |
may be intermixed freely with individual characters, as in |
|---|
| 458 |
`[a-z$%.]', which matches any lower-case ASCII letter or `$', `%' |
|---|
| 459 |
or period. |
|---|
| 460 |
|
|---|
| 461 |
Note that the usual regexp special characters are not special |
|---|
| 462 |
inside a character set. A completely different set of special |
|---|
| 463 |
characters exists inside character sets: `]', `-' and `^'. |
|---|
| 464 |
|
|---|
| 465 |
To include a `]' in a character set, you must make it the first |
|---|
| 466 |
character. For example, `[]a]' matches `]' or `a'. To include a |
|---|
| 467 |
`-', write `-' as the first or last character of the set, or put |
|---|
| 468 |
it after a range. Thus, `[]-]' matches both `]' and `-'. |
|---|
| 469 |
|
|---|
| 470 |
To include `^' in a set, put it anywhere but at the beginning of |
|---|
| 471 |
the set. (At the beginning, it complements the set--see below.) |
|---|
| 472 |
|
|---|
| 473 |
When you use a range in case-insensitive search, you should write |
|---|
| 474 |
both ends of the range in upper case, or both in lower case, or |
|---|
| 475 |
both should be non-letters. The behavior of a mixed-case range |
|---|
| 476 |
such as `A-z' is somewhat ill-defined, and it may change in future |
|---|
| 477 |
Emacs versions. |
|---|
| 478 |
|
|---|
| 479 |
`[^ ... ]' |
|---|
| 480 |
`[^' begins a "complemented character set", which matches any |
|---|
| 481 |
character except the ones specified. Thus, `[^a-z0-9A-Z]' matches |
|---|
| 482 |
all characters _except_ ASCII letters and digits. |
|---|
| 483 |
|
|---|
| 484 |
`^' is not special in a character set unless it is the first |
|---|
| 485 |
character. The character following the `^' is treated as if it |
|---|
| 486 |
were first (in other words, `-' and `]' are not special there). |
|---|
| 487 |
|
|---|
| 488 |
A complemented character set can match a newline, unless newline is |
|---|
| 489 |
mentioned as one of the characters not to match. This is in |
|---|
| 490 |
contrast to the handling of regexps in programs such as `grep'. |
|---|
| 491 |
|
|---|
| 492 |
`^' |
|---|
| 493 |
is a special character that matches the empty string, but only at |
|---|
| 494 |
the beginning of a line in the text being matched. Otherwise it |
|---|
| 495 |
fails to match anything. Thus, `^foo' matches a `foo' that occurs |
|---|
| 496 |
at the beginning of a line. |
|---|
| 497 |
|
|---|
| 498 |
`$' |
|---|
| 499 |
is similar to `^' but matches only at the end of a line. Thus, |
|---|
| 500 |
`x+$' matches a string of one `x' or more at the end of a line. |
|---|
| 501 |
|
|---|
| 502 |
`\' |
|---|
| 503 |
has two functions: it quotes the special characters (including |
|---|
| 504 |
`\'), and it introduces additional special constructs. |
|---|
| 505 |
|
|---|
| 506 |
Because `\' quotes special characters, `\$' is a regular |
|---|
| 507 |
expression that matches only `$', and `\[' is a regular expression |
|---|
| 508 |
that matches only `[', and so on. |
|---|
| 509 |
|
|---|
| 510 |
Note: for historical compatibility, special characters are treated as |
|---|
| 511 |
ordinary ones if they are in contexts where their special meanings make |
|---|
| 512 |
no sense. For example, `*foo' treats `*' as ordinary since there is no |
|---|
| 513 |
preceding expression on which the `*' can act. It is poor practice to |
|---|
| 514 |
depend on this behavior; it is better to quote the special character |
|---|
| 515 |
anyway, regardless of where it appears. |
|---|
| 516 |
|
|---|
| 517 |
For the most part, `\' followed by any character matches only that |
|---|
| 518 |
character. However, there are several exceptions: two-character |
|---|
| 519 |
sequences starting with `\' that have special meanings. The second |
|---|
| 520 |
character in the sequence is always an ordinary character when used on |
|---|
| 521 |
its own. Here is a table of `\' constructs. |
|---|
| 522 |
|
|---|
| 523 |
`\|' |
|---|
| 524 |
specifies an alternative. Two regular expressions A and B with |
|---|
| 525 |
`\|' in between form an expression that matches some text if |
|---|
| 526 |
either A matches it or B matches it. It works by trying to match |
|---|
| 527 |
A, and if that fails, by trying to match B. |
|---|
| 528 |
|
|---|
| 529 |
Thus, `foo\|bar' matches either `foo' or `bar' but no other string. |
|---|
| 530 |
|
|---|
| 531 |
`\|' applies to the largest possible surrounding expressions. |
|---|
| 532 |
Only a surrounding `\( ... \)' grouping can limit the grouping |
|---|
| 533 |
power of `\|'. |
|---|
| 534 |
|
|---|
| 535 |
Full backtracking capability exists to handle multiple uses of |
|---|
| 536 |
`\|'. |
|---|
| 537 |
|
|---|
| 538 |
`\( ... \)' |
|---|
| 539 |
is a grouping construct that serves three purposes: |
|---|
| 540 |
|
|---|
| 541 |
1. To enclose a set of `\|' alternatives for other operations. |
|---|
| 542 |
Thus, `\(foo\|bar\)x' matches either `foox' or `barx'. |
|---|
| 543 |
|
|---|
| 544 |
2. To enclose a complicated expression for the postfix operators |
|---|
| 545 |
`*', `+' and `?' to operate on. Thus, `ba\(na\)*' matches |
|---|
| 546 |
`bananana', etc., with any (zero or more) number of `na' |
|---|
| 547 |
strings. |
|---|
| 548 |
|
|---|
| 549 |
3. To record a matched substring for future reference. |
|---|
| 550 |
|
|---|
| 551 |
This last application is not a consequence of the idea of a |
|---|
| 552 |
parenthetical grouping; it is a separate feature that is assigned |
|---|
| 553 |
as a second meaning to the same `\( ... \)' construct. In practice |
|---|
| 554 |
there is usually no conflict between the two meanings; when there |
|---|
| 555 |
is a conflict, you can use a "shy" group. |
|---|
| 556 |
|
|---|
| 557 |
`\(?: ... \)' |
|---|
| 558 |
specifies a "shy" group that does not record the matched substring; |
|---|
| 559 |
you can't refer back to it with `\D'. This is useful in |
|---|
| 560 |
mechanically combining regular expressions, so that you can add |
|---|
| 561 |
groups for syntactic purposes without interfering with the |
|---|
| 562 |
numbering of the groups that were written by the user. |
|---|
| 563 |
|
|---|
| 564 |
`\D' |
|---|
| 565 |
matches the same text that matched the Dth occurrence of a `\( ... |
|---|
| 566 |
\)' construct. |
|---|
| 567 |
|
|---|
| 568 |
After the end of a `\( ... \)' construct, the matcher remembers |
|---|
| 569 |
the beginning and end of the text matched by that construct. Then, |
|---|
| 570 |
later on in the regular expression, you can use `\' followed by the |
|---|
| 571 |
digit D to mean "match the same text matched the Dth time by the |
|---|
| 572 |
`\( ... \)' construct." |
|---|
| 573 |
|
|---|
| 574 |
The strings matching the first nine `\( ... \)' constructs |
|---|
| 575 |
appearing in a regular expression are assigned numbers 1 through 9 |
|---|
| 576 |
in the order that the open-parentheses appear in the regular |
|---|
| 577 |
expression. So you can use `\1' through `\9' to refer to the text |
|---|
| 578 |
matched by the corresponding `\( ... \)' constructs. |
|---|
| 579 |
|
|---|
| 580 |
For example, `\(.*\)\1' matches any newline-free string that is |
|---|
| 581 |
composed of two identical halves. The `\(.*\)' matches the first |
|---|
| 582 |
half, which may be anything, but the `\1' that follows must match |
|---|
| 583 |
the same exact text. |
|---|
| 584 |
|
|---|
| 585 |
If a particular `\( ... \)' construct matches more than once |
|---|
| 586 |
(which can easily happen if it is followed by `*'), only the last |
|---|
| 587 |
match is recorded. |
|---|
| 588 |
|
|---|
| 589 |
`\`' |
|---|
| 590 |
matches the empty string, but only at the beginning of the buffer |
|---|
| 591 |
or string being matched against. |
|---|
| 592 |
|
|---|
| 593 |
`\'' |
|---|
| 594 |
matches the empty string, but only at the end of the buffer or |
|---|
| 595 |
string being matched against. |
|---|
| 596 |
|
|---|
| 597 |
`\=' |
|---|
| 598 |
matches the empty string, but only at point. |
|---|
| 599 |
|
|---|
| 600 |
`\b' |
|---|
| 601 |
matches the empty string, but only at the beginning or end of a |
|---|
| 602 |
word. Thus, `\bfoo\b' matches any occurrence of `foo' as a |
|---|
| 603 |
separate word. `\bballs?\b' matches `ball' or `balls' as a |
|---|
| 604 |
separate word. |
|---|
| 605 |
|
|---|
| 606 |
`\b' matches at the beginning or end of the buffer regardless of |
|---|
| 607 |
what text appears next to it. |
|---|
| 608 |
|
|---|
| 609 |
`\B' |
|---|
| 610 |
matches the empty string, but _not_ at the beginning or end of a |
|---|
| 611 |
word. |
|---|
| 612 |
|
|---|
| 613 |
`\<' |
|---|
| 614 |
matches the empty string, but only at the beginning of a word. |
|---|
| 615 |
`\<' matches at the beginning of the buffer only if a |
|---|
| 616 |
word-constituent character follows. |
|---|
| 617 |
|
|---|
| 618 |
`\>' |
|---|
| 619 |
matches the empty string, but only at the end of a word. `\>' |
|---|
| 620 |
matches at the end of the buffer only if the contents end with a |
|---|
| 621 |
word-constituent character. |
|---|
| 622 |
|
|---|
| 623 |
`\w' |
|---|
| 624 |
matches any word-constituent character. The syntax table |
|---|
| 625 |
determines which characters these are. *Note Syntax::. |
|---|
| 626 |
|
|---|
| 627 |
`\W' |
|---|
| 628 |
matches any character that is not a word-constituent. |
|---|
| 629 |
|
|---|
| 630 |
`\sC' |
|---|
| 631 |
matches any character whose syntax is C. Here C is a character |
|---|
| 632 |
that designates a particular syntax class: thus, `w' for word |
|---|
| 633 |
constituent, `-' or ` ' for whitespace, `.' for ordinary |
|---|
| 634 |
punctuation, etc. *Note Syntax::. |
|---|
| 635 |
|
|---|
| 636 |
`\SC' |
|---|
| 637 |
matches any character whose syntax is not C. |
|---|
| 638 |
|
|---|
| 639 |
`\cC' |
|---|
| 640 |
matches any character that belongs to the category C. For |
|---|
| 641 |
example, `\cc' matches Chinese characters, `\cg' matches Greek |
|---|
| 642 |
characters, etc. For the description of the known categories, |
|---|
| 643 |
type `M-x describe-categories <RET>'. |
|---|
| 644 |
|
|---|
| 645 |
`\CC' |
|---|
| 646 |
matches any character that does _not_ belong to category C. |
|---|
| 647 |
|
|---|
| 648 |
The constructs that pertain to words and syntax are controlled by the |
|---|
| 649 |
setting of the syntax table (*note Syntax::). |
|---|
| 650 |
|
|---|
| 651 |
Here is a complicated regexp, stored in `sentence-end' and used by |
|---|
| 652 |
Emacs to recognize the end of a sentence together with any whitespace |
|---|
| 653 |
that follows. We show its Lisp syntax to distinguish the spaces from |
|---|
| 654 |
the tab characters. In Lisp syntax, the string constant begins and |
|---|
| 655 |
ends with a double-quote. `\"' stands for a double-quote as part of |
|---|
| 656 |
the regexp, `\\' for a backslash as part of the regexp, `\t' for a tab, |
|---|
| 657 |
and `\n' for a newline. |
|---|
| 658 |
|
|---|
| 659 |
"[.?!][]\"')]*\\($\\| $\\|\t\\| \\)[ \t\n]*" |
|---|
| 660 |
|
|---|
| 661 |
This contains four parts in succession: a character set matching |
|---|
| 662 |
period, `?', or `!'; a character set matching close-brackets, quotes, |
|---|
| 663 |
or parentheses, repeated zero or more times; a set of alternatives |
|---|
| 664 |
within backslash-parentheses that matches either end-of-line, a space |
|---|
| 665 |
at the end of a line, a tab, or two spaces; and a character set |
|---|
| 666 |
matching whitespace characters, repeated any number of times. |
|---|
| 667 |
|
|---|
| 668 |
To enter the same regexp interactively, you would type <TAB> to |
|---|
| 669 |
enter a tab, and `C-j' to enter a newline. (When typed interactively, |
|---|
| 670 |
`C-j' should be preceded by a `C-q', to prevent Emacs from running the |
|---|
| 671 |
command bound to a newline.) You would also type single backslashes as |
|---|
| 672 |
themselves, instead of doubling them for Lisp syntax. |
|---|
| 673 |
|
|---|
| 674 |
|
|---|
| 675 |
File: emacs, Node: Search Case, Next: Replace, Prev: Regexps, Up: Search |
|---|
| 676 |
|
|---|
| 677 |
Searching and Case |
|---|
| 678 |
================== |
|---|
| 679 |
|
|---|
| 680 |
Incremental searches in Emacs normally ignore the case of the text |
|---|
| 681 |
they are searching through, if you specify the text in lower case. |
|---|
| 682 |
Thus, if you specify searching for `foo', then `Foo' and `foo' are also |
|---|
| 683 |
considered a match. Regexps, and in particular character sets, are |
|---|
| 684 |
included: `[ab]' would match `a' or `A' or `b' or `B'. |
|---|
| 685 |
|
|---|
| 686 |
An upper-case letter anywhere in the incremental search string makes |
|---|
| 687 |
the search case-sensitive. Thus, searching for `Foo' does not find |
|---|
| 688 |
`foo' or `FOO'. This applies to regular expression search as well as |
|---|
| 689 |
to string search. The effect ceases if you delete the upper-case |
|---|
| 690 |
letter from the search string. |
|---|
| 691 |
|
|---|
| 692 |
Typing `M-c' within an incremental search toggles the case |
|---|
| 693 |
sensitivity of that search. The effect does not extend beyond the |
|---|
| 694 |
current incremental search to the next one, but it does override the |
|---|
| 695 |
effect of including an upper-case letter in the current search. |
|---|
| 696 |
|
|---|
| 697 |
If you set the variable `case-fold-search' to `nil', then all |
|---|
| 698 |
letters must match exactly, including case. This is a per-buffer |
|---|
| 699 |
variable; altering the variable affects only the current buffer, but |
|---|
| 700 |
there is a default value which you can change as well. *Note Locals::. |
|---|
| 701 |
This variable applies to nonincremental searches also, including those |
|---|
| 702 |
performed by the replace commands (*note Replace::) and the minibuffer |
|---|
| 703 |
history matching commands (*note Minibuffer History::). |
|---|
| 704 |
|
|---|
| 705 |
|
|---|
| 706 |
File: emacs, Node: Replace, Next: Other Repeating Search, Prev: Search Case, Up: Search |
|---|
| 707 |
|
|---|
| 708 |
Replacement Commands |
|---|
| 709 |
==================== |
|---|
| 710 |
|
|---|
| 711 |
Global search-and-replace operations are not needed often in Emacs, |
|---|
| 712 |
but they are available. In addition to the simple `M-x replace-string' |
|---|
| 713 |
command which is like that found in most editors, there is a `M-x |
|---|
| 714 |
query-replace' command which finds each occurrence of the pattern and |
|---|
| 715 |
asks you whether to replace it. |
|---|
| 716 |
|
|---|
| 717 |
The replace commands normally operate on the text from point to the |
|---|
| 718 |
end of the buffer; however, in Transient Mark mode (*note Transient |
|---|
| 719 |
Mark::), when the mark is active, they operate on the region. The |
|---|
| 720 |
replace commands all replace one string (or regexp) with one |
|---|
| 721 |
replacement string. It is possible to perform several replacements in |
|---|
| 722 |
parallel using the command `expand-region-abbrevs' (*note Expanding |
|---|
| 723 |
Abbrevs::). |
|---|
| 724 |
|
|---|
| 725 |
* Menu: |
|---|
| 726 |
|
|---|
| 727 |
* Unconditional Replace:: Replacing all matches for a string. |
|---|
| 728 |
* Regexp Replace:: Replacing all matches for a regexp. |
|---|
| 729 |
* Replacement and Case:: How replacements preserve case of letters. |
|---|
| 730 |
* Query Replace:: How to use querying. |
|---|
| 731 |
|
|---|
| 732 |
|
|---|
| 733 |
File: emacs, Node: Unconditional Replace, Next: Regexp Replace, Prev: Replace, Up: Replace |
|---|
| 734 |
|
|---|
| 735 |
Unconditional Replacement |
|---|
| 736 |
------------------------- |
|---|
| 737 |
|
|---|
| 738 |
`M-x replace-string <RET> STRING <RET> NEWSTRING <RET>' |
|---|
| 739 |
Replace every occurrence of STRING with NEWSTRING. |
|---|
| 740 |
|
|---|
| 741 |
`M-x replace-regexp <RET> REGEXP <RET> NEWSTRING <RET>' |
|---|
| 742 |
Replace every match for REGEXP with NEWSTRING. |
|---|
| 743 |
|
|---|
| 744 |
To replace every instance of `foo' after point with `bar', use the |
|---|
| 745 |
command `M-x replace-string' with the two arguments `foo' and `bar'. |
|---|
| 746 |
Replacement happens only in the text after point, so if you want to |
|---|
| 747 |
cover the whole buffer you must go to the beginning first. All |
|---|
| 748 |
occurrences up to the end of the buffer are replaced; to limit |
|---|
| 749 |
replacement to part of the buffer, narrow to that part of the buffer |
|---|
| 750 |
before doing the replacement (*note Narrowing::). In Transient Mark |
|---|
| 751 |
mode, when the region is active, replacement is limited to the region |
|---|
| 752 |
(*note Transient Mark::). |
|---|
| 753 |
|
|---|
| 754 |
When `replace-string' exits, it leaves point at the last occurrence |
|---|
| 755 |
replaced. It sets the mark to the prior position of point (where the |
|---|
| 756 |
`replace-string' command was issued); use `C-u C-<SPC>' to move back |
|---|
| 757 |
there. |
|---|
| 758 |
|
|---|
| 759 |
A numeric argument restricts replacement to matches that are |
|---|
| 760 |
surrounded by word boundaries. The argument's value doesn't matter. |
|---|
| 761 |
|
|---|
| 762 |
|
|---|
| 763 |
File: emacs, Node: Regexp Replace, Next: Replacement and Case, Prev: Unconditional Replace, Up: Replace |
|---|
| 764 |
|
|---|
| 765 |
Regexp Replacement |
|---|
| 766 |
------------------ |
|---|
| 767 |
|
|---|
| 768 |
The `M-x replace-string' command replaces exact matches for a single |
|---|
| 769 |
string. The similar command `M-x replace-regexp' replaces any match |
|---|
| 770 |
for a specified pattern. |
|---|
| 771 |
|
|---|
| 772 |
In `replace-regexp', the NEWSTRING need not be constant: it can |
|---|
| 773 |
refer to all or part of what is matched by the REGEXP. `\&' in |
|---|
| 774 |
NEWSTRING stands for the entire match being replaced. `\D' in |
|---|
| 775 |
NEWSTRING, where D is a digit, stands for whatever matched the Dth |
|---|
| 776 |
parenthesized grouping in REGEXP. To include a `\' in the text to |
|---|
| 777 |
replace with, you must enter `\\'. For example, |
|---|
| 778 |
|
|---|
| 779 |
M-x replace-regexp <RET> c[ad]+r <RET> \&-safe <RET> |
|---|
| 780 |
|
|---|
| 781 |
replaces (for example) `cadr' with `cadr-safe' and `cddr' with |
|---|
| 782 |
`cddr-safe'. |
|---|
| 783 |
|
|---|
| 784 |
M-x replace-regexp <RET> \(c[ad]+r\)-safe <RET> \1 <RET> |
|---|
| 785 |
|
|---|
| 786 |
performs the inverse transformation. |
|---|
| 787 |
|
|---|
| 788 |
|
|---|
| 789 |
File: emacs, Node: Replacement and Case, Next: Query Replace, Prev: Regexp Replace, Up: Replace |
|---|
| 790 |
|
|---|
| 791 |
Replace Commands and Case |
|---|
| 792 |
------------------------- |
|---|
| 793 |
|
|---|
| 794 |
If the first argument of a replace command is all lower case, the |
|---|
| 795 |
command ignores case while searching for occurrences to |
|---|
| 796 |
replace--provided `case-fold-search' is non-`nil'. If |
|---|
| 797 |
`case-fold-search' is set to `nil', case is always significant in all |
|---|
| 798 |
searches. |
|---|
| 799 |
|
|---|
| 800 |
In addition, when the NEWSTRING argument is all or partly lower |
|---|
| 801 |
case, replacement commands try to preserve the case pattern of each |
|---|
| 802 |
occurrence. Thus, the command |
|---|
| 803 |
|
|---|
| 804 |
M-x replace-string <RET> foo <RET> bar <RET> |
|---|
| 805 |
|
|---|
| 806 |
replaces a lower case `foo' with a lower case `bar', an all-caps `FOO' |
|---|
| 807 |
with `BAR', and a capitalized `Foo' with `Bar'. (These three |
|---|
| 808 |
alternatives--lower case, all caps, and capitalized, are the only ones |
|---|
| 809 |
that `replace-string' can distinguish.) |
|---|
| 810 |
|
|---|
| 811 |
If upper-case letters are used in the replacement string, they remain |
|---|
| 812 |
upper case every time that text is inserted. If upper-case letters are |
|---|
| 813 |
used in the first argument, the second argument is always substituted |
|---|
| 814 |
exactly as given, with no case conversion. Likewise, if either |
|---|
| 815 |
`case-replace' or `case-fold-search' is set to `nil', replacement is |
|---|
| 816 |
done without case conversion. |
|---|
| 817 |
|
|---|
| 818 |
|
|---|
| 819 |
File: emacs, Node: Query Replace, Prev: Replacement and Case, Up: Replace |
|---|
| 820 |
|
|---|
| 821 |
Query Replace |
|---|
| 822 |
------------- |
|---|
| 823 |
|
|---|
| 824 |
`M-% STRING <RET> NEWSTRING <RET>' |
|---|
| 825 |
`M-x query-replace <RET> STRING <RET> NEWSTRING <RET>' |
|---|
| 826 |
Replace some occurrences of STRING with NEWSTRING. |
|---|
| 827 |
|
|---|
| 828 |
`C-M-% REGEXP <RET> NEWSTRING <RET>' |
|---|
| 829 |
`M-x query-replace-regexp <RET> REGEXP <RET> NEWSTRING <RET>' |
|---|
| 830 |
Replace some matches for REGEXP with NEWSTRING. |
|---|
| 831 |
|
|---|
| 832 |
If you want to change only some of the occurrences of `foo' to |
|---|
| 833 |
`bar', not all of them, then you cannot use an ordinary |
|---|
| 834 |
`replace-string'. Instead, use `M-%' (`query-replace'). This command |
|---|
| 835 |
finds occurrences of `foo' one by one, displays each occurrence and |
|---|
| 836 |
asks you whether to replace it. Aside from querying, `query-replace' |
|---|
| 837 |
works just like `replace-string'. It preserves case, like |
|---|
| 838 |
`replace-string', provided `case-replace' is non-`nil', as it normally |
|---|
| 839 |
is. A numeric argument means consider only occurrences that are |
|---|
| 840 |
bounded by word-delimiter characters. |
|---|
| 841 |
|
|---|
| 842 |
`C-M-%' performs regexp search and replace (`query-replace-regexp'). |
|---|
| 843 |
|
|---|
| 844 |
The characters you can type when you are shown a match for the string |
|---|
| 845 |
or regexp are: |
|---|
| 846 |
|
|---|
| 847 |
`<SPC>' |
|---|
| 848 |
to replace the occurrence with NEWSTRING. |
|---|
| 849 |
|
|---|
| 850 |
`<DEL>' |
|---|
| 851 |
to skip to the next occurrence without replacing this one. |
|---|
| 852 |
|
|---|
| 853 |
`, (Comma)' |
|---|
| 854 |
to replace this occurrence and display the result. You are then |
|---|
| 855 |
asked for another input character to say what to do next. Since |
|---|
| 856 |
the replacement has already been made, <DEL> and <SPC> are |
|---|
| 857 |
equivalent in this situation; both move to the next occurrence. |
|---|
| 858 |
|
|---|
| 859 |
You can type `C-r' at this point (see below) to alter the replaced |
|---|
| 860 |
text. You can also type `C-x u' to undo the replacement; this |
|---|
| 861 |
exits the `query-replace', so if you want to do further |
|---|
| 862 |
replacement you must use `C-x <ESC> <ESC> <RET>' to restart (*note |
|---|
| 863 |
Repetition::). |
|---|
| 864 |
|
|---|
| 865 |
`<RET>' |
|---|
| 866 |
to exit without doing any more replacements. |
|---|
| 867 |
|
|---|
| 868 |
`. (Period)' |
|---|
| 869 |
to replace this occurrence and then exit without searching for more |
|---|
| 870 |
occurrences. |
|---|
| 871 |
|
|---|
| 872 |
`!' |
|---|
| 873 |
to replace all remaining occurrences without asking again. |
|---|
| 874 |
|
|---|
| 875 |
`^' |
|---|
| 876 |
to go back to the position of the previous occurrence (or what |
|---|
| 877 |
used to be an occurrence), in case you changed it by mistake. |
|---|
| 878 |
This works by popping the mark ring. Only one `^' in a row is |
|---|
| 879 |
meaningful, because only one previous replacement position is kept |
|---|
| 880 |
during `query-replace'. |
|---|
| 881 |
|
|---|
| 882 |
`C-r' |
|---|
| 883 |
to enter a recursive editing level, in case the occurrence needs |
|---|
| 884 |
to be edited rather than just replaced with NEWSTRING. When you |
|---|
| 885 |
are done, exit the recursive editing level with `C-M-c' to proceed |
|---|
| 886 |
to the next occurrence. *Note Recursive Edit::. |
|---|
| 887 |
|
|---|
| 888 |
`C-w' |
|---|
| 889 |
to delete the occurrence, and then enter a recursive editing level |
|---|
| 890 |
as in `C-r'. Use the recursive edit to insert text to replace the |
|---|
| 891 |
deleted occurrence of STRING. When done, exit the recursive |
|---|
| 892 |
editing level with `C-M-c' to proceed to the next occurrence. |
|---|
| 893 |
|
|---|
| 894 |
`e' |
|---|
| 895 |
to edit the replacement string in the minibuffer. When you exit |
|---|
| 896 |
the minibuffer by typing <RET>, the minibuffer contents replace the |
|---|
| 897 |
current occurrence of the pattern. They also become the new |
|---|
| 898 |
replacement string for any further occurrences. |
|---|
| 899 |
|
|---|
| 900 |
`C-l' |
|---|
| 901 |
to redisplay the screen. Then you must type another character to |
|---|
| 902 |
specify what to do with this occurrence. |
|---|
| 903 |
|
|---|
| 904 |
`C-h' |
|---|
| 905 |
to display a message summarizing these options. Then you must type |
|---|
| 906 |
another character to specify what to do with this occurrence. |
|---|
| 907 |
|
|---|
| 908 |
Some other characters are aliases for the ones listed above: `y', |
|---|
| 909 |
`n' and `q' are equivalent to <SPC>, <DEL> and <RET>. |
|---|
| 910 |
|
|---|
| 911 |
Aside from this, any other character exits the `query-replace', and |
|---|
| 912 |
is then reread as part of a key sequence. Thus, if you type `C-k', it |
|---|
| 913 |
exits the `query-replace' and then kills to end of line. |
|---|
| 914 |
|
|---|
| 915 |
To restart a `query-replace' once it is exited, use `C-x <ESC> |
|---|
| 916 |
<ESC>', which repeats the `query-replace' because it used the |
|---|
| 917 |
minibuffer to read its arguments. *Note C-x ESC ESC: Repetition. |
|---|
| 918 |
|
|---|
| 919 |
See also *Note Transforming File Names::, for Dired commands to |
|---|
| 920 |
rename, copy, or link files by replacing regexp matches in file names. |
|---|
| 921 |
|
|---|
| 922 |
|
|---|
| 923 |
File: emacs, Node: Other Repeating Search, Prev: Replace, Up: Search |
|---|
| 924 |
|
|---|
| 925 |
Other Search-and-Loop Commands |
|---|
| 926 |
============================== |
|---|
| 927 |
|
|---|
| 928 |
Here are some other commands that find matches for a regular |
|---|
| 929 |
expression. They all ignore case in matching, if the pattern contains |
|---|
| 930 |
no upper-case letters and `case-fold-search' is non-`nil'. Aside from |
|---|
| 931 |
`occur', all operate on the text from point to the end of the buffer, |
|---|
| 932 |
or on the active region in Transient Mark mode. |
|---|
| 933 |
|
|---|
| 934 |
`M-x occur <RET> REGEXP <RET>' |
|---|
| 935 |
Display a list showing each line in the buffer that contains a |
|---|
| 936 |
match for REGEXP. To limit the search to part of the buffer, |
|---|
| 937 |
narrow to that part (*note Narrowing::). A numeric argument N |
|---|
| 938 |
specifies that N lines of context are to be displayed before and |
|---|
| 939 |
after each matching line. |
|---|
| 940 |
|
|---|
| 941 |
The buffer `*Occur*' containing the output serves as a menu for |
|---|
| 942 |
finding the occurrences in their original context. Click `Mouse-2' |
|---|
| 943 |
on an occurrence listed in `*Occur*', or position point there and |
|---|
| 944 |
type <RET>; this switches to the buffer that was searched and |
|---|
| 945 |
moves point to the original of the chosen occurrence. |
|---|
| 946 |
|
|---|
| 947 |
`M-x list-matching-lines' |
|---|
| 948 |
Synonym for `M-x occur'. |
|---|
| 949 |
|
|---|
| 950 |
`M-x how-many <RET> REGEXP <RET>' |
|---|
| 951 |
Print the number of matches for REGEXP that exist in the buffer |
|---|
| 952 |
after point. In Transient Mark mode, if the region is active, the |
|---|
| 953 |
command operates on the region instead. |
|---|
| 954 |
|
|---|
| 955 |
`M-x flush-lines <RET> REGEXP <RET>' |
|---|
| 956 |
Delete each line that contains a match for REGEXP, operating on |
|---|
| 957 |
the text after point. In Transient Mark mode, if the region is |
|---|
| 958 |
active, the command operates on the region instead. |
|---|
| 959 |
|
|---|
| 960 |
`M-x keep-lines <RET> REGEXP <RET>' |
|---|
| 961 |
Delete each line that _does not_ contain a match for REGEXP, |
|---|
| 962 |
operating on the text after point. In Transient Mark mode, if the |
|---|
| 963 |
region is active, the command operates on the region instead. |
|---|
| 964 |
|
|---|
| 965 |
You can also search multiple files under control of a tags table |
|---|
| 966 |
(*note Tags Search::) or through Dired `A' command (*note Operating on |
|---|
| 967 |
Files::), or ask the `grep' program to do it (*note Grep Searching::). |
|---|
| 968 |
|
|---|
| 969 |
|
|---|
| 970 |
File: emacs, Node: Fixit, Next: Files, Prev: Search, Up: Top |
|---|
| 971 |
|
|---|
| 972 |
Commands for Fixing Typos |
|---|
| 973 |
************************* |
|---|
| 974 |
|
|---|
| 975 |
In this chapter we describe the commands that are especially useful |
|---|
| 976 |
for the times when you catch a mistake in your text just after you have |
|---|
| 977 |
made it, or change your mind while composing text on the fly. |
|---|
| 978 |
|
|---|
| 979 |
The most fundamental command for correcting erroneous editing is the |
|---|
| 980 |
undo command, `C-x u' or `C-_'. This command undoes a single command |
|---|
| 981 |
(usually), a part of a command (in the case of `query-replace'), or |
|---|
| 982 |
several consecutive self-inserting characters. Consecutive repetitions |
|---|
| 983 |
of `C-_' or `C-x u' undo earlier and earlier changes, back to the limit |
|---|
| 984 |
of the undo information available. *Note Undo::, for more information. |
|---|
| 985 |
|
|---|
| 986 |
* Menu: |
|---|
| 987 |
|
|---|
| 988 |
* Kill Errors:: Commands to kill a batch of recently entered text. |
|---|
| 989 |
* Transpose:: Exchanging two characters, words, lines, lists... |
|---|
| 990 |
* Fixing Case:: Correcting case of last word entered. |
|---|
| 991 |
* Spelling:: Apply spelling checker to a word, or a whole file. |
|---|
| 992 |
|
|---|
| 993 |
|
|---|
| 994 |
File: emacs, Node: Kill Errors, Next: Transpose, Up: Fixit |
|---|
| 995 |
|
|---|
| 996 |
Killing Your Mistakes |
|---|
| 997 |
===================== |
|---|
| 998 |
|
|---|
| 999 |
`<DEL>' |
|---|
| 1000 |
Delete last character (`delete-backward-char'). |
|---|
| 1001 |
|
|---|
| 1002 |
`M-<DEL>' |
|---|
| 1003 |
Kill last word (`backward-kill-word'). |
|---|
| 1004 |
|
|---|
| 1005 |
`C-x <DEL>' |
|---|
| 1006 |
Kill to beginning of sentence (`backward-kill-sentence'). |
|---|
| 1007 |
|
|---|
| 1008 |
The <DEL> character (`delete-backward-char') is the most important |
|---|
| 1009 |
correction command. It deletes the character before point. When <DEL> |
|---|
| 1010 |
follows a self-inserting character command, you can think of it as |
|---|
| 1011 |
canceling that command. However, avoid the mistake of thinking of |
|---|
| 1012 |
<DEL> as a general way to cancel a command! |
|---|
| 1013 |
|
|---|
| 1014 |
When your mistake is longer than a couple of characters, it might be |
|---|
| 1015 |
more convenient to use `M-<DEL>' or `C-x <DEL>'. `M-<DEL>' kills back |
|---|
| 1016 |
to the start of the last word, and `C-x <DEL>' kills back to the start |
|---|
| 1017 |
of the last sentence. `C-x <DEL>' is particularly useful when you |
|---|
| 1018 |
change your mind about the phrasing of the text you are writing. |
|---|
| 1019 |
`M-<DEL>' and `C-x <DEL>' save the killed text for `C-y' and `M-y' to |
|---|
| 1020 |
retrieve. *Note Yanking::. |
|---|
| 1021 |
|
|---|
| 1022 |
`M-<DEL>' is often useful even when you have typed only a few |
|---|
| 1023 |
characters wrong, if you know you are confused in your typing and aren't |
|---|
| 1024 |
sure exactly what you typed. At such a time, you cannot correct with |
|---|
| 1025 |
<DEL> except by looking at the screen to see what you did. Often it |
|---|
| 1026 |
requires less thought to kill the whole word and start again. |
|---|
| 1027 |
|
|---|
| 1028 |
|
|---|
| 1029 |
File: emacs, Node: Transpose, Next: Fixing Case, Prev: Kill Errors, Up: Fixit |
|---|
| 1030 |
|
|---|
| 1031 |
Transposing Text |
|---|
| 1032 |
================ |
|---|
| 1033 |
|
|---|
| 1034 |
`C-t' |
|---|
| 1035 |
Transpose two characters (`transpose-chars'). |
|---|
| 1036 |
|
|---|
| 1037 |
`M-t' |
|---|
| 1038 |
Transpose two words (`transpose-words'). |
|---|
| 1039 |
|
|---|
| 1040 |
`C-M-t' |
|---|
| 1041 |
Transpose two balanced expressions (`transpose-sexps'). |
|---|
| 1042 |
|
|---|
| 1043 |
`C-x C-t' |
|---|
| 1044 |
Transpose two lines (`transpose-lines'). |
|---|
| 1045 |
|
|---|
| 1046 |
The common error of transposing two characters can be fixed, when |
|---|
| 1047 |
they are adjacent, with the `C-t' command (`transpose-chars'). |
|---|
| 1048 |
Normally, `C-t' transposes the two characters on either side of point. |
|---|
| 1049 |
When given at the end of a line, rather than transposing the last |
|---|
| 1050 |
character of the line with the newline, which would be useless, `C-t' |
|---|
| 1051 |
transposes the last two characters on the line. So, if you catch your |
|---|
| 1052 |
transposition error right away, you can fix it with just a `C-t'. If |
|---|
| 1053 |
you don't catch it so fast, you must move the cursor back between the |
|---|
| 1054 |
two transposed characters before you type `C-t'. If you transposed a |
|---|
| 1055 |
space with the last character of the word before it, the word motion |
|---|
| 1056 |
commands are a good way of getting there. Otherwise, a reverse search |
|---|
| 1057 |
(`C-r') is often the best way. *Note Search::. |
|---|
| 1058 |
|
|---|
| 1059 |
`M-t' transposes the word before point with the word after point |
|---|
| 1060 |
(`transpose-words'). It moves point forward over a word, dragging the |
|---|
| 1061 |
word preceding or containing point forward as well. The punctuation |
|---|
| 1062 |
characters between the words do not move. For example, `FOO, BAR' |
|---|
| 1063 |
transposes into `BAR, FOO' rather than `BAR FOO,'. |
|---|
| 1064 |
|
|---|
| 1065 |
`C-M-t' (`transpose-sexps') is a similar command for transposing two |
|---|
| 1066 |
expressions (*note Expressions::), and `C-x C-t' (`transpose-lines') |
|---|
| 1067 |
exchanges lines. They work like `M-t' except as regards what units of |
|---|
| 1068 |
text they transpose. |
|---|
| 1069 |
|
|---|
| 1070 |
A numeric argument to a transpose command serves as a repeat count: |
|---|
| 1071 |
it tells the transpose command to move the character (word, expression, |
|---|
| 1072 |
line) before or containing point across several other characters (words, |
|---|
| 1073 |
expressions, lines). For example, `C-u 3 C-t' moves the character |
|---|
| 1074 |
before point forward across three other characters. It would change |
|---|
| 1075 |
`f-!-oobar' into `oobf-!-ar'. This is equivalent to repeating `C-t' |
|---|
| 1076 |
three times. `C-u - 4 M-t' moves the word before point backward across |
|---|
| 1077 |
four words. `C-u - C-M-t' would cancel the effect of plain `C-M-t'. |
|---|
| 1078 |
|
|---|
| 1079 |
A numeric argument of zero is assigned a special meaning (because |
|---|
| 1080 |
otherwise a command with a repeat count of zero would do nothing): to |
|---|
| 1081 |
transpose the character (word, expression, line) ending after point |
|---|
| 1082 |
with the one ending after the mark. |
|---|
| 1083 |
|
|---|
| 1084 |
|
|---|
| 1085 |
File: emacs, Node: Fixing Case, Next: Spelling, Prev: Transpose, Up: Fixit |
|---|
| 1086 |
|
|---|
| 1087 |
Case Conversion |
|---|
| 1088 |
=============== |
|---|
| 1089 |
|
|---|
| 1090 |
`M-- M-l' |
|---|
| 1091 |
Convert last word to lower case. Note `Meta--' is Meta-minus. |
|---|
| 1092 |
|
|---|
| 1093 |
`M-- M-u' |
|---|
| 1094 |
Convert last word to all upper case. |
|---|
| 1095 |
|
|---|
| 1096 |
`M-- M-c' |
|---|
| 1097 |
Convert last word to lower case with capital initial. |
|---|
| 1098 |
|
|---|
| 1099 |
A very common error is to type words in the wrong case. Because of |
|---|
| 1100 |
this, the word case-conversion commands `M-l', `M-u' and `M-c' have a |
|---|
| 1101 |
special feature when used with a negative argument: they do not move the |
|---|
| 1102 |
cursor. As soon as you see you have mistyped the last word, you can |
|---|
| 1103 |
simply case-convert it and go on typing. *Note Case::. |
|---|
| 1104 |
|
|---|