Ex Reference Manual Version 3.7

1. Starting ex
2. File manipulation
2.1. Current file
2.2. Alternate file
2.3. Filename expansion
2.4. Multiple files and named buffers
2.5. Read only
3. Exceptional Conditions
3.1. Errors and interrupts
3.2. Recovering from hangups and crashes
4. Editing modes
5. Command structure
5.1. Command parameters
5.2. Command variants
5.3. Flags after commands
5.4. Comments
5.5. Multiple commands per line
5.6. Reporting large changes
6. Command addressing
6.1. Addressing primitives
6.2. Combining addressing primitives
7. Command descriptions
.
a!
.
args
.
c!
.
.
i!
.
j!
n!
preserve
q!
rew!
stop
^D
8. Regular expressions and substitute replacement patterns
8.1. Regular expressions
8.2. Magic and nomagic
8.3. Basic regular expression summary
8.4. Combining regular expression primitives
8.5. Substitute replacement patterns
9. Option descriptions
w300, w1200 w9600
10. Acknowledgements

William Joy

Mark Horton

Computer Science Division
Department of Electrical Engineering and Computer Science
University of California, Berkeley
Berkeley, Ca. 94720

ABSTRACT

Ex a line oriented text editor, which supports both command and display oriented editing. This reference manual describes the command oriented part of ex; the display editing features of ex are described in An Introduction to Display Editing with Vi. Other documents about the editor include the introduction Edit: A tutorial, the Ex/edit Command Summary, and a Vi Quick Reference card.

1. Starting ex

Each instance of the editor has a set of options, which can be set to tailor it to your liking. The command edit invokes a version of ex designed for more casual or beginning users by changing the default settings of some of these options. To simplify the description which follows we assume the default settings of the options.

When invoked, ex determines the terminal type from the TERM variable in the environment. It there is a TERMCAP variable in the environment, and the type of the terminal described there matches the TERM variable, then that description is used. Also if the TERMCAP variable contains a pathname (beginning with a /) then the editor will seek the description of the terminal in that file (rather than the default /etc/termcap). If there is a variable EXINIT in the environment, then the editor will execute the commands in that variable, otherwise if there is a file .exrc in your HOME directory ex reads commands from that file, simulating a source command. Option setting commands placed in EXINIT or .exrc will be executed before each editor session.


ex [ - ] [ -v ] [ -t tag ] [ -r ] [ -l ] [ -wn ] [ -x ] [ -R ] [ +command ] name ...

The most common case edits a single file with no options, i.e.:

ex name

The - command line option option suppresses all interactive­user feedback and is useful in processing editor scripts in command files. The -v option is equivalent to using vi rather than ex. The -t option is equivalent to an initial tag command, editing the file containing the tag and positioning the editor at its definition. The -r option is used in recovering after an editor or system crash, retrieving the last saved version of the named file or, if no file is specified, typing a list of saved files. The -l option sets up for editing LISP, setting the showmatch and lisp options. The -w option sets the default window size to n, and is useful on dialups to start in small windows. The -x option causes ex to prompt for a key, which is used to encrypt and decrypt the contents of the file, which should already be encrypted using the same key, see crypt(1). The -R option sets the readonly option at the start. Name arguments indicate files to be edited. An argument of the form +command indicates that the editor should begin by executing the specified command. If command is omitted, then it defaults to ``$'', positioning the editor at the last line of the first file initially. Other useful commands here are scanning patterns of the form ``/pat'' or line numbers,


The financial support of an IBM Graduate Fellowship and the National Science Foundation under grants MCS74­07644­A03 and MCS78­07291 is gratefully acknowledged.


USD:12­2 Ex Reference Manual e.g. ``+100'' starting at line 100.

2. File manipulation

2.1. Current file

Ex is normally editing the contents of a single file, whose name is recorded in the current file name. Ex performs all editing actions in a buffer (actually a temporary file) into which the text of the file is initially read. Changes made to the buffer have no effect on the file being edited unless and until the buffer contents are written out to the file with a write command. After the buffer contents are written, the previous contents of the written file are no longer accessible. When a file is edited, its name becomes the current file name, and its contents are read into the buffer.

