Emacs Advent Calendar 11: Replacing
Emacs has two modes for searching and replacement. The first one unconditionally replaces all occurences of a searched string, the second one interactively asks what to do for every match.
Unconditional replacement can be invoked with
M-x replace-string. It
will replace all occurences of the search string with the replacement
string after point. So to operate on the whole buffer, point must be
at the start. If the region is active, replacements will only be done
An interactive session can be started with
which is also bound to
M-%. It will loop through all matches,
asking what to do with each one.
|<RET>, C-g||exit without replacement|
|.||replace this match and exit|
|!||replace all remaining matches|
|^||go back to previous match|
|C-r||enter recursive edit|
|e||edit replacement string|
|C-h, ?||display help|
Recursive editing means that the replacing is suspended and you are
free to edit the buffer with the usual commands. To resume replacing,
Some in-between note on Emacs terminology:
<DEL> is an Emacs
synonym for the backspace key. (By the way, did you notice that it is
located directly above
<RET> on qwerty keyboards? Useful for
quickly deciding on yes/no.)
To restart a canceled replace, press
C-x ESC ESC (which, in
fact, just repeats the latest command that used the Minibuffer).
For more information see sections 15.10 (Replacement Commands).Tags: emacs
This text by Ludger Sandig is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.