ARCHIVED: In Emacs, how can I search for and replace text?

This content has been archived, and is no longer maintained by Indiana University. Information here may no longer be accurate, and links may no longer be available or reliable.

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 How keystrokes are denoted in Emacs

Unconditional search and replace

To perform an unconditional search and replace, press M-x. Then enter:

  replace-string RET your_search_string RET your_replace_string

Replace 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 options:

Spacebar
Replace text and find the next occurrence
Del
Leave text as is and find the next occurrence
. (period)
Replace text, then stop looking for occurrences
! (exclamation point)
Replace all occurrences without asking
^ (caret)
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 Get help for Linux or Unix at IU.

This is document abdp in the Knowledge Base.
Last modified on 2018-01-18 08:52:48.