This topic describes the search and replace commands Search( ) and Replace( ) which correspond to the functions as clicking Search then Search or clicking Search then Replace.

Searching and Search Options

The text to search for is specified with a "search string". Usually the search string consists of the exact characters you want to locate.

Search("today")
Search for the next occurrence of "today".

Like any string argument, the search string must be enclosed in delimiters that are not part of the search string. If the search is successful, the "edit position" is placed at the first character of the matched text, e.g. at the "t" of "today". If not, the command gives the error message "CANNOT FIND "string" - search errors can also be suppressed.

Sometimes it is preferable to have the edit position placed past the matched text, e.g. immediately after the "y" of "today". For example, this is convenient when a following block operation needs to include the matched text. This is easily done with the "ADVANCE" option.

Search("today",ADVANCE)
Search for the next occurrence of "today"; if found, place the edit position past the matched text.

You can directly search for the 'n'th occurrence with the command form Search("string", COUNT, n). For example:

Search("today",COUNT,7)
Search for the 7th occurrence of "today".

All searches are normally forwards, toward the end of the file. However, you can also search backwards toward the beginning of the file by using the command option "REVERSE".

Search("house",REVERSE)
Search backward for the nearest occurrence of "house".

When searching, vEdit normally equates upper and lower case letters. However, when needed, the command option "CASE" can be used to distinguish between upper and lower case letters.

Search("house",CASE)
Make a distinction between upper and lower case letters. This search with not match "House".

Sometimes you want to search for a distinct "word" that is separated from other characters with spaces or other separators. For example, you might want to search for the word "and", but not match "sand", "Anderson" or other words that contain "and". For this use the "WORD" option.

Search("and",WORD)
Restrict the search to distinct words. This search will not match "band".

The search string can include any desired pattern matching codes, just like [SEARCH]. For example: (The "|" is the keyboard character above "\".)

Search("|D|D")
Search for two consecutive digits using pattern matching.

Regular Expressions

Regular expressions can also be used when the "REGEXP" option is specified.

Search("[A-Z][a-z]*",REGEXP)
Search for a capitalized word using Regular expressions.
The configuration parameters {CONFIG, Search, Default case sensitive option} and {CONFIG, Search, Default search mode} have no effect on the Search( ) and Replace( ) commands.

[SEARCH] and [SEARCH AGAIN]

The [SEARCH] function saves the current search string so that it can be reused by [SEARCH AGAIN]. You can do the same thing in Command Mode by using the command option "SET":

Search("house",SET)
Perform the search and save the search string as the default search string.

Following the example above, [SEARCH AGAIN] would search for the string "house". There is an equivalent to [Search AGAIN] in command mode:

Search( )
Search again for the next occurrence of the default search string.

One difference between a "search" and a "search again" is that the "search" begins at the current edit position, while the "search again" begins with the character following the current edit position. Otherwise, a "search again" would tend to match the same text over and over again.

Replacing

The Replace( ) command is used to search for text and replace it with a new text.

Replace("today","not today")
Replace the next occurrence of "today" with "not today".

All of the options of the Search( ) command apply.

Replace("today","not today",COUNT,4)
Replace the next four occurrence of "today" with "not today"
Replace("today","not today",REVERSE)
Search backwards for "today" and replace it with "not today"

A common use of Replace( ) is to replace all occurrences of a word (perhaps a misspelled one) with another word(s). You could use the command option "COUNT" with a large number, but it is preferable to use the command option "ALL".

Replace("today","not today",WORD+ALL)
Replace all occurrences of the word "today" with "not today".

Another use for Replace( ) is to delete all occurrences of some particular text. For example, the command to find and delete all occurrences of the word "junk" is:

Replace("junk","",WORD+ALL)
Search and delete all occurrences of "junk", i.e. "junk" is being replaced with nothing.

Unsuccessful Search and Replace

An unsuccessful search normally gives the error message CANNOT FIND "string". When the "ALL" option is used with the Search( ) and Replace( ) commands, the error is only given if no occurrences were found.

The error message can be suppressed with the "NOERR" and "ERRBREAK" options. The "NOERR" option must be used with care to avoid "infinite" loops inside command macros that use looping. Break out of an infinite loop by pressing Ctrl-C or Ctrl-Break.

When a search is unsuccessful, Config(SR_RES_POS, n) controls where the edit position will be placed.

0
The edit position is only restored if it is still in memory. No file buffering is performed.
1
The edit position is always restored.
2
The edit position is left at the end of the file. In case of a backwards, REVERSE, search, it is left at the beginning of the file.

When searching large files, it is not always desirable to restore the edit position following an unsuccessful search because it can take a significant amount of time. Using the option "NORESTORE" with Search( ) or Replace( ) overrides the setting of Config(SR_RES_POS, n) and does not restore the edit position; it is left at the end (beginning) of the file.

Search("house",NORESTORE)
If the search is unsuccessful, don't restore the edit position. This saves time when searching in very large files.

Other Resources