Previous Table of Contents Next

Module 43


The external ex command is a powerful line editor that forms the base of the vi editor. It is a supet-set of the original UNIX line editor ed. The basic line editing commands, macros, abbreviations, tags, substitutions, options, the EXINIT variable, and the .exrc file are discussed in this module. The visually oriented aspects of ex/vi are discussed in Module 151.


Following is the general format of the ex command.

     ex [ -CRLsvx ] [ -r lost ] [ -t tag ] [ -c cmd ] [ file ]

BSD (Berkeley)
ex [ - ] [ -lv ] [ -r lost ] [ -t tag ] [ +cmd ] [ file ]


The following list describes the options and their arguments that may be used to control how ex functions.

-C Same as -x below except all text read into the buffer is assumed to be encrypted, unless a null key is specified.
-R Read-only mode; no updates allowed.
-L List the names of all files saved when the system crashed or the editor aborted.
-c cmd Display buffer after interpreting an ex command cmd. Enclose commands with special characters in single quotes ('cmd') to escape shell interpretation.
-r [lost] Recover lost file that was preserved during abnormal termination of ex/vi. Use -r option alone for list of preserved files.
-s Suppress all standard output (interactive feedback). Used for batch editor script processing.
-t tag Starting at tag, edit the file that is referenced by tag in the "tags" file.
-x Edit encrypted files. You are prompted for an encryption key.
-v Invoke vi.

BSD (Berkeley)
- Same as System V -s option.
-l Sets the showmatch and lisp mode options. The ( ), { }, [ [ and ] ] commands are changed to indent for lisp code.
+cmd Same as System V -c cmd. Plus (+) with no cmd begins editing at last line of buffer.


The following argument may be passed to the ex command.

file_list One or more files to edit. If you specify more than one file, ex edits the first file on the command line. To edit the next file, you enter :n and press Return.

Table of Contents

The following is a brief table of contents that will help you locate information in this module.

vi Mode
Basic Addressing
Address Ranges
Command Format
Displaying Text
Window Sizing
Inserting Text
Changing Text
Copying Text
Deleting Text
Marking Positions
Moving Text
Moving Text (cut & paste)
Yanking (cutting)
Putting (pasting)
Moving text between files
Indenting Text
Filtering Text
Join Lines
Undo, Redo, and Retrieve
Edit/Re-edit a File
Edit/Re-edit Multiple Files
Read from a File
Write to a File
Escape to Unix
Global Searches
Global Exception Search
Substitute Options
Special Substitute Characters
Tag File Format
Referencing a Tag
The Options
The .exrc File
The EXINIT Variable


The ex editor is a line editor. Most of the commands are line oriented. Thus to perform character changes and deletions you have to use the substitute command or open mode. Open mode is visual editing on one line of text. You may find these methods to be tedious and not very productive, which is why vi exists.

It is also necessary to provide unique amounts of each command's name. This causes the names in ex to have more meaning than the single keystrokes used in vi but may cause some confusion between the two editors. A suggestion is to utilize vi for all character and line oriented editing and the moving of text with operators. While using ex to perform file operations, substitutions, setting macros, setting abbreviations, and tags. Obviously, there is overlapping of functions but in general, ex supplies a gateway to administrative commands used while in vi.

Each command in ex command mode must be followed by a Return. When you are in vi command mode, the characters are interpreted as they are typed and no Return is needed in most cases.

Interface capabilities

The ex editor can interface with vi mode to provide full-screen editing. It has the ability to interface to the UNIX system. A subshell may be entered from ex and UNIX WP NYcommands executed until a Ctrl-D or exit is entered. UNIX commands can be executed from ex without leaving the editor. The vi mode can interface with UNIX filters to allow for special editing capabilities.


You can move between the ex and vi editors with simple commands. The vi editor is the visual extension of the ex editor. To access vi from ex you simply type vi at the colon prompt. You must have your TERM variable set correctly for vi to function properly. ex also requires limited knowledge of your terminal type for certain commands and features. Refer to the sections in the vi module that discuss the requirements for setting up your environment to use vi.

vi Mode

The following commands may be used to interface with the vi screen editor and return to the ex screen editor.

:vi Enter/Return to vi mode. If you are in ex you can enter or return to vi.
:open Enter into open mode, a pseudo screen control mode entered from ex. Makes it easier to view text. In open mode you can make visual changes to individual lines. You're halfway to vi.
Enter/Return to ex mode, return to vi mode with :vi. You may want to use ex if you have a lot of substitutions to perform, or if you entered vi with the wrong TERM definition. You can enter ex mode and type set term=termtype and then type vi to return to vi.
: Enter temporary ex mode while in vi. Precede each ex command you need to perform with a : (colon). This causes vi to send the command to ex for processing.


The ex editor has many ways of addressing lines of text within the current session buffer. The addresses should always precede the command they are affecting.

Basic Addressing  The following table describes the basic addressing syntax available for your use.

Symbol Description

If no address is specified the current line is affected or used for the address.
. Refers to the current line of the buffer. When you enter ex the last line is the current line.
$ The last line of the buffer.
% The entire buffer (lines 1,$).
n Refers to line number n.
+ Refers to the next line.
+n Refers to n lines forward.
- Refers to the previous line.
-n Refers to n lines backward.
/pat/ Searches forward for the next line containing the string pat.
?pat? Searches backward for the first previous line containing the string pat.
// Repeats the last forward search.
?? Repeats the last backward search.
'x Jumps to the line with mark x.
'' Jumps to the line that was current before the last nonrelative motion.
line Any of the above basic addresses.

Address Ranges  Some ex commands accept ranges of lines. The following table describes the available ranges.

Symbol Description

a1,a2 Specifies a range of lines beginning at line address a1 and continuing through line address a2. Any of the basic addresses are valid. But a1 must address a line that precedes address a2.
For example,
/John/,/Mary/ d
deletes all lines from the line where the string John was found to the line where the string Mary was found. This assumes John resides on a line previous to the line where Mary resides.
a1;a2 Specifies a new current line and the number of lines to offset from the new current line. The address a1 becomes your new current line and a2 becomes the number of lines past the current line. For example,
10;5 co 50
copies line 10 through 15 to line 50. The 10 sets your current line to line 10; the 5 includes the next 5 lines.
range Any of the above addresses.

If NO address is specified in an ex command, the current line is usually the default.


These characters have special meaning to the ex editor when you are at the colon (:) command prompt.

% Name of current buffer/file. Address for entire buffer, lines 1 through end-of-file.
# Name of alternate buffer/file, last file edited or used in ex command.
Is also a command alias for the number command.
! Ignore changes when used with :e, :w, :n. If you have modified the contents of the buffer, ex will normally not allow you to perform these functions without saving the buffer to disk. The exclamation mark (!) overrides this feature.
Also used as a temporary escape to UNIX to execute a single UNIX command line.
!! Repeats the last escape to UNIX command.
| Sequential ex command separator. You can place multiple ex commands on the command line and separate them with a vertical bar.
; Sequential ex command separator for tag file use. If you want to have multiple ex commands performed for a specific tag, you can separate the ex commands with the semicolon in your tag file.

Previous Table of Contents Next