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-sand 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-winstead. 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 Enterpositions 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.
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 Enteragain. 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.
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.
To use query-replace, go to the beginning of the buffer using M-<and then type M-%. The prompt Query replace:
appears in the minibuffer. Type the search string and press Enter. Now this appears:
Query replace searchstring with:
Type the replacement string and press Enter. So far, this procedure is almost identical to a replace-stringoperation; only the prompts are different.
Emacs now searches for the first occurrence of the search string. When it finds one, a new prompt appears:
Query replacing searchstring with newstring
Before performing the replacement, Emacs waits for a response to tell it what to do. Table 3-3lists the possible responses and their results.
Table 3-3. Responses during query-replace
Keystrokes |
Action |
Spaceor y |
Replace searchstring with newstring and go to the next instance of the string. |
Delor n |
Don't replace; move to next instance. |
. |
Replace the current instance and quit. |
, |
Replace and let me see the result before moving on. (Press Spaceor yto move on.) |
! |
Replace all the rest and don't ask. |
^ |
Back up to the previous instance. |
Enteror q |
Exit query-replace. |
E |
Modify the replacement string. |
C-r |
Enter a recursive edit (discussed in detail later). |
C-w |
Delete this instance and enter a recursive edit (so you can make a custom replacement). |
C-M-c |
Exit recursive edit and resume query-replace. |
C-] |
Exit recursive edit and exit query-replace. |
This list seems like a lot of keystrokes to remember, but you can get away with knowing two or three. Most of the time you'll respond to the prompt by pressing Space, telling Emacs to perform the replacement and go on to the next instance, or nto skip this replacement and go on to the next instance. If you're not too sure what will happen, enter a comma ( ,); Emacs makes the replacement but doesn't go on until you press Space. After performing the first few replaces, you may realize that there's no need to inspect every change individually. Typing an exclamation mark ( !) tells Emacs to go ahead and finish the job without bothering you anymore. If you remember these keystrokes, you're all set.
Читать дальше