Search and replace text
The commands for searching and replacing in Emacs work in all modes and are described in this section.
The basic and most popular way to search in Emacs is to use incremental search, so called because the search begins immediately, as soon as you type the first character. It searches in increments with every character you type, so if you search for the text lamb, for instance, as soon as you type the l, point moves to the next l in the buffer; and when you type the a, it moves to the first instance of la (which might or might not be the same place where the first l was found) and so on.
This is a very efficient way of searching text, because unlike applications with search-box facilities where you type a term and then press Enter (or -- worse -- you have to click the OK button), an incremental search begins the moment you type the first letter of your term. As soon as the right match is found, you can stop typing.
The most common incremental search is the
isearch-forward function, which is bound to
C-s. It searches forward from point in the buffer for
the text you give it. To repeat a search and move to the next match, type
When you reach the end of the buffer, Emacs rings the bell and prints a message
in the minibuffer that the end of the buffer has been reached. If you type
C-s again after that, the search wraps around to the
beginning of the buffer (and you're so notified in the minibuffer).
C-s. Notice how you're prompted to give a string to search for in the minibuffer.
land see how point is moved to the next l in the buffer. If you have a very recent version of Emacs, all the l characters in the buffer will also be highlighted.
eand see how point moves forward and highlights the next l and the e character right after it.
Type a space and see how point moves to the next line and highlights the le in that line.
C-sto repeat the search for le and see how point moves to the next line.
C-sto repeat the search for le and notice how Emacs tells you that the search has failed -- it's reached the end of the buffer without finding another match for the le you're searching for.
C-sto repeat the search for le anyway, wrapping around to the front of the buffer, and see how point moves to where it's found on the first line at the top.
There's also a way to search for text you see in the buffer:
C-s C-w puts the string from point forward to the end
of the word into the search buffer, and
everything from point to the end of the line into the search buffer.
Incremental searches are normally case-insensitive; however, if you specify anything but all lowercase letters in your search, then only the case you give is matched.
To search backward from point, use
C-r), searching in reverse through the
As with the forward incremental search, typing this command twice starts a
search for the text that was last searched. When you get to the top of the
C-r again wraps around to the bottom.
Try it: Type
C-r lit to search in reverse and then
C-r multiple times to wrap down to the bottom of
the buffer in reverse.
There's a way to do a non-incremental search, too. This is useful when, for instance, you want to search for a particular phrase or string that you see in the buffer, but instead of typing it, you'd rather paste it.
The non-incremental search works the same for both the forward and reverse
searches. To do it, press Enter after typing either
C-r for the search,
type the entire string to search for, and press Enter again.
C-sto start a forward search and press Enter to specify that it be done non-incrementally.
Type the word
littleand press Enter.
Sometimes you might want to search for words or phrases in a buffer and find them no matter how they happen to be spaced or formatted -- even if they're split between lines.
For instance, what if you want to match feed by in the buffer, where it's split by a newline?
Try it: Type
C-r and give feed by as the text
to search for.
Emacs quickly beeps and reports "Failing I-search" because there's a newline character between the words.
To match phrases regardless of the spacing between words, use the Emacs word
search. You can do this with either a forward or reverse incremental search --
press Enter, type
C-w, and then give the word
or phrase to search for.
Try the search again:
Move point to the top of the buffer.
C-sto start a forward search.
C-wto specify a word search.
feed byand press Enter.
The string is matched even across a newline, and point is moved to just after the By in the second line of the phrase.
You can also search for regular expressions in Emacs. To do this, run the
isearch-backward-regexp function. These functions are
respectively. Then, give a regular expression as an argument. These searches are
Try searching forward and notice how the matches change depending on how you build the regexp:
C-M-sto start a forward regexp search.
Give the regexp
l.*eand notice how the matches change for every character you type.
There are several ways to replace text in Emacs.
replace-string function prompts you for a string
to match and a string to replace it with, and it replaces all instances from
point to the end of the buffer.
Try replacing the word something with clothing throughout the buffer:
M-x replace-stringand press Enter.
somethingand press Enter.
clothingand press Enter.
After the command runs, it reports in the minibuffer how many occurrences it replaced -- in this case, two.
To delete a word or phrase throughout a buffer, run this command and replace it with nothing.
Another powerful function for replacing text is
replace-regexp, which takes a regular expression as a
string to search for and a string of text to replace it with.
Finally, you can run the
which queries for every instance of the replacement. It's bound to
M-%. Your choices for each match are described in
Table 4. Options for Emacs' query-replace function
|Space, y||Replace this match.|
|Del, n||Skip this match and move to the next.|
|Enter, q||Exit |
|.||Make this replacement and then exit
|,||Make this replacement, move point to it, and exit
|C-r||Specify a recursive edit.|
|C-w||Delete the match and recursive edit.|
|C-l||Redraw the screen with this line in the center.|
|!||Continue making all replacements without querying first.|
|E||Edit the replacement string.|
|^||Go back to the previous replacement.|
query-replace-regexp functions work similarly, but
they take a regular expression as the string to be replaced.
Table 5 shows a summary of the various Emacs search and replace commands you've just learned.
Table 5. Emacs search and replace commands
|Binding||Command or function||Description|
||Incrementally search forward through the buffer for string (default is the
last search string you gave, if any); |
||Incrementally search backward through the buffer for string
(default is the last search string you gave, if any);
||Search forward through the buffer for the given word or phrase, regardless of spacing.|
||Search backward through the buffer for the given word or phrase, regardless of spacing.|
||Incrementally search forward through the buffer for a given regular expression.|
||Incrementally search backward through the buffer for a given regular expression.|
||Search for a given string from point to the end of the buffer and replace it with a given string.|
||Search for a given regular expression from point to the end of the buffer and replace it with a given string.|
||Search for a given string from point to the end of the buffer and, in each instance, query (as described in Table 4) to replace it with a given string.|
||Search for a given regular expression from point to the end of the buffer and, in each instance, query (as described in Table 4) to replace it with a given string.|