Книга: Learning GNU Emacs, 3rd Edition

3.2.2 Query-Replace

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.

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-string operation; 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-3 lists the possible responses and their results.

Table 3-3. Responses during query-replace

Keystrokes Action
Space or y Replace searchstring with newstring and go to the next instance of the string.
Del or 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 Space or y to move on.)
! Replace all the rest and don't ask.
^ Back up to the previous instance.
Enter or 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 n to 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.

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 Space to 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.

Оглавление книги


Генерация: 0.082. Запросов К БД/Cache: 0 / 0
поделиться
Вверх Вниз