The current file is almost always considered to be edited. This means that the contents of the buffer are logically connected with the current file name, so that writing the current buffer contents onto that file, even if it exists, is a reasonable action. If the current file is not edited then ex will not normally write on it if it already exists.*

2.2. Alternate file

Each time a new value is given to the current file name, the previous current file name is saved as the alternate file name. Similarly if a file is mentioned but does not become the current file, it is saved as the alternate file name.

2.3. Filename expansion

Filenames within the editor may be specified using the normal shell expansion conventions. In addition, the


2.4. Multiple files and named buffers

If more than one file is given on the command line, then the first file is edited as described above. The remaining arguments are placed with the first file in the argument list. The current argument list may be displayed with the args command. The next file in the argument list may be edited with the next command. The argument list may also be respecified by specifying a list of names to the next command. These names are expanded, the resulting list of names becomes the new argument list, and ex edits the first file on the list.

For saving blocks of text while editing, and especially when editing more than one file, ex has a group of named buffers. These are similar to the normal buffer, except that only a limited number of operations are


2.5. Read only

It is possible to use ex in read only mode to look at files that you have no intention of modifying. This mode protects you from accidently overwriting the file. Read only mode is on when the readonly option is set. It can be turned on with the -R command line option, by the view command line invocation, or by setting the readonly option. It can be cleared by setting noreadonly. It is possible to write, even while in read only mode, by indicating that you really know what you are doing. You can write to a different file, or can use the ! form of write, even while in read only mode.

3. Exceptional Conditions

3.1. Errors and interrupts

When errors occur ex (optionally) rings the terminal bell and, in any case, prints an error diagnostic. If the primary input is from a file, editor processing will terminate. If an interrupt signal is received, ex prints ``Interrupt'' and returns to its command level. If the primary input is a file, then ex will exit when this occurs.


than replacing if upper case names are used.

Ex Reference Manual

3.2. Recovering from hangups and crashes

If a hangup signal is received and the buffer has been modified since it was last written out, or if the system crashes, either the editor (in the first case) or the system (after it reboots in the second) will attempt to preserve the buffer. The next time you log in you should be able to recover the work you were doing, losing at most a few lines of changes from the last point before the hangup or editor crash. To recover a file you can use the -r option. If you were editing the file resume, then you should change to the directory where you were when the crash occurred, giving the command

ex -r resume

After checking that the retrieved file is indeed ok, you can write it over the previous contents of that file.

You will normally get mail from the system telling you when a file has been saved after a crash.
command

ex -r

The

will print a list of the files which have been saved for you. (In the case of a hangup, the file will not appear in the list, although it can be recovered.)

4. Editing modes

Ex has five distinct modes. The primary mode is command mode. Commands are entered in command mode when a `:' prompt is present, and are executed each time a complete line is sent. In text input mode ex gathers input lines and places them in the file. The append, insert, and change commands use text input mode. No prompt is printed when you are in text input mode. This mode is left by typing a `.' alone at the beginning of a line, and command mode resumes.

The last three modes are open and visual modes, entered by the commands of the same name, and, within open and visual modes text insertion mode. Open and visual modes allow local editing operations to be performed on the text in the file. The open command displays one line at a time on any terminal while visual works on CRT terminals with random positioning cursors, using the screen as a (single) window for file editing changes. These modes are described (only) in An Introduction to Display Editing with Vi.

5. Command structure

Most command names are English words, and initial prefixes of the words are acceptable abbreviations. The ambiguity of abbreviations is resolved in favor of the more commonly used commands.*

5.1. Command parameters

Most commands accept prefix addresses specifying the lines in the file upon which they are to have effect. The forms of these addresses will be discussed below. A number of commands also may take a trailing count


in the buffer while ``delete 5'' will delete five lines from the buffer, starting with the current line.

Some commands take other information or parameters, this information always being given after the


5.2. Command variants

A number of commands have two distinct variants. The variant form of the command is invoked by placing an `!' immediately after the command name. Some of the default variants may be controlled by options; in this case, the `!' serves to toggle the default.

5.3. Flags after commands

