Replace

Contents  Previous  Next

 

Menu: Search > Replace

 

Default Shortcut Key: Ctrl+R

 

Macro function: Replace()

 

The Replace command can be used to search for a text string and replace it with another string.  Replacements can be made selectively or globally, within the current file or across all edited files.  Regular Expressions can be entered within the search string.

 

The controls and options in the Replace dialog box are described below:

 

SearchReplace

 

Find text

This is the edit box where the search string is entered.  When the Replace command is issued, the word beneath the text cursor is placed into the Find Text edit box, in case that word--or a word which is nearly the same--is to be the search string.  To recall a search string which was previously entered, use the drop-down list or press the up or down arrow keys to review the items in the history list.  Regular Expressions may be used within the search string.

 

bm1The Delete key can be used while the drop-down list is displayed to delete a selected entry from the history list.

 

bm1Special characters can be entered into the Find text edit box using the technique described in the Help topic Inserting Special Characters.

 

Replace with

This is the edit box where the replace string is entered.  To recall a replace string which was previously entered, use the drop-down list or press the up or down arrow keys to review the items in the history list.

 

bm1The Delete key can be used while the drop-down list is displayed to delete a selected entry from the history list.

 

bm2The Replace command is line-oriented.  It considers each line individually and does not look across line enders to match a search string which might span lines.  Consequently, it is not possible to create new line enders using the Replace command, nor to delete existing line enders.  For these types of operations, the Replace Line Enders command must be used.

 

Insert Tab

Use this button to insert a tab character into the Find Text or Replace with edit boxes.

 

Ordinarily, the Tab key is used to move from field to field within a dialog box.  If you would prefer that the Tab key insert a tab character in this dialog box, and in other Find/Replace related dialog boxes, check the relevant box on the Configure | Preferences | Tabs dialog page.

 

Scope

Selected text

This option can be used to restrict the search and replace operation to the extent of the selected text.

 

Cursor to bottom

This option causes the search and replace operation to be performed from the cursor onward, toward the end of file.  (There is no provision for making replacements in a backward direction.)

 

Wraparound

This option causes the search to be performed from the cursor onward, toward the end of file.  When the end of file is reached, the search resumes at the top and continues to the original cursor position.

 

Top to bottom

This option causes the search and replace operation to be performed from the top of file onward, toward the end of file.  (There is no provision for making replacements in a backward direction.)

 

All open files

This option causes the search and replace operation to be performed across all open files.

 

Active project

Use this option to limit the scope of the Replace operation to those files within the active project.

 

Search Options

Perl regular expressions

If this box is checked, wildcard characters within the search string will be interpreted according to the Perl-Compatible Regular Expression (PCRE) convention.  In part, this means that the asterisk (*) will cause a match of zero or more occurrences of the preceding character.  The period (.) will match any single character. For more information, see Regular Expressions.

 

Maximal matching

When using pattern matching characters, there can sometimes be more than one text string that matches the search string. This option can be used to request that the longest possible matching string be returned.

 

Match case

This option can be used to force the search string to be matched exactly. When unchecked, a case insensitive search is performed.

 

Match whole words

This option can be used to restrict matches to those strings which appear as a whole word.  The characters which serve to delimit words are user-configurable; see Configure | Preferences | Cursor Travel.

 

Match at start of line

This option can be used to force the search string to be matched only when a matching string appears at the start of a line.  This effect can also be achieved with a Regular Expression.

 

Match at end of line

This option can be used to force the search string to be matched only when a matching string appears at the end of a line.  This effect can also be achieved with a Regular Expression.

 

Replace Options

Process $1, $2, $3... substring directives in the replace string

When this option is checked, special directives in the replace string will be replaced at match-time with subpatterns from the search string. This is a very powerful feature, as the following examples will illustrate.

 

Example 1:

 

       Find text: (\w+),(\w+)

       Replace with: $2 $1

 

The search string will match a string of one or more word characters followed by a comma, followed by another string of one or more word characters.  For example: Smith,John.  The parentheses are used to define subpatterns.  The first open parenthesis indicates subpattern number 1, the next number 2, and so on.  In this way, the replace string can vary depending on what the search string matches.  If the string Smith,John is matched, then the replace string will be John Smith.  Running this search and replace operation on a data file would have the effect of inverting a list of Lastname,Firstname data to Firstname Lastname format.

 

Example 2:

 

       Find text: (Boxer|BOXER)

       Replace with: <b>$1</b>

 

This search string will match either Boxer or BOXER.  The replace string will be equal to whatever the string matched, surrounded by the HTML open-bold and close-bold sequences.  In this way, the target word can be replaced without regard to its case, while ensuring that no case conversion occurs due to the replacement.

 

bm2The entire matching string is designated as $0, even if subpatterns are not used.  Up to 100 subpatterns can be referenced, numbering from $0 to $99.

 

Example 3:

 

       Find text: "([^"]+),([^"]*)"

       Replace with: "$1$2"

 

This pair of search and replace strings can be used to remove commas from within the data fields of quote and comma-delimited data, without disturbing the commas that are used as field separators.  The search pattern matches an entire double-quoted data field, so long as a comma appears within the data with at least one character to its left.  The replace string references the data on either side of the comma as $1 and $2, resulting in a replace string that duplicates the string matched, except that the comma is excluded.  (If multiple commas appear within a single data field, you'll need to run the replace operation repeatedly until all occurrences have been replaced.)

 

bm2References to named subpatterns such as (?P=name) are also recognized in the replace string.  See the Regular Expressions topic for more information about named subpatterns.

 

Replace only the first occurrence on a line

When this option is checked, only the first matching instance on a line will be considered eligible for replacement.

 

Confirm during Replace All

When this option is selected the Replace All operation will prompt before making each replacement.  A dialog box will be presented so that each replacement can be confirmed.  From this confirmation dialog box it is possible to later opt for unconditional replacements, by selecting its All button.

 

Sort history lists

If this box is checked the search and replace history lists will be maintained in alphabetic order, rather than in the order the strings were entered.

 

bm2When switching to alphabetically sorted lists, the chronological ordering of the lists will be lost, and cannot be restored by unchecking the checkbox.

 

bm2No attempt is made to associate the history list entries with the time that they were added to the list.  If a sorted history list is used consistently, over time the list will come to hold an unrepresentative set of search phrases.  In the extreme case, after many Replace operations, a list could result that contained only phrases beginning with the letter 'A'.  This occurs because entries at the bottom of the list will be removed after the maximum size of the list is reached.