Next: , Previous: , Up: Basic Commands   [Contents][Index]


2.5 Searching and Replacing

Searching for some string known to appear in the text is a commonly used method of moving long distances in a file. Replacing occurrences of one pattern with another is a useful way to make many simple changes to text. Hemlock provides powerful commands for doing both of these operations.

Hemlock Variable: String Search Ignore Case (initial value t)

This variable determines the kind of search done by searching and replacing commands.

Incremental Search searches for an occurrence of a string after the current point. It is known as an incremental search because it reads key-events form the keyboard one at a time and immediately searches for the pattern of corresponding characters as you type. This is useful because it is possible to initially type in a very short pattern and then add more characters if it turns out that this pattern has too many spurious matches.

This command dispatches on the following key-events as sub-commands:

C-s

Search forward for an occurrence of the current pattern. This can be used repeatedly to skip from one occurrence of the pattern to the next, or it can be used to change the direction of the search if it is currently a reverse search. If C-s is typed when the search string is empty, then a search is done for the string that was used by the last searching command.

C-r

Similar to C-s, except that it searches backwards.

Delete, Backspace

Undoes the effect of the last key-event typed. If that key-event simply added to the search pattern, then this removes the character from the pattern, moving back to the last match found before entering the removed character. If the character was a C-s or C-r, then this moves back to the previous match and possibly reverses the search direction.

C-g

If the search is currently failing, meaning that there is no occurrence of the search pattern in the direction of search, then C-g deletes enough characters off the end of the pattern to make it successful. If the search is currently successful, then C-g causes the search to be aborted, leaving the point where it was when the search started. Aborting the search inhibits the saving of the current search pattern as the last search string.

Escape

Exit at the current position in the text, unless the search string is empty, in which case a non-incremental string search is entered.

C-q

Search for the character corresponding to the next key-event, rather than treating it as a command.

Any key-event not corresponding to a graphic character, except those just described, causes the search to exit. Hemlock then uses the key-event in it normal command interpretation.

For example, typing C-a will exit the search and go to the beginning of the current line. When either of these commands successfully exits, they push the starting position (before the search) on the mark stack. If the current region was active when the search started, this foregoes pushing a mark.

These commands do a normal dumb string search, prompting for the search string in a normal dumb fashion. One reason for using a non-incremental search is that it may be faster since it is possible to specify a long search string from the very start. Since Hemlock uses the Boyer–Moore search algorithm, the speed of the search increases with the size of the search string. When either of these commands successfully exits, they push the starting position (before the search) on the mark stack. This is inhibited when the current region is active.

Command: Query Replace (bound to M-%)

This command prompts in the echo area for a target string and a replacement string. It then searches for an occurrence of the target after the point. When it finds a match, it prompts for a key-event indicating what action to take. The following are valid responses:

Space, y

Replace this occurrence of the target with the replacement string, and search again.

Delete, Backspace, n

Do not replace this occurrence, but continue the search.

!

Replace this and all remaining occurrences without prompting again.

.

Replace this occurrence and exit.

C-r

Go into a recursive edit (see page recursive-edits) at the current location. The search will be continued from wherever the point is left when the recursive edit is exited. This is useful for handling more complicated cases where a simple replacement will not achieve the desired effect.

Escape

Exit without doing any replacement.

Home, C-_, ?, h

Print a list of all the options available.

Any other key-event causes the command to exit, returning the key-event to the input stream; thus, Hemlock will interpret it normally for a command binding.

When the current region is active, this command uses it instead of the region from point to the end of the buffer. This is especially useful when you expect to use the ! option.

If the replacement string is all lowercase, then a heuristic is used that attempts to make the case of the replacement the same as that of the particular occurrence of the target pattern. If "foo" is being replaced with "bar" then "Foo" is replaced with "Bar" and "FOO" with "BAR".

This command may be undone with Undo, but its undoing may not be undone. On a successful exit from this command, the starting position (before the search) is pushed on the mark stack.

Hemlock Variable: Case Replace (initial value t)

If this variable is true then the case preserving heuristic in Query Replace is enabled, otherwise all replacements are done with the replacement string exactly as specified.

Command: Replace String

This command is the same as Query Replace except it operates without ever querying the user before making replacements. After prompting for a target and replacement string, it replaces all occurrences of the target string following the point. If a prefix argument is specified, then only that many occurrences are replaced. When the current region is active, this command uses it instead of the region from point to the end of the buffer.

Command: List Matching Lines

This command prompts for a search string and displays in a pop-up window all the lines containing the string that are after the point. If a prefix argument is specified, then this displays that many lines before and after each matching line. When the current region is active, this command uses it instead of the region from point to the end of the buffer.

Command: Delete Matching Lines
Command: Delete Non-Matching Lines

Delete Matching Lines prompts for a search string and deletes all lines containing the string that are after the point. Similarly, Delete Non-Matching Lines deletes all lines following the point that do not contain the specified string. When the current region is active, these commands uses it instead of the region from point to the end of the buffer.


Next: Page Commands, Previous: Filtering, Up: Basic Commands   [Contents][Index]