vi has a number of search command. You can search for individual charaters through to regular expressions.
The main two character based search commands are [ f ] and [ t ] .
fc Find the next character c. Moves RIGHT to the next. Fc Find the next character c. Moves LEFT to the preceding. tc Move RIGHT to character before the next c. Tc Move LEFT to the character following the preceding c. (Some clones this is the same as Fc) ; Repeats the last f,F,t,T command , Same as ; but reverses the direction to the orginal command.
If the character you were searching for was not found, vi will beep or give some other sort of signal.
vi allows you to search for a string in the edit buffer.
/str Searches Right and Down for the next occurance of str. ?str Searches Left and UP for the next occurance of str. n Repeat the last / or ? command N Repeats the last / or ? in the Reverse direction.
When using the [ / ] or [ ? ] commands a line will be cleared along the bottom of the screen. You enter the search string followed by [ RETURN ] .
The string in the command [ / ] or [ ? ] can be a regular expression. A regular expression is a description of a set of characters. The description is build using text intermixed with special characters. The special characters in regular expressions are . * [] ^$.
. Matches any single character except newline. \ Escapes any special characters. * Matches 0 or More occurances of the preceding character. [] Matches exactly one of the enclosed characters. ^ Match of the next character must be at the begining of the line. $ Matches characters preceding at the end of the line. [^] Matches anything not enclosed after the not character. [-] Matches a range of characters.
The only way to get use to the regular expression is to use them. Following is a series of examples.
c.pe Matches cope, cape, caper etc c\.pe Matches c.pe, c.per etc sto*p Matches stp, stop, stoop etc car.*n Matches carton, cartoon, carmen etc xyz.* Matches xyz to the end of the line. ^The Matches any line starting with The. atime$ Matches any line ending with atime. ^Only$ Matches any line with Only as the only word in the line. b[aou]rn Matches barn, born, burn. Ver[D-F] Matches VerD, VerE, VerF. Ver[^1-9] Matches Ver followed by any non digit. the[ir][re] Matches their,therr, there, theie. [A-Za-z][A-Za-z]* Matches any word.
vi uses ex command mode to perform search and replace operations. All commands which start with a colon are requests in ex mode.
The search and replace command allows regular expression to be used over a range of lines and replace the matching string. The user can ask for confirmation before the substitution is performed. It may be well worth a review of line number representation in the ed tutorial.
:<start>,<finish>s/<find>/<replace>/g General command :1,$s/the/The/g Search the entire file and replace the with The. :%s/the/The/g % means the complete file. (Same as above). :.,5s/^.*//g Delete the contents from the current to 5th line. :%s/the/The/gc Replace the with The but ask before substituting. :%s/^....//g Delete the first four characters on each line.
The search command is very powerfull when combined with the regular expression search strings. If the [ g ] directive is not included then the change is performed only on the first occurance of a match on each line.
Sometimes you may want to use the original search string in the replacement result. You could retype the command on the line but vi allows the replacement string to contain some special characters.
:1,5s/help/&ing/g Replaces help with helping on the first 5 lines. :%s/ */&&/g Double the number of spaces between the words.
Using the complete match string has its limits hence vi uses the
escaped parentheses [
( ] and [
) ] to select the range of the
substitution. Using an escaped digit [
1 ] which identifies the
range in the order of the definition the replacement can be build.
:s/^\(.*\):.*/\1/g Delete everything after and including the colon. :s/\(.*\):\(.*\)/\2:\1/g Swap the words either side of the colon.
You will most likely read the last series of gems again. vi offers powerfull commands that many more modern editors do not or can not offer. The cost for this power is also the main argument against vi. The commands can be difficult to learn and read. Though most good things can be a little awkward at first. With a little practice and time, the vi command set will become second nature.