4 The Editor

4.5 Searching and replacing text

The editor provides a wide range of operations for searching for and replacing text. The examples below introduce the basic principles of search and replace, and the remainder of this section describes some of the more advanced keyboard commands available when searching for or replacing text in a buffer.

To search for text in the current buffer, select Edit > Find. A prompt appears in the echo area, asking you to type a string to search for. As soon as you start typing, the search commences. This kind of search is known as an incremental search -- with every character you type, the search is further refined.

Suppose you want to search for the word "defmacro" in a file.

1. Select Edit > Find.

The following prompt appears in the echo area:

I-search:
2. Type the letter d

The editor's cursor moves to the first occurrence of d after its current position. The prompt in the echo area changes to:

I-search:d
3. Type the letter e

The editor's cursor moves to the first occurrence of de. The prompt in the echo area changes to:

I-search:de
4. Type the letter f

The cursor moves to the first occurrence of def. The prompt in the echo area changes to

I-search:def
This continues until you stop typing, or until the editor fails to find the string you have typed in the current buffer. If the latter condition ever occurs, the prompt in the echo area changes to reflect it. For instance, if your file contains the word "defun" but not the word "defmacro", the prompt changes to:

Failing I-search: defm
As soon as you type m.

You can search for text and replace it with some other text using Edit > Replace. In the echo area, you will be asked to supply the text to be found and the text to replace it. In addition, whenever that text is found, you will be asked whether or not to replace that occurrence of it.

For instance, assume you wanted to replace every occurrence of "equal" to "equalp".

1. Choose Edit > Replace.

In the echo area, you will see:

Replace String:
2. Type equal and press Return.

In the echo area you will see:

Replace "equal" With:
3. Type equalp and press Return.

At every occurrence of "equal" after the current cursor position, you will see:

Replace "equal" with "equalp"?
in the echo area.

Other options are also available. See the end of this section for more details.

Note: Both Find and Replace start searching from the current position in the buffer. When Find reaches the end of the buffer, it starts again at the beginning, but Replace stops. To replace all occurrences of a text string in the buffer, you must ensure you are at the top of the buffer when you begin.

Note: The rest of this section goes into greater detail about searching for and replacing text. You can skip it and go on to the next section now if you wish.

The editor also provides a range of keyboard commands which allow you to search for or replace text. Each command prompts you for a search string in exactly the same way as the menu commands.

DescriptionKey
Search the buffer for the string specified, starting from the current cursor position.C-s or Find
Search backward in the buffer for the string specified, starting from the current cursor position.C-r
End the search. The cursor remains at the current position.Esc
Abort the search. The cursor returns to the position it was in before you started the search.C-g
Replace every occurrence of string after the current cursor position with newstring, prompting you whenever string is found.Esc % or Replace

When replacing text, you are prompted for both the string to search for and the string with which to replace it. Press Return after specifying each string. The editor then searches through the buffer, beginning at the current cursor position, and prompts you whenever the search string is found. At each prompt, you can use any of the following keyboard commands:

yReplace the text prompted for and search for next occurrence
nDo not replace the text. Start search for the next occurrence.
Space Same as y, but case is preserved (e.g. if foo is to be replaced by bar, and Foo is found, it will be replaced by Bar)
. (dot / period / full stop) Replace this occurrence and then exit search
Esc Exit search without doing any more replacements.
! Replace all remaining occurrences without asking again.
? Display a list of all the above keyboard commands.

The searches listed above look forward (or backward) in the current buffer, beginning the search as soon as you enter the first character. While in search mode, pressing C-w adds the word after the cursor into the search.

To repeat the search in either direction, type C-s or C-r again, as appropriate.


FreeLisp User's Guide - 5 FEB 1996

Generated with Harlequin WebMaker