|
Query Replace: Search For and Replace Character Strings
What Query Replace Is
The query replace function searches for all the occurrences of a certain string in a document, and asks if you want to replace each with a second string you have specified. You can also replace all the occurrences at once, or choose to replace some but not others. The function is available in EZ and in the Send Message window.
Using Query Replace
To begin, position the text caret where you want to start and choose Query Replace from the Search/Spell menu card, or type Esc-q. At the "Replace" prompt on the message line, type the string you wish to find and replace, and press Enter. At the "New string" prompt, type the string you want as the replacement, and press Enter.
It is important to remember that, like searches, query replaces start at the current text caret position. The function cannot find instances that occur above the starting position of the text caret. To improve your chances of finding and replacing all instances of the old string, you may wish to place the caret at the very beginning of the document before choosing Query Replace.
Do upper and lower case matter? Query replace does "case folding" in the old string (the one you want to replace), ignoring the distinction between upper and lower case -- only the letters themselves matter. Even if you type upper case letters in the old string, the query replace function will find all matching instances, including those with only lower case letters (and vice versa). It does pay attention to case in the replacement string, however, inserting it just as you typed it.
After you start the query replace, the function will find and select the first occurrence of the old string, if it exists. At this point you have several choices of what to type:
| <spacebar>
| Performs the replacement. The query replace function will then find the next instance of the old string automatically.
|
| n
| Skips this instance, and finds the next one automatically.
|
| !
| Replaces all the instances in one step (without asking you about each one) and then puts the caret back where it started.
|
| . (the period)
| Replaces this one instance, quits the query replace function, and puts the caret back where it started.
|
| q
| Quits without replacing the current instance, and puts the caret back where it started.
|
| r
| Enters recursive editing mode, which allows you to "leave" the query replace function temporarily and then return to it. This is useful if you notice another error while you are doing a query replace, and want to edit it right then. When you choose r, the word(s) selected by the routine are deselected, and you can edit any part of the document you choose. When you are finished editing, press ^C (Ctrl-c) to re-enter the query replace routine.
Warning: When you re-enter the routine, the search and replace will begin from the current position of the text caret. If during editing you have moved the caret past some unchanged instances of the old string, the routine will not find and replace them. Also, the instance that was selected when you chose r will not be replaced, so you will need to change it by hand, if you want it changed.
Note: You must have the ez.ExpertMode preference in your preferences file set to "on" to make recursive editing possible.
|
| - (the hyphen)
| Overcomes "case folding," thus maintaining the same case in the replacement word as in the old word. That is, if the old word is capitalized or all upper case, so will the replacement word be. Note that this only works predictably when replacing a single word with another single word.
With the other query replace options, the function will replace all instances of the old string, regardless of case, with the replacement string exactly as you typed it.
|
| , (the comma)
| Performs the replacement and quits the query-replace function, but leaves the caret at that position. This is usful if you have acidently replaced a string which you didn't mean to replace. You can use this, fix the mistake, and then invoke query-replace again from there.
|
| =
| Same as the comma (,) above - but does not perform the curent replacement before quitting the query-replace function.
|
You can choose any of these options at any time during the query replace. When the function quits, it reports to you how many instances of the old string it found, and how many it replaced. It also returns the text caret to where it was when you started the query replace, and scrolls the screen to make it visible (unless you use ',' or '=').
Repeated Query-Replace
The first time you use the query-replace function, you are prompted for the string to be replaced and then the string to replace it with. The next time you invoke query-replace in that application (in that session) - the first prompt will also contain a default choice - the string you used the previous time. If you want the default simply hit <return> at the prompt. i.e, If the prompt looks like this:
Replace [foo] :
and you want to replace the string "foo", simply hit <return>. Otherwise, type in the string you do want to replace as you normally would.
Regular Expressions in Query-Replace
It is possible to use "regular expressions" in the old string (it is nonsensical to use them in the replacement string). For that reason, if you want to replace any of the special regular expression characters "as themselves" in the old string, you need to precede them with a backslash. The special characters include
\ . * [ ]
See the searching help document to learn more about these special characters and how to use them in regular expressions.
Control Characters in Query-Replace
It is possible to find and replace control characters, such as the newline character ^J, by using a special procedure. At the first, "Replace" prompt, precede the control character(s) you want to replace with ^X^Q. This "comments" the character(s) so you can find and replace them. Nothing will appear on the message line as you type, but the query replace function will work as usual.
This is useful if you are converting a file to EZ from another editor that does not do autowrapping, because it allows you to replace all the extra new line characters (^J) with spaces or no character at all (which you get by hitting Return only at the "New string" prompt).
Related Topic
|