All Manuals > LispWorks Editor User Guide > 3 Command Reference

NextPrevUpTopContentsIndex

3.23 Searching and replacing

This section is divided into three parts. The first two provide details of commands used for searching. These commands are, on the whole, non-modifying and non-destructive, and merely search for strings and patterns. The third part provides details of commands used for replacing a string or pattern.

3.23.1 Searching

Most of the search commands perform straightforward searches, but there are two useful commands (Incremental Search and Reverse Incremental Search) which perform incremental searches. This means that the search is started as soon as the first character is typed.

Incremental Search

Editor Command

Arguments: string
Key sequence: Ctrl+S string

Searches forward, starting from the current point, for the search string that is input, beginning the search as soon as each character is typed in. When a match is found for the search string, the current point is moved to the end of the matched string. If the search string is not found between the current point and the end of the buffer, an error is signalled.

The search result is highlighted. You can change the style of the highlighting in the LispWorks IDE by Preferences... > Environment > Styles > Colors and Attributes > Search Match .

Incremental Search records the starting location (see Locations).

With a prefix argument p the matches are displayed at a fixed line position, p lines below the top of the window. Otherwise, the position of the matched string within the window is influenced by the editor variable incremental-search-minimum-visible-lines.

For example, to display successive definitions one line from the top of the text view of the Editor window, enter:

