Previous Table of Contents Next


Escape to Unix

These commands allow you to interface with the UNIX utilities. You can temporarily exit to a shell without leaving the editor among other various functions.

sh Escape to a subshell, return to vi with a Ctrl-D, or exit.
!cmd [args] Escape to shell and perform single UNIX command with arguments.
rangew !cmd Escape to shell and perform single UNIX command on lines specified by range.
!! Repeat last ! command.
range!cmd Send lines specified by range to UNIX command and replace specified lines with output. See Filtering Text for further discussion.
stop Stop editor, place in background and return to top level shell. Berkeley UNIX only, fg command resumes editing.
Ctrl-Z Stop editor; same as the stop command.
!cmd % Escape to shell and perform single UNIX command with current file as an argument.

Searches

These commands search for the first occurrence of a pattern.


NOTE:  
Refer to Module 113 on Regular Expressions for special characters used for patterns in the search and substitute commands. For a brief synopsis of the regular expressions supported by ex and vi refer to the following section on Regular Expressions.




NOTE:  
The slashes (/) used in the following search and substitute commands are delimiters for the search, old, and new strings. You can use any character as the delimiters for these commands.



:/pat/cmd Perform ex command cmd on line matching first occurrence of pat.
Examples:
:/John Search for first occurrence of John. No command is given so nothing is done; only the current line is changed to the new location.
:?Tina Search backward for first occurrence of Tina.
:/John/p Search for first occurrence of John and print the line.
:// Repeat last search; search for John.
:?? Repeat last search backward.

Global Searches

The following commands search the entire file or range of lines specified and match each line containing the given pat tern.


NOTE:  
A search command (global) may be continued on the following line by typing a backslash (\) followed by a Return.


     :g/Test 1/s//Test 2/p\
     i\
     # RJF\
     .

Searches for all lines containing the pattern Test 1 and substitutes Test 2 in place of Test 1. Inserts the # RJF line before each line that was substituted.


range g/pat/cmd Perform ex command cmd on each line specified in range that has a matching pat. If range is not defined, then all lines are searched for a match. If cmd is not given, then the current line becomes the last line containing the string pat.
Examples:
:g/Total/ Search for last occurrence of Total. The line is not displayed on your screen.
:g/pat/nu Print all (global) occurrences of pat preceded with the line numbers.
:g/ME/s//I/gp Searches for all occurrences of ME and changes them to I and prints the line. The g in gp specifies global line substitution, which means all occurrences on a line are changed.

Global Exception Search

The following commands search the entire file or range of lines specified and match all lines NOT containing the given pat tern.

range v/pat/cmd Perform ex command cmd on each line specified in range that does NOT have a matching pat. If range is not defined, then all lines are searched for a NON-match. If cmd is not given, then the current line becomes the last line containing the string pat.
Examples:
:v/Total/ Search for last line that does not contain an occurrence of Total. The line is not displayed on your screen.
:v/Tom/nu Print all (global) lines that do not contain an occurrence of Tom. Each line is precede by its line numbers.
:v/ME/s/You/I/nu Searches for all lines that do not contain the ME pattern and changes the You pattern to I and prints the changed line preceded by a line number.

Substitutions

The substitute command is useful for changing specific portions of a line for a specified range of lines.

range s/old/new/opt count flags
Substitute old pattern with new on lines specified by range or count. If the range or count is not specified, the current line is assumed. The options may be c or g. See Substitute options that follow. The flags may be #, nu, p, l, +n, or -n. Refer to previous Command Format section.
range & options count flags
The & replaces the last s/old/new/ substitute command. You can also use the s in place of the &.
Substitute Options These options may be placed at the end of a substitute command.
c Interactive confirmation (answer y or n).
g Global substitutions for entire line.
Examples:
:s/We/Us/g Substitute all occurrences of We with Us on the current line.
:/Test/s/1.0/1.1/p Search for first line containing Test, then substitute first occurrence of 1.0 with 1.1 and print the changed line.
:100,$s/outlaw/inlaw/c Search lines 100 through end-of-file for outlaw and prompt to change the first occurrence to inlaw. If you respond to the prompt with a y then the substitution is done.
:~ Repeat the last substitution.

REGULAR EXPRESSIONS

The following table contains each regular expression and the task that it performs when used inside a pattern. A regular expression consists of regular alphanumeric characters matching themselves and special characters matching certain patterns of text. Regular Expressions are often referred to as REs in UNIX terminology. Thus we use the RE notation for uniformity and briefness. For further descriptions refer to Module 113, Regular Expressions.


Alphanumeric
RE Description

c Matches any ordinary character c.
string Matches the set of characters string.

Metacharacters

Metacharacters are the special characters used in regular expression patterns that have special meanings. Metacharacters are often referred to as special or magic characters or wild cards.


Special
RE Description

\ Escapes the meaning of a metacharacter.
^ Matches the beginning of the line.
$ Matches the end of the line.
. Matches any single character.
[class] A character class. Matches any one character in the class.
[c1-c2] Match any one of the ASCII characters in the range defined within the brackets.
[^class] Do NOT match any of the ASCII characters listed within the brackets. Ranges may be specified.
* Matches zero or more occurrences of the preceding regular expression.
\< Matches the beginning of a word.
\> Matches the end of a word.
\(\) Groups the regular expression RE for later reference; each parenthetical RE is referenced by a corresponding number (see \n). The first group is referenced by \1, the second group by \2, and so on. You can have a maximum of nine groups in one regular expression pattern.
\n Matches the nth grouped \(RE\) withing the same regular expression.
// Uses the last RE if it has been defined.

To have a metacharacter interpreted as a normal character precede it with a backslash (\).

Special Substitute Characters

There are special characters that you can use in the replacement (new) string of a substitute command to help speed up the process.

& Matches last search or substitution (old) string.
Example:
:s/born/new&/p
The newborn is doing fine.
:s/Tom/&my/p
My name is Tommy.
The first command substitutes born to newborn. The second substitutes Tom to Tommy.
~ Matches last replacement (new) string.
Example:
:s/finish/end/
:s/stop/~/
The first line substitutes end in place of finish. The second command substitutes the last replacement string, end in place of stop.
\l Converts the next character in the replacement string to lowercase.
Example:
:s/A big/\l&/p
a big
The substitute command replaces the string A big with the string a big.
\L Converts all following characters of the replacement string to lowercase until a \e or \E is encountered.
Example:
:s/THIS/\L&\E/p
this
\u Converts the next character in the replacement string to uppercase.
\U Converts all following characters of the replacement string to uppercase until a \e or \E is encountered.
\n The nth grouped \(RE\).
\E or \e Turn off the action of the \U or \L strings.


Previous Table of Contents Next