The characters `#', `p' and `l' may be placed after many commands.** In this case, the command abbreviated by these characters is executed after the command completes. Since ex normally prints the new


substitute command, or a target address for a copy command, i.e. ``1,5 copy 25''.

** A `p' or `l' must be preceded by a blank or tab except in the single special case `dp'.

USD:12­4

current line after each change, `p' is rarely necessary. Any number of `+' or `-' characters may also be given with these flags. If they appear, the specified offset is applied to the current line value before the printing command is executed.

5.4. Comments

It is possible to give editor commands which are ignored. This is useful when making complex editor scripts for which comments are desired. The comment character is the double quote: ". Any command line beginning with " is ignored. Comments beginning with " may also be placed at the ends of commands, except in cases where they could be confused as part of text (shell escapes and the substitute and map commands).

5.5. Multiple commands per line

More than one command may be placed on a line by separating each pair of commands by a `|' character. However the global commands, comments, and the shell escape `!' must be the last command on a line, as they are not terminated by a `|'.

5.6. Reporting large changes

Most commands which change the contents of the editor buffer give feedback if the scope of the change exceeds a threshold given by the report option. This feedback helps to detect undesirably large changes so that they may be quickly and easily reversed with an undo. After commands with more global effect such as global or visual, you will be informed if the net change in the number of lines in the buffer during this command exceeds this threshold.

6. Command addressing

6.1. Addressing primitives

.

The current line. Most commands leave the current line as the last line which they affect. The default address for most commands is the current line, thus `.' is rarely used alone as an address.

n

$

%

+n

The nth line in the editor's buffer, lines being numbered sequentially from 1. The last line in the buffer.

An abbreviation for ``1,$'', the entire buffer.

An offset relative to the current buffer line.


/pat/ ?pat?

Scan forward and backward respectively for a line containing pat, a regular expression (as defined below). The scans normally wrap around the end of the buffer. If all that is desired is to print the next line containing pat, then the trailing / or ? may be omitted. If pat is omitted or explicitly empty, then the last regular expression specified is located.


´´ ´x

Before each non­relative motion of the current line `.', the previous current line is marked with a tag, subsequently referred to as `´´'. This makes it easy to refer or return to this previous context. Marks may also be established by the mark command, using single lower case letters x and the marked lines referred to as `´x'.

6.2. Combining addressing primitives

Addresses to commands consist of a series of addressing primitives, separated by `,' or `;'. Such address lists are evaluated left­to­right. When addresses are separated by `;' the current line `.' is set to the value of the previous addressing expression before the next address is interpreted. If more addresses are given than the command requires, then all but the last one or two are ignored. If the command takes two addresses, the first



It is an error to give a prefix address to a command which expects none.

Ex Reference Manual

7. Command descriptions

The following form is a prototype for all ex commands:

address command ! parameters count flags

All parts are optional; the degenerate case is the empty command which prints the next line in the file. For sanity with use from within visual mode, ex ignores a ``:'' preceding any command.

In the following command descriptions, the default addresses are shown in parentheses, which are not, however, part of the command.
abbreviate word rhs

Add the named abbreviation to the current list. When in input mode in visual, if word is typed as a complete word, it will be changed to rhs.

( . ) append
text

abbr: a


.

Reads the input text and places it after the specified line. After the command, `.' addresses the last line input or the specified line if no lines were input. If address `0' is given, text is placed at the beginning of the buffer.

a!

text

.

The variant flag to append toggles the setting for the autoindent option during the input of text.

args

The members of the argument list are printed, with the current argument delimited by `[' and `]'.

( . , . ) change count
text

abbr: c


.

Replaces the specified lines with the input text. The current line becomes the last line input; if no lines were input it is left as for a delete.

c!

text

.

The variant toggles autoindent during the change.

( . , . ) copy addr flags

A copy of the specified lines is placed after addr, which may be `0'. The current line `.' addresses the last line of the copy. The command t is a synonym for copy.

( . , . ) delete buffer count flags

Removes the specified lines from the buffer. The line after the last line deleted becomes the current line; if the lines deleted were originally at the end, the new last line becomes the current line. If a named buffer is specified by giving a letter, then the specified lines are saved in that buffer, or appended to it if an upper case letter is used.

