How does this work in practice? Let's revisit our previous example, assuming that we want to change Whirligig to HealthBug throughout (and that we didn't save the changes we made with replace-string).
Type: M-< M-% Whirligig Enter HealthBug Enter

You're ready to replace the first occurrence; press Spaceto go on.
Press: Space

When you press Space, Emacs replaces the first word; the query-replace operation then moves to the second word.
This procedure continues until you reach the end of the file. As we've said, typing !fixes the rest of the file.
In Table 3-3, you might have noticed that several keys, such as Space, have specialized meanings while the replacement is in progress. In practice, using these keys for a different function is not confusing, though it might sound bad on paper. You might want to try a query-replace on a practice file to get the hang of using the different responses. If you are easily amused, you might enjoy opening the Emacs FAQ, saving it as another file, then replacing Emacs throughout.
3.2.3 Repeating Query-Replaces (and Other Complex Commands)
Now that you've learned the basics of query-replace, let's talk about a shortcut that applies not only in query-replace but anywhere in Emacs: repeating complex commands, with slight modifications. We often exit a query-replace by mistake or decide that the replacement we really wanted was just slightly different. Do we have to type it all again? No. Simply go the beginning of the file and press C-x Esc Esc. The last complex command you typed appears. If it's not the one you want, type M-pto see the previous command (do this as many times as necessary; M-ngoes to the next command). For example, let's go to the beginning of the file and repeat the query-replace we just carried out.
Type: M-<followed by C-x Esc Esc

Emacs puts the last complex command in the minibuffer; in fact it looks more complex than we remember it.
When we press M-<, we move to the beginning of the file; when we press C-x Esc Esc, the last complex command is displayed. Emacs speaks to itself in dark words, but we can still see that this is the command that we want.
This is the right command, so we don't have to press M-pto see a previous command. If we wanted to, we could change the query-replace strings before pressing Enter. In this case, the Marketing Department has once again changed the product's name from HealthBug (since bug could be construed as pest) to HealthBot (neutral, but a bit less descriptive in our opinion). Our earlier query replace changed Whirligig to HealthBug. We need to modify this command so it replaces Bug with Bot .
In the minibuffer, change Whirligig to Bug and HealthBug to Bot and press Enter.

Pressing Enterexecutes the command again with the modified search and replacement strings.
As we mentioned, C-x Esc Escworks for any command involving input in the minibuffer, not just query-replace. But we use this feature most frequently in query-replace. It is also good for repeating keyboard macros (see Chapter 6).
When you do a query-replace, you inevitably see something else you want to change in the file. Try it a few times—you'll see what we mean! We typically try to remember the problem until we're done, then get frustrated when we forget exactly what and where the problem was.
Fortunately, Emacs provides an easier way. It allows you to start a recursive edit while you're in the middle of a query-replace. By starting a recursive edit, you effectively put query-replace on hold while you make any other desired edits. When you exit the recursive edit, the query-replace resumes where you left off.
To start a recursive edit while in query-replace, press C-r. (Note that like many other key bindings, C-rhas a different meaning in query-replace than it does in standard Emacs.) When you start a recursive edit, square brackets ( [ ]
) appear on the mode line. Let's go back, one more time, to our public relations piece. You've used query-replace to find the first Bug to change to Bot, and you are about to press Spaceto fix it, when you remember that the lawyers said that the "64 ounces of water" statement was too specific and could be construed as giving medical advice. A quick recursive edit saves the day.
Type: C-r

Notice the square brackets around (Text Fill)
, indicating a recursive edit in progress.
Now do any editing you want to; you are in an editing mode just like standard Emacs. Move down to the third line and delete "64 ounces of." When you want to resume the query-replace, press C-M-c. This command tells Emacs to leave the recursive edit and reactivate the query-replace. Emacs moves back to the point where you were when you started the recursive edit. You can then continue making replacements just as if nothing had happened.
Delete "64 ounces of," then type C-M-c

Emacs goes back to query-replace and you press Spaceto fix the next Bug.
If you decide to exit the recursive edit and cancel the query-replace in one fell swoop, you can type C-](for abort-recursive-edit) or M-x top-level Enterrather than C-M-c.
In fact, you can start a recursive edit at any time, not just when you're in a query-replace. The command M-x recursive-edit Enterputs you into a recursive edit; C-M-ctakes you out of the recursive edit and brings you back to what you were doing before. You can even have recursive edits within recursive edits, although the possibility for confusion increases with each new level.
3.2.5 Are Emacs Searches Case-Sensitive?
By default, Emacs searches are not case-sensitive. Look at the Options menu and you'll see that the option Case-Insensitive Search is the only option that is checked by default.
What does this mean in practical terms? If you search for the word random , the search finds random , Random , and RANDOM , as well as oddities like RanDoM and rANdOM . When doing replacements, Emacs pays attention to the form of the word being replaced and replaces it with the same case. If you replaced random with tandem , Random would be replaced with Tandem , and RANDOM would be replaced with TANDEM . If you mix capitalization, the replacement string appears just as you type it. healthbug would be replaced with HealthBug if that was the case in the replacement string. In other words, the default search and replacement operations usually do what you want: they find a search string regardless of its case and adjust the replacement appropriately for its context. However, sometimes you need finer control.
Читать дальше