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 ‘foo’ to
‘bar’, not all of them, use M-% (
This command finds occurrences of ‘foo’ one by one, displays each
occurrence and asks you whether to replace it. Aside from querying,
query-replace works just like
(see Unconditional Replace). In particular, it preserves case
case-replace is non-
nil, as it normally is
(see Replacement and Lax Matches). A numeric argument means to
consider only occurrences that are bounded by word-delimiter
characters. A negative prefix argument replaces backward.
C-M-% performs regexp search and replace (
It works like
replace-regexp except that it queries
You can reuse earlier replacements with these commands. When
query-replace-regexp prompts for the
search string, use M-p and M-n to show previous
replacements in the form ‘from -> to’, where
from is the search pattern, to is its replacement, and the
separator between them is determined by the value of the variable
query-replace-from-to-separator. Type RET to select the
These commands highlight the current match using the face
query-replace. You can disable this highlight by setting the
nil. They highlight
other matches using
lazy-highlight just like incremental search
(see Incremental Search); this can be disabled by setting
nil. By default,
query-replace-regexp will show the substituted replacement
string for the current match in the minibuffer. If you want to keep
special sequences ‘\&’ and ‘\n’ unexpanded, customize
query-replace-skip-read-only, if set
nil, will cause replacement commands to ignore matches in
read-only text. The default is not to ignore them.
The characters you can type when you are shown a match for the string or 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, DEL and SPC are equivalent in this situation; both move to the next occurrence.
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
query-replace, so if you want to do further replacement you
must use C-x ESC ESC RET to restart
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 or want to reexamine it.
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 edit the replacement string in the minibuffer. When you exit the minibuffer by typing RET, the minibuffer contents replace the current occurrence of the pattern. They also become the new replacement string for any further occurrences.
to redisplay the screen. Then you must type another character to specify what to do with this occurrence.
to replace all remaining occurrences in all remaining buffers in multi-buffer replacements (like the Dired Q command that performs query replace on selected files). It answers this question and all subsequent questions in the series with “yes”, without further user interaction.
to skip to the next buffer in multi-buffer replacements without replacing remaining occurrences in the current buffer. It answers this question “no”, gives up on the questions for the current buffer, and continues to the next buffer in the sequence.
to display a message summarizing these options. Then you must type another character to specify what to do with this occurrence.
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
line. In particular, C-g simply exits the
To restart a
query-replace once it is exited, use C-x
ESC ESC, which repeats the
query-replace because it
used the minibuffer to read its arguments. See C-x ESC
search-invisible determines how
treats invisible text. See Outline Search.
See Operating on Files, for the Dired Q command which performs query replace on selected files. See also Transforming File Names, for Dired commands to rename, copy, or link files by replacing regexp matches in file names.