Previous: Replacement and Lax Matches, Up: Replace
If you want to change only some of the occurrences of ‘foo’ to
‘bar’, not all of them, use M-% (query-replace
).
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 replace-string
(see Unconditional Replace). In particular, it preserves case
provided 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 (query-replace-regexp
).
It works like replace-regexp
except that it queries
like query-replace
.
You can reuse earlier replacements with these commands. When
query-replace
or 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
desired replacement. If the value of this variable is nil
,
replacements are not added to the command history, and cannot be
reused.
These commands highlight the current match using the face
query-replace
. You can disable this highlight by setting the
variable query-replace-highlight
to nil
. They highlight
other matches using lazy-highlight
just like incremental search
(see Incremental Search); this can be disabled by setting
query-replace-lazy-highlight
to 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-show-replacement
variable.
The variable query-replace-skip-read-only
, if set
non-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:
You can type C-r at this point (see below) to alter the replaced
text. You can also undo the replacement with the undo
command
(e.g., type C-x u; see Undo); this exits the
query-replace
, so if you want to do further replacement you
must use C-x <ESC> <ESC> <RET> to restart
(see Repetition).
Aside from this, any other character exits the query-replace
,
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 query-replace
.
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> <ESC>.
The option search-invisible
determines how query-replace
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.