Emacs provides several commands for performing search-and-replace
operations. In addition to the simple M-x replace-string
command, there is M-% (query-replace), which presents
each occurrence of the search pattern and asks you whether to replace
it.
The replace commands normally operate on the text from point to the
end of the buffer. When the region is active, they operate on it
instead (see The Mark and the Region). The basic replace commands replace one
search string (or regexp) with one replacement string. It
is possible to perform several replacements in parallel, using the
command expand-region-abbrevs (see Controlling Abbrev Expansion).
If you set query-replace-show-preview to a non-nil
value, the replace commands show a preview of the replacement while you
type it: the matches visible in the window are displayed as they would
look after the replacement. This tells you what back-references like
‘\1’ (see Regexp Replacement) expand to before you commit to the
replacement. However, replacements that use ‘\,’ or ‘\#’ are
not previewed.
The value can be replace-preview-replacement-only, to display
the replacement alone, replace-preview-both, to display the match
next to its replacement, separated by an arrow, or a function of your
own, written by copying either of those two. Such a function takes the
match and the replacement and returns the string to display in place of
the match. If the replacement is empty,
replace-preview-replacement-only would display nothing at all, so
it displays a thin bar to mark the place of the match.
The preview uses the faces query-replace-preview and
query-replace-preview-match.