edit file
ex
file

abbr: e

Used to begin an editing session on a new file. The editor first checks to see if the buffer has been modified since the last write command was issued. If it has been, a warning is issued and the command is aborted. The command otherwise deletes the entire contents of the editor buffer, makes the named file the current file and prints the new filename. After insuring that this file is sensible the editor reads the file into its buffer.


USD:12­6

If the read of the file completes without error, the number of lines and characters read is typed. If there were any non­ASCII characters in the file they are stripped of their non­ASCII high bits, and any null characters in the file are discarded. If none of these errors occurred, the file is considered edited. If the last line of the input file is missing the trailing newline character, it will be supplied and a complaint will be issued. This command leaves the current line `.' at the last line read.



e! file

The variant form suppresses the complaint about modifications having been made and not written from the editor buffer, thus discarding all changes which have been made before editing the new file.

e +n file

Causes the editor to begin at line n rather than at the last line; n may also be an editor command containing no spaces, e.g.: ``+/pat''.

file

Prints the current file name, whether it has been `[Modified]' since the last write command, whether it is read only, the current line, the number of lines in the buffer, and the percentage of the way through the buffer of the current line.*

file file

The current file name is changed to file which is considered `[Not edited]'.

( 1 , $ ) global /pat / cmds

First marks each line among those specified which matches the given regular expression. Then the given command list is executed with `.' initially set to each marked line.

The command list consists of the remaining commands on the current input line and may continue to multiple lines by ending all but the last such line with a `'. If cmds (and possibly the trailing / delimiter) is omitted, each line matching pat is printed. Append, insert, and change commands and associated input are permitted; the `.' terminating input may be omitted if it would be on the last line of the command list. Open and visual commands are permitted in the command list and take input from the terminal.

The global command itself may not appear in cmds. The undo command is also not permitted there, as undo instead can be used to reverse the entire global command. The options autoprint and autoindent are inhibited during a global, (and possibly the trailing / delimiter) and the value of the report option is temporarily infinite, in deference to a report for the entire global. Finally, the context mark `´´' is set to the value of `.' before the global command begins and is not changed during a global command, except perhaps by an open or visual within the global.

g! /pat/ cmds

The variant form of global runs cmds at each line not matching pat.

( . ) insert
text

abbr: i


.

Places the given text before the specified line. The current line is left at the last line input; if there were none input it is left at the line before the addressed line. This command differs from append only in the placement of text.


* In the rare case that the current file is `[Not edited]' this is noted also; in this case you have to use the form w! to write to the file, since the editor is not sure that a write will not destroy a file unrelated to the current contents of the buffer.

Ex Reference Manual

i!

text

.

The variant toggles autoindent during the insert.

( . , .+1 ) join count flags

Places the text from a specified range of lines together on one line. White space is adjusted at each junction to provide at least one blank character, two if there was a `.' at the end of the line, or none if the first following character is a `)'. If there is already white space at the end of the line, then the white space at the start of the next line will be discarded.

j!

The variant causes a simpler join with no white space processing; the characters in the lines are simply concatenated.

( . ) k x

The k command is a synonym for mark. It does not require a blank or tab before the following letter.

( . , . ) list count flags

Prints the specified lines in a more unambiguous way: tabs are printed as `^I' and the end of each line is marked with a trailing `$'. The current line is left at the last line printed.

map lhs rhs

The map command is used to define macros for use in visual mode. Lhs should be a single character, or the sequence ``#n'', for n a digit, referring to function key n. When this character or function key is typed in visual mode, it will be as though the corresponding rhs had been typed. On terminals without function keys, you can type ``#n''. See section 6.9 of the ``Introduction to Display Editing with Vi'' for more details.

( . ) mark x

