3.1.2 Simple Searches
Emacs also offers a simple, or nonincremental, search. To use a more straightforward search, type C-s Enter. Type the search string, press Enter, and Emacs begins the search. Simply press C-s again to repeat the search. To start a nonincremental search backwards through the file, press C-r Enter. Again, you type the search string and press Enter to begin the search.
The search icon on the toolbar (a magnifying glass over paper) and the Edit → Search → String Forward option run the same kind of a search. The prompt is slightly different. C-s Enter prompts you with Search:
in the minibuffer while the toolbar icon and the menu option prompt with Search for string:
. This is a minor difference; the searches are virtually identical otherwise.
Table 3-2 summarizes the simple search commands.
Table 3-2. Simple search commands
Keystrokes | Action |
---|---|
C-s Enter searchstring Enter Edit → Search → String Forward |
Start nonincremental search forward. |
C-s | Repeat search forward. |
C-r Enter searchstring Enter Edit → Search → String Backwards |
Start nonincremental search backward. |
C-r | Repeat search backward. |
3.1.3 Word Search
If you're searching for a phrase and you know it's in the file but you can't find it with incremental search, try word search. (You probably can't find your phrase with incremental search because the phrase has a line break in it.) Word search is a nonincremental search that ignores line breaks, spaces, and punctuation. It also requires that your search string match entire words in the file.
To do a word search, type C-s Enter C-w (for word-search-forward). The prompt Word search
appears in the minibuffer. (Don't be put off by the prompts that appear along the way: you'll see an I-search
prompt after typing C-s and a Search
prompt after pressing Enter. Ignore these.) Type the search string and press Enter. Emacs searches for the given string. To do a word search backwards, type C-r Enter C-w instead. For example, assume that you have the following text, with the cursor at the beginning:
He said, "All good elephants are wise, aren't they?"
She answered, "Some are smarter than others, but we
think this is socially conditioned."
The command C-s Enter C-w they she Enter positions the cursor after the word She. This command looks complicated, but it's really nothing more than a word search (C-s Enter C-w) for the word they, followed by the word she. It ignores the punctuation (?") and the newline between they and she.
Assume that you're looking for the word the. You don't want to bother with thence, there, theater, thesis, blithe, or any other word that happens to contain the letters the. In this situation, neither an incremental search nor a simple search is very useful—you need a word search. If you're writing a paper, word search is often exactly what you need. It is the only one of the three basic search commands that allows you to find what you want even if the phrase is split between two lines.
Now that you've seen the three most commonly used searches, you might want to experiment and see which you find most useful.
3.2 Search and Replace
Search and replace definitely go together, like coffee and cream. Let's say you're working on a new software application and at the last possible moment, the Marketing Department decides to change the product's name.
There's a press release for Whirligig, an email service that periodically reminds you to make healthy lifestyle changes like exercising, drinking water, and taking vitamins. The level of harassment or, as the marketing department says, encouragement, can be set by the user. Whirligig isn't really the most descriptive name, so at the last minute the Marketing Department changes it to HealthBug.
3.2.1 Simple Search and Replace Operations
Assume you're in the situation we just described. You want to replace every occurrence of one string with another. You know that Whirligig is never correct, and there is absolutely no ambiguity about how you want to replace it. When you want to replace every instance of a given string, you can use a simple command that tells Emacs to do just that. Type M-x replace-string Enter, then type the search string and press Enter. Now type the replacement string and press Enter again. Emacs replaces all occurrences in the file from the cursor position onward. If you want to search and replace throughout the file, press M-< to go to the beginning of the file before typing this command. Here's a quick example of using replace-string.
Initial state:
Whirligig appears four times, but the cursor is positioned after the first instance.
Now we'll do the replacement.
Type: M-x replace-string Enter Whirligig Enter HealthBug Enter
Emacs replaces all instances from the cursor position onward.
The replacement occurs only from the cursor position onward; Whirligig in the first sentence is still incorrect. We'll work with this example again in a moment.
3.2.2 Query-Replace
Few search and replace situations are as straightforward as those we've described. Often you're not sure that you want to replace every appearance of your search string: a global replacement can be reckless. If you want to decide whether to replace the string on a case-by-case basis, use a query-replace, which allows you to change a string conditionally throughout a file. After Emacs finds an occurrence of the search string, it asks whether it should replace it, and you respond accordingly.