Ctrl+U 2 Ctrl+S ( d e f Ctrl+S Ctrl+S

All incremental searches can be controlled by entering one of the following key sequences at any time during the search.

Ctrl+S

If the search string is empty, repeats the last incremental search, otherwise repeats a forward search for the current search string.

If the search string cannot be found, starts the search from the beginning of the buffer (wrap-around search).

Ctrl+R

Changes to a backward (reverse) search.

Delete

Cancels the last character typed.

Ctrl+Q

Quotes the next character typed.

Ctrl+W

Adds the next word under the cursor to the search string.

Meta+Ctrl+Y

Adds the next form under the cursor to the search string.

Ctrl+Y

Adds the remainder of the line under the cursor to the search string.

Meta+Y

Adds the current kill string to the search string.

Ctrl+C

Add the editor window's selected text to the search string.

Esc

If the search string is empty, invokes a non-incremental search, otherwise exits the search, leaving the current point at the last find.

Ctrl+G

Aborts the search, returning the current point to its original location.

If the search string cannot be found, cancels the last character typed (equivalent to Delete).

Return

Exits the search, leaving the current point at the last find.

incremental-search-minimum-visible-lines

Editor Variable

Default value: 3

Determines the minimum of visible lines between an incremental search match and the closest window border (top or bottom). If the point is closer to the border than the value, the point is scrolled to the center of the window.

Lines are counted from the start of the match, and the line where the match starts is included in the count.

Note that this has no effect when doing "fixed position" search (with numeric prefix, for example Ctrl+U digit Ctrl+S, or if the window is too short.

Setting the value to 0 makes incremental searching behave as in LispWorks 6.0 and earlier versions, that is the match can be shown on the top or bottom line currently displayed in the window.

Reverse Incremental Search

Editor Command

Arguments: string
Key sequence: Ctrl+R string

Searches backward, starting from the current point, for the search string that is input, beginning the search as soon as each character is provided. When a match is found for the search string, the current point is moved to the start of the matched string. If the search string is not found between the current point and the beginning of the buffer, an error is signalled.

You can use a fixed line position for the matches and/or modify the style used to display them, as described for Incremental Search.

With a prefix argument p the matches are displayed at a fixed line position, p lines below the top of the window. Otherwise, the position of the matched string within the window is influenced by the editor variable incremental-search-minimum-visible-lines.

The search can be controlled by entering one of the following key sequences at any time during the search.

Ctrl+R

If the search string is empty, repeats the last incremental search, otherwise repeats a backward search for the current search string.

If the search string cannot be found, starts the search from the end of the buffer (wrap-around search).

Ctrl+S

Changes to a forward search.

Delete

Cancels the last character typed.

Esc

If the search string is empty, invokes a non-incremental search, otherwise exits the search, leaving the current point at the last find.

Ctrl+G

Aborts the search, returning the current point to its original location.

If the search string cannot be found, cancels the last character typed (equivalent to Delete).

Ctrl+Q

Quotes the next character typed.

Forward Search

Editor Command

Arguments: string
Key sequence: Ctrl+S Esc string

editor:forward-search-command p &optional string the-point

The default for the-point is the current point.

Searches forwards from the-point for string. When a match is found, the-point is moved to the end of the matched string. In contrast with Incremental Search, the search string must be terminated with a carriage return before any searching is done. If an empty string is provided, the last search is repeated.

Records the starting location (see Locations).

Backward Search

Editor Command

Arguments: string
Key sequence: None

editor:reverse-search-command p &optional string the-point

The default for the-point is the current point.

Searches backwards from the-point for string. When a match is found, the-point is moved to the start of the matched string. In contrast with Reverse Incremental Search, the search string must be terminated with a carriage return before any searching is done. If an empty string is provided, the last search is repeated.

Records the starting location (see Locations).

Reverse Search is a synonym for Backward Search.

List Matching Lines

Editor Command

Arguments: string
Key sequence: None

editor:list-matching-lines-command p &optional string

Lists all lines after the current point that contain string, in a Matches window.

A prefix argument causes the appropriate number of lines before and after each matching line to be listed also.

Delete Matching Lines

Editor Command

Arguments: string
Key sequence: None

editor:delete-matching-lines-command p &optional string

Deletes all lines after the current point that match string.

Delete Non-Matching Lines

Editor Command

Arguments: string
Key sequence: None

editor:delete-non-matching-lines-command p &optional string

Deletes all lines after the current point that do not match string.

Search All Buffers

Editor Command

Arguments: string
Key sequence: None

Searches all the buffers for string. If only one buffer contains string, it becomes the current buffer, with the cursor positioned at the start of the string. If more than one buffer contains the string, a popup window displays a list of those buffers. A buffer may then be selected from this list.

Buffers Search

Editor Command

Arguments: search-string
Key sequence: None

The command Buffers Search searches all opened buffers for search-string, displaying the first match.

Use the key sequence Meta+, to find subsequent occurrences of search-string.

Search Buffers

Editor Command

Arguments: search-string
Key sequence: None

The command Search Buffers searches all opened buffers using the Search Files tool.

It prompts for a string and then activates the Search Files tool in the Opened Buffers mode. See the LispWorks IDE User Guide for a description of the Search Files tool.

Directory Search

Editor Command

Arguments: directory string
Key sequence: None

Searches source files in directory for string. The current working directory is offered as a default for directory .

By default only files with suffix .lisp, .lsp, .c, .cpp or .h are searched. A non-nil prefix argument causes all files to be searched, except for those ending with one of the strings in the list system:*ignorable-file-suffices*.

Directory Search displays the first match. Use the key sequence Meta+, to find subsequent occurrences of the search string.

Search Files

Editor Command

Arguments: search-string directory
Key sequence: Ctrl+X * search-string directory

Searches for a string in a directory using a Search Files tool.

The command prompts for search-string and directory and then raises a Search Files tool. The configuration of the Search Files tool controls which files in the directory are searched. If the search string is not empty, it starts searching automatically, unless a prefix argument is given.

See the LispWorks IDE User Guide for a description of the Search Files tool.

Search Files Matching Patterns

Editor Command

Arguments: search-string directory patterns
Key sequence: Ctrl+X & search-string directory patterns

Searches for a string in files under a directory with names matching given patterns, using a Search Files tool.

The command prompts for search-string, directory and patterns, and raises a Search Files tool in Roots and Patterns mode. If the search string is not empty, it starts searching automatically, unless a prefix argument is given.

patterns should be a comma-separated set of filename patterns delimited by braces. A pattern where the last component does not contain * is assumed to be a directory onto which the Search Files tool adds its own filename pattern. patterns defaults to {*.lisp,*.lsp,*.c,*.h}.

See the LispWorks IDE User Guide for a description of the Search Files tool.

System Search

Editor Command

Arguments: system string
Key sequence: None

Searches the files of system for string.

Matches are shown in editor buffers consecutively. Use the key sequence Meta+, to find subsequent definitions of the search string.

Search System

Editor Command

Arguments: search-string system
Key sequence: None

Prompts for search-string and system and then raises a Search Files tool in System Search mode, which displays the search results and allows you to visit the files.

See the LispWorks IDE User Guide for a description of the Search Files tool.

default-search-kind

Editor Variable

Default value: :string-insensitive

Defines the default method of searching. By default, all searching (including regexp searching, and replacing commands) ignores case. If you want searching to be case-sensitive, the value of this variable should be set to :string-sensitive using Set Variable.

It is also possible to search a set of files programmatically using the search-files function:

editor:search-files

Function

editor:search-files &key string files generator => nil

search-files searches all the files in a list for a given string.

string is a string to search for (prompted if not given).

files is a list of pathnames of files to search, and generator is a function to generate the files if none are supplied.

If a match is found the file is displayed in a buffer with the cursor on the occurrence. Meta+-, makes the search continue until the next occurrence.

search-files returns nil.

For example:

(editor:search-files 
 :files '(".login" ".cshrc") 
 :string "alias")

3.23.2 Regular expression syntax

A regular expression ( regexp ) allows the specification of the search string to include wild characters, repeated characters, ranges of characters, and alternatives. Strings which follow a specific pattern can be located, which makes regular expression searches very powerful.

The regular expression syntax used is similar to that of Emacs. In addition to ordinary characters, a regular expression can contain the following special characters to produce the search pattern:

.

Matches any single character except a newline. For example, c.r matches any three character string starting with c and ending with r.

*

Matches the previous regexp any number of times (including 0 times). For example, ca*r matches strings beginning with c and ending with r, with any number of a's in-between.

An empty regexp followed by * matches an empty part of the input. By extension, ^* will match exactly what ^ matches.

+

Matches the previous regexp any number of times, but at least once. For example, ca+r matches strings beginning with c and ending with r, with at least one a in-between. An empty regexp followed by + matches an empty part of the input.

?

Matches the previous regexp either 0 or 1 times. For example, ca?r matches either the string cr or car, and nothing else. An empty regexp followed by ? matches an empty part of the input.

^

Matches the next regexp as long as it is at the beginning of a line. For example, ^foo matches the string foo as long as it is at the beginning of a line.

$

Matches the previous regexp as long as it is at the end of a line. For example, foo$ matches the string foo as long as it is at the end of a line.

[ ]

Contains a character set to be used for matching, where the other special characters mentioned do not apply. The empty string is automatically part of the character set. For example, [a.b] matches either a or . or b or the empty string. The regexp c[ad]*r matches strings beginning with c and ending with r, with any number of a's and d's in-between.

The characters - and ^ have special meanings inside character sets. - defines a range and ^ defines a complement character set. For example, [a-d] matches any character in the range a to d inclusive. [^ab] matches any character except a or b.

\

Quotes the special characters. For example, \* matches the character * (that is, * has lost its special meaning).

\|

Specifies an alternative. For example, ab\|cd matches either ab or cd.

\(, \)

Provides a grouping construct. For example, ab\(cd\|ef\) matches either abcd or abef.

Regexp Forward Search

Regexp Reverse Search

Editor Commands

Arguments: string
Key sequence: None

Performs a forward or backward search for string using regular expressions. The search pattern must be terminated with a carriage return before any searching is done. If an empty string is provided, the last regexp search is repeated.

See also: editor:regular-expression-search

ISearch Forward Regexp

Editor Command

Arguments: string
Key sequence: Meta+Ctrl+S string

The command ISearch Forward Regexp performs incremental search forwards, using regular expression matching.

ISearch Backward Regexp

Editor Command

Arguments: string
Key sequence: Meta+Ctrl+R string

The command ISearch Backward Regexp performs incremental search backwards, using regular expression matching.

Count Occurrences

Editor Command

Arguments: None
Default binding: None

editor:count-occurrences-command p &optional regexp

Counts the number of regular expression matches for the string regexp between the current point and the end of the buffer.

Count Matches is a synonym for Count Occurrences.

3.23.3 Replacement

Replace String

Editor Command

Arguments: target replacement
Key sequence: None

editor:replace-string-command p &optional target replacement

Replaces all occurrences of target string by replacement string, starting from the current point.

Whenever replacement is substituted for target, case may be preserved, depending on the value of the editor variable case-replace.

Query Replace

Editor Command

Arguments: target replacement
Key sequence: Meta+Shift+% target replacement

editor:query-replace-command p &optional target replacement

Replaces occurrences of target string by replacement string, starting from the current point, but only after querying the user. Each time target is found, an action must be indicated from the keyboard.

Whenever replacement is substituted for target, case may be preserved, depending on the value of the editor variable case-replace.

The following key sequences are used to control Query Replace:

Space or y

Replace target by replacement and move to the next occurrence of target .

Delete

Skip target without replacing it and move to the next occurrence of target.

.

Replace target by replacement and then exit.

!

Replace all subsequent occurrences of target by replacement without prompting.

Ctrl+R

Enter recursive edit. This allows the current occurrence of target to be edited. When this editing is completed, Exit Recursive Edit should be invoked. The next instance of target is then found.

Esc

Quit from Query Replace with no further replacements.

Directory Query Replace

Editor Command

Arguments: directory target replacement
Key sequence: None

Replaces occurrences of target string by replacement string for each source file in directory , but only after querying the user.

The current working directory is offered as a default for directory .

By default only files with suffix .lisp, .lsp, .c, .cpp or .h are searched. A non-nil prefix argument causes all files to be searched, except for those ending with one of the strings in the list system:*ignorable-file-suffices*.

Each time target is found, an action must be indicated from the keyboard. For details of possible actions see Query Replace.

System Query Replace

Editor Command

Arguments: system target replacement
Key sequence: None

Replaces occurrences of target string by replacement string, for each file in system , but only after querying the user. Each time target is found, an action must be indicated from the keyboard. For details of possible actions see Query Replace.

Buffers Query Replace

Editor Command

Arguments: target replacement
Key sequence: None

The command Buffers Query Replace does a query replace operation on all opened buffers. See Query Replace for details of the operation.

case-replace

Editor Variable

Default value: t

If the value of this variable is t, Replace String and Query Replace try to preserve case when doing replacements. If its value is nil, the case of the replacement string is as defined by the user.

Replace Regexp

Query Replace Regexp

Editor Commands

Arguments: target replacement
Key sequence: None

editor:replace-regexp-command p &optional target replacement editor:query-replace-regexp-command p &optional target replacement

Replaces matches of target regular expression by replacement string, starting from the current point.

See Regular expression syntax for a description of regular expressions.

Replace Regexp replaces all matches.

Query Replace Regexp asks the user whether to replace each match in turn. Each time target is matched, an action must be indicated from the keyboard.

See Regular expression syntax for a description of regular expressions, and Query Replace for the keyboard gestures available.

In replacement, the Backslash character \ has special meaning. It causes the pair of characters of the Backslash and the following character char to be replaced for each match before using replacement. The character char following the Backslash must be one of:

&

Use the string that matched the whole pattern.

#

Use a string that is the decimal representation of the number of matches that have already been replaced in the current operation (first one will use 0).

\

Use the single character string "\\".

A non-zero digit

Use the string that matched the corresponding \( and \) pair in the pattern, starting from 1. The pairs are counted by the order of appearance of the \( in the pattern, so nested pairs have larger numbers than their enclosing pairs.

When replacement contains a \ character, after each match the system replaces all the pairs of \char by the appropriate string, and uses the result as the replacement string for this match. For example, you can change dates in the form dd/mm/yyyy to the form yyyy-mm-dd by using

target

\([0-9][0-9]\)/\([0-9][0-9]\)/\([0-9][0-9][0-9][0-9]\)

replacement

\3-\2-\1

This replaces, for example, 12/03/1979 by 1979-03-12.

Compatibility note: the special meaning of the Backslash character \ was introduced in LispWorks 7.0.


LispWorks Editor User Guide (Unix version) - 9 Dec 2014

NextPrevUpTopContentsIndex