Gives the specified line mark x, a single lower case letter. The x must be preceded by a blank or a tab. The addressing form `´x' then addresses this line. The current line is not affected by this command.

( . , . ) move addr

The move command repositions the specified lines to be after addr. The first of the moved lines becomes the current line.

next

The next file from the command line argument list is edited.

n!

The variant suppresses warnings about the modifications to the buffer not having been written out, discarding (irretrievably) any changes which may have been made.

n filelist
n +
command filelist

The specified filelist is expanded and the resulting list replaces the current argument list; the first file in the new list is then edited. If command is given (it must contain no spaces), then it is executed after editing the first such file.

( . , . ) number count flags abbr: # or nu

Prints each specified line preceded by its buffer line number. The current line is left at the last line printed.


USD:12­8

Ex Reference Manual

( . ) open flags
(
. ) open /pat / flags

abbr: o

Enters intraline editing open mode at each addressed line. If pat is given, then the cursor will be placed initially at the beginning of the string matched by the pattern. To exit this mode use Q. See An Introduction to Display Editing with Vi for more details.

preserve

The current editor buffer is saved as though the system had just crashed. This command is for use only in emergencies when a write command has resulted in an error and you don't know how to save your work. After a preserve you should seek help.

( . , . ) print count

Prints the specified lines with non­printing characters printed as control characters `^x '; delete (octal 177) is represented as `^?'. The current line is left at the last line printed.

( . ) put buffer

Puts back previously deleted or yanked lines. Normally used with delete to effect movement of lines, or with yank to effect duplication of lines. If no buffer is specified, then the last deleted or yanked text is restored.* By using a named buffer, text may be restored that was saved there at any previous time.

quit


Normally, you will wish to save your changes, and you should give a write command; if you wish to discard them, use the q! command variant.

q!

Quits from the editor, discarding changes to the buffer without complaint.

( . ) read file

Places a copy of the text of the given file in the editing buffer after the specified line. If no file is given the current file name is used. The current file name is not changed unless there is none in which case file becomes the current name. The sensibility restrictions for the edit command apply here also. If the file buffer is empty and there is no current name then ex treats this as an edit command.

Address `0' is legal for this command and causes the file to be read at the beginning of the buffer. Statistics are given as for the edit command when the read successfully terminates. After a read the current line is the last line read.



( . ) read !command

Reads the output of the command command into the buffer after the specified line. This is not a variant form of the command, rather a read specifying a command rather than a filename; a blank or tab before the ! is mandatory.

recover file

Recovers file from the system save area. Used after a accidental hangup of the phone** or a system crash** or preserve command. Except when you use preserve you will be notified by mail when a file is saved.


* But no modifying commands may intervene between the delete or yank and the put, nor may lines be moved between files without using a named buffer.


** The system saves a copy of the file you were editing only if you have made changes to the file.

Ex Reference Manual

USD:12­9

rewind

The argument list is rewound, and the first file in the list is edited.

abbr: rew

rew!

Rewinds the argument list discarding any changes made to the current buffer.

set parameter

With no arguments, prints those options whose values have been changed from their defaults; with parameter all it prints all of the option values.

Giving an option name followed by a `?' causes the current value of that option to be printed. The `?' is unnecessary unless the option is Boolean valued. Boolean options are given values either by the form `set option' to turn them on or `set nooption' to turn them off; string and numeric options are assigned via the form `set option=value'.

More than one parameter may be given to set ; they are interpreted left­to­right.

shell

A new shell is created. When it terminates, editing resumes.

source file

abbr: sh

abbr: so

Reads and executes commands from the specified file. Source commands may be nested.

( . , . ) substitute /pat /repl / options count flags

On each specified line, the first instance of pattern pat is replaced by replacement pattern repl. If the global indicator option character `g' appears, then all instances are substituted; if the confirm indication character `c' appears, then before each substitution the line to be substituted is typed with the string to be substituted marked with `­' characters. By typing an `y' one can cause the substitution to be performed, any other input causes no change to take place. After a substitute the current line is the last line substituted.

Lines may be split by substituting new­line characters into them. The newline in repl must be escaped by preceding it with a `'. Other metacharacters available in pat and repl are described below.

stop

Suspends the editor, returning control to the top level shell. If autowrite is set and there are unsaved
changes, a write is done first unless the form
stop! is used. This commands is only available where
supported by the teletype driver and operating system.
( . , . ) substitute options count flags

If pat and repl are omitted, then the last substitution is repeated. This is a synonym for the & command.

( . , . ) t addr flags

The t command is a synonym for copy.

ta tag