Simple Pattern Matching

When editing a file, you often want to be able to say "Find me an occurrence of this string" so you can work on it without knowing precisely where it is. This is especially true when working from a paper listing in which you know the text string you want to edit, but probably not its line number. In the editor you can find a string simply by enclosing it in slashes on the command line. The command /son/ will cause a search to be made for the string "son". If option dual is off (od-) then the matching will be case insensitive and /son/ will match "SON", "SoN", "sON" and so on. It will also match a line containing "personal" since it contains an instance of "son".

Searching begins at the character after your cursor, resulting in the editor finding the next occurrence of your pattern. If the editor searches down to the end of the buffer without finding your pattern and option wrap is on (w+) it will continue the search at the first line of the buffer and continue until it reaches and tries your current line from behind. If your pattern is not found, an error will be generated.

Assuming you match a line containing your pattern, then that line will become your current line and your cursor will either point to the first character of the string matched (a+) or the character after this string (a-). The default is option anchor enabled (a+); however, if you are adding commas to the end of words you may prefer to switch to a-.

Enclosing a pattern in question marks instead of slashes will cause a search to be made backwards through your buffer. Therefore ?son? will search for the first occurrence of the string "son" starting at the character before the cursor. If the editor searches backward to the beginning of the buffer without finding the pattern and option wrap is on (w+) it will continue searching backwards from the end of the buffer. Again if your pattern is not found an error will be generated.

The editor is careful always to remember the last pattern you specified. Typing // will cause a search for the last pattern specified. This can save typing when looking for multiple occurrences of a long pattern. An even faster method would be to use the F9 key to re-execute the last command.

Up until now we have used the words string and pattern interchangeably. Although the editor can search for simple strings of characters like /The quick brown fox/ they are a subset of a more powerful pattern matching facility. This facility is enabled by the option metacharacters (om+) when you define a pattern. When enabled the characters ., @, $, ^,*, /, ?, and [ have a special meaning. For instance, a dot (.) is a pattern which matches any character. The pattern /a.c/ would match an occurrence of a string containing an "a" followed by any character followed by a "c". The meanings of these characters are explained in detail in the reference manual chapter. Unless you intend to read this you should turn off option metacharacters (om-) or prefix all special characters with a backslash character in your pattern. When prefixed by a backslash the special characters mentioned above lose their special meaning. A /cat\./ will only match the string "cat." regardless of the state of option m. If you want to match a backslash you must type two of them. A /a\\b/ will match the string "a\b". For non-alphanumerics the rule is simply this. If a character is preceded by a \ then remove the backslash and take that character literally. This allows you to specify a slash in your pattern. A /total\/number/ will match the string "total/number". The slash is protected and not taken as the pattern delimiter.

For alphanumerics the rule is slightly more involved. If the two characters following the backslash are hexadecimal (0 to 9 or A to F) then the whole sequence is taken as one character which has the hexadecimal value indicated. A /\07/ is a pattern consisting of the single character whose hexadecimal value is 07. A /\z/ is just a "z" while a /\bc/ is the character whose hexadecimal value is 0xBC. If this seems complex or confusing you probably don't need this ability. Just remember to type two back slashes to get one and that you can match a slash (or a ? if you scan backwards) by prefixing it with a backslash.