Replacement and Case Replace
Replace some occurrences of string with newstring.
|Replace some matches for regexp with newstring.|
If you want to change only some of the occurrences of `
bar', not all of them, then you cannot use an ordinary
replace-string. Instead, use M-% (
This command finds occurrences of `
foo' one by one, displays each
occurrence and asks you whether to replace it. A numeric argument to
query-replace tells it to consider only occurrences that are
bounded by word-delimiter characters. This preserves case, just like
case-replace is non-
as it normally is.
Aside from querying,
query-replace works just like
query-replace-regexp works just like
replace-regexp. This command is run by C-M-%.
The things you can type when you are shown an occurrence of string or a match for regexp are:
to replace the occurrence with newstring.
to skip to the next occurrence without replacing this one.
to replace this occurrence and display the result. You are then asked
for another input character to say what to do next. Since the
replacement has already been made,
You can type C-r at this point (see below) to alter the replaced
text. You can also type C-x u to undo the replacement; this exits
to exit without doing any more replacements.
to replace this occurrence and then exit without searching for more occurrences.
to replace all remaining occurrences without asking again.
to go back to the position of the previous occurrence (or what used to
be an occurrence), in case you changed it by mistake. This works by
popping the mark ring. Only one ^ in a row is meaningful, because
only one previous replacement position is kept during
to enter a recursive editing level, in case the occurrence needs to be edited rather than just replaced with newstring. When you are done, exit the recursive editing level with C-M-c to proceed to the next occurrence. See Recursive Edit.
to delete the occurrence, and then enter a recursive editing level as in C-r. Use the recursive edit to insert text to replace the deleted occurrence of string. When done, exit the recursive editing level with C-M-c to proceed to the next occurrence.
to redisplay the screen. Then you must type another character to specify what to do with this occurrence.
to display a message summarizing these options. Then you must type another character to specify what to do with this occurrence.
Some other characters are aliases for the ones listed above: y,
n and q are equivalent to
Aside from this, any other character exits the
and is then reread as part of a key sequence. Thus, if you type
C-k, it exits the
query-replace and then kills to end of
To restart a
query-replace once it is exited, use C-x
ESC, which repeats the
query-replace because it
used the minibuffer to read its arguments. See C-x ESC ESC.
See also Transforming File Names, for Dired commands to rename, copy, or link files by replacing regexp matches in file names.Replacement and Case Replace