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.
The text to search for is specified with a "search string". Usually the search string consists of the exact characters you want to locate.
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.
You can directly search for the 'n'th occurrence with the command form Search("string", COUNT, n). For example:
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".
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.
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.
The search string can include any desired pattern matching codes, just like [SEARCH]. For example: (The "|" is the keyboard character above "\".)
Regular expressions can also be used when the "REGEXP" option is specified.
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":
Following the example above, [SEARCH AGAIN] would search for the string "house". There is an equivalent to [Search AGAIN] in command mode:
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.
The Replace( ) command is used to search for text and replace it with a new text.
All of the options of the Search( ) command apply.
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".
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:
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.
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.