In Emacs, how can I search for and replace text?
Emacs has two types of search and replace functions: unconditional and queried.
Note: If you aren't sure how to enter the following commands in Emacs, see In Emacs, how are keystrokes denoted?
Unconditional search and replace
To perform an unconditional search and replace, press
M-x . Then enter:
your_search_string with the existing text
string you want to find and remove, and replace
your_replace_string with the new text you want to add
in its place.
Queried search and replace
To do a queried search and replace (with a prompt for replacing each
occurrence of a text string), press
M-% . You will then
be prompted for your search and replace strings. Emacs will find
and display each occurrence of the search string and ask you for
further instructions. You can respond with any of the following
|Spacebar||Replace text and find the next occurrence|
||Leave text as is and find the next occurrence|
||Replace text, then stop looking for occurrences|
||Replace all occurrences without asking|
||Return the cursor to previously replaced text|
If you type any other character, Emacs will stop looking for occurrences and execute the character as a command. To restart the queried search and replace, type:C-x M- M-RET
At Indiana University, for personal or departmental Linux or Unix systems support, see At IU, how do I get support for Linux or Unix?
Last modified on September 16, 2010.