Your Browser is not longer supported

Please use Google Chrome, Mozilla Firefox or Microsoft Edge to view the page correctly
Loading...

{{viewport.spaceProperty.prod}}

ex - command and display editor

&pagelevel(4)&pagelevel

ex is a line-based text editor.

Only users who have accessed the POSIX shell via rlogin can use ex (except in line mode).

ex offers you various operating modes:

  • In the ex input mode you enter text directly.

  • In the ex command mode you enter commands. Among other things, commands enable you to:

    • position the cursor

    • search for (and replace) text patterns with the aid of regular expressions

    • switch to another file

    • invoke a shell

You also have the option of switching from the line editor ex to the screen editor vi.

Layout of this description

The description of how to call ex on POSIX shell level is followed by the sections:

Functionality of ex

  • Operating modes of ex

  • Saving the editor buffer and quitting ex

  • Presetting ex

  • Current and alternate files

  • Regular expressions

  • Replacement strings

  • Buffers

  • Error and signal handling

ex commands

  • Addressing

  • Parameters

  • Commands

ex options


Syntax


ex[ option] file...

option

-s

(silent)

All interactive output of the editor is suppressed. The -s option must be set when ex is to read its commands from a command script.

The -s option has replaced the old - option.

-t tag

(tag) ex loads the file containing the tag for editing and positions the tag definition under the cursor at the center of the screen. The tags file containing the search strings for the definitions must be in the same directory. This option is typically used by C programmers to quickly locate the definition of a function or macro on calling the editor. The tags file needed for this purpose must first be built with the ctags command.

-r file

(recover)

Restores the ex session in which you were editing file if ex or the system crashed during your session.

Changes that are only in the editor buffer are not copied to the edited file after a system or ex editor crash. POSIX does, however, try to save the buffer contents by creating a copy of it, if possible. file is recovered, including any changes made prior to the crash, and is retrieved into the vi buffer. You can now continue editing the file or save the changes in another file.

-L

Lists the names of all files saved following an editor or system crash.

-w n

Sets the value of the ex window option (see "ex command and display editor") to n.

-R

(readonly)

The named file is opened in readonly mode. This mode prevents the original file from being accidentally overwritten, but allows you to write the buffer contents to a file with another name.


Caution!
The file can still be overwritten with w! in the ex command mode.


-v

(vi)

Invokes the vi editor. A detailed description of vi is provided in this manual (see vi on "vi screen oriented (visual) display editor").

-c command

Positions the editor at a specific line of the file to be edited or executes an ex command when ex is invoked. If you position the editor at a line, the selected line will be displayed in the center of the screen. By contrast, if you have an ex command executed, the editor will be positioned at the last line of the file after executing the command, unless the ex command you specify (e.g. search) positions the editor itself.

command not specified:
The ex editor is positioned at the end of the file.

command specified:
command may be specified as a line number (n), a search command (/pattern), or any other ex command (see “Commands”). The specified command is executed on invocation of ex:


n

n must be an integer. ex is positioned to the nth line of the file.

/pattern

ex is positioned to the first line containing pattern. If the pattern you specify includes metacharacters, you must escape them to protect them from being interpreted by the shell.

'ex-command' or "ex-command"

ex-command can be any ex command. The ex command must be quoted (using single or double quotes) in order to prevent it from being interpreted by the shell. If the editor is not already positioned as a result of an ex command, ex will be positioned to the last line of the file.


Example

When ex is called with:

ex -c /tuesday appointments

the file appointments is opened, and the cursor is positioned at the first line that contains the word tuesday.

file

Name of the file that you wish to edit. If you specify a number of files, these files will be processed in the order in which they are listed. The ex command n can be used to switch to the next file.

file not specified:
ex opens an empty buffer into which you can write your text. The contents of this buffer are not assigned a name until you write them back (with w) to a file or assign a name explicitly using the f command.


Caution!
If your file contains null characters, ex will discard them when reading in the file. No null characters appear in files written by ex.

Functionality of ex

ex always works on a buffer, not on files. When you start an ex session, a working copy of the file you are editing is read into the editor buffer. All the editing changes you make are initially performed in this buffer and are not saved until you use the w (write) command to copy the buffer contents back into the original file.
Once you have done this, you can quit ex using the q command.
If you wish to exit the ex editor without writing back the changes to the original file, you must enter q!.

Null ASCII characters (see section “ASCII character set (ISO 646)”) are discarded in input files and thus cannot appear in output files.

Operating modes of ex

ex offers you two file editing modes:

  • the ex command mode, and

  • the ex input mode.

You can also switch from ex to the vi editor, which provides you with additional operating modes (for more details see vi, “vi modes” (vi screen oriented (visual) display editor)).

When ex is invoked, it comes up in command mode, as indicated by the prompt : displayed on the screen. The commands a (append), i (insert), and c (change) can be used to switch to the input mode, which enables you to add or modify text in the buffer (see “Commands”).

In input mode, all subsequent input characters, including various non-printing characters, are written into the buffer. Commands are not interpreted as such. You can exit from input mode by entering a period (.) in the first column and then pressing the enter key.

Saving the editor buffer and quitting ex

ex must be in command mode to save the editor buffer or to quit the ex editor.

Use the following command to save the contents of your editor buffer to the file edited or that specified:

w[ file]

(write)

The contents of the editor buffer are saved to the file specified.

file not specified:
The contents of the editor buffer are written back to the edited file.


You can quit ex using one of the following options:

q

(quit)

Quit ex. This works only if the editor buffer has not been modified at all or if the modified editor buffer has been saved to a file.

q!

(quit)

Quit ex. Unsaved modifications made to the editor buffer are not saved.

x

Quit ex and write the modified editor buffer to the edited file.

wq[ file]

(write and quit)

Quit ex and write the modified editor buffer to the file specified.

file not specified:
The contents of the editor buffer are written to the edited file.


Caution!
ex does not issue a warning if you place text in named buffers and do not save it before exiting the editor. The text is irretrievably discarded.

Presetting ex

The ex editor can, to some extent, be adapted to suit your requirements and working habits. This entails using the ex command se (set), which allows you to set or change specific options (see “ex options”). The changes you make remain in effect for the current session. Details on how to change default settings permanently can be found in the vi command description (see "vi screen oriented (visual) display editor").

Current and alternate file

The file (or its copy) being edited by ex at any given moment is referred to as the current file. The alternate file is the file most recently mentioned in an editing command. If you switch to the alternate file, the file you switch from becomes the new alternate file. When specifying files, you can use the % character for the current file and the # character for the alternate file. These characters are replaced in file names by the names of the current file and the alternate file, respectively.

Example

w %.bak

backs up the current file to a file with the same name but with the extension .bak.

Regular expressions

The interpretation of metacharacters in regular expressions depends on whether or not the magic option has been set for the ex editor (see “ex options”).

When magic is set:

character

An ordinary character stands for itself. The following are metacharacters, not ordinary characters:

^

(caret) at the beginning of a pattern

$

(dollar sign) at the end of a pattern

*

(asterisk) anywhere except at the beginning of a pattern

.

(period) at any position in a pattern

[

(left square bracket) at any position in a pattern

~

(tilde) at any position in a pattern

Metacharacters have special meanings and must be escaped with a backslash \ if they are to lose their special meanings.

^

^ at the beginning of a pattern stands for the beginning of the line.

$

$ at the end of a pattern stands for the end of the line.

.

stands for any one character.

\<

\>

\< matches the beginning of a word. The word must begin with a letter, digit, or underscore and be preceded by the beginning of the line or a character other than the above. \> matches the end of a word.

[string]

Any character contained in string, where string is not a sequence of blank characters. The following characters have special meanings within string:

  • a dash - between a pair of characters defines a range, e.g. [a-z] matches a, z, and all the characters which come between them in the ASCII collating sequence.

  • if a caret ^ is the first character in string, it causes the construct to match any characters not included in string.

If these characters are escaped by a backslash, they lose their special meanings.

*

Zero, one or more occurrences of the regular expression preceding the * character.

~

Matches the replacement string used in the last s command (substitute, see “Commands”).

\(pattern\)

A regular expression pattern can be enclosed in parentheses escaped by backslashes. This serves only to identify them for substitution actions (see Replacement strings below). 

rs

A sequence of two regular expressions r and s is a regular expression in itself. rs matches all strings which consist of a string matching r followed by a string matching s.

When nomagic is set:

If nomagic has been set, the only characters that have special meanings are:

  • caret ^ at the beginning of a pattern

  • dollar $ at the end of a pattern, and

  • backslash \.

The following characters only have a special meaning if they have been escaped by a preceding backslash \:

  • period .

  • asterisk *

  • left square bracket [

  • tilde ~

Replacement strings

The ampersand character & (with a preceding backslash, i.e. \&, if nomagic is set) in the replacement string stands for the text matched by the pattern, i.e. the text to be replaced.

The tilde ~ (\~ if nomagic is set) is replaced by the replacement string used in the previous s (substitute) command.

The sequence \n, where n is an integer, is replaced by the text that is matched by the pattern enclosed in the nth set of parentheses \(...\).

If the replacement string includes the sequence \u or \l, the character that immediately follows \u (upper) or \l (lower) in the replacement string will be converted to uppercase (for u) or lowercase (for l) if this character is a letter. The sequence \U or \L converts into uppercase or lowercase all letters until the end of the replacement string or until the sequence \E or \e is encountered.

Buffers

ex uses one unnamed and 26 named buffers, named a through z.

You can use these buffers to copy text with ya (yank), to save deleted text with d (delete), and to subsequently retrieve this text with pu (put).

The d, pu, and ya commands use the unnamed buffer unless you explicitly name a buffer. In other words, if you do not specify a buffer for a delete operation, for example, the material you delete will be saved in the unnamed buffer.

You can save blocks of text in up to 26 buffers. The buffers are designated in either lowercase or uppercase letters from a-z (or A-Z). If you use lowercase letters, the old contents of the buffer will be overwritten by the new text, i.e. the old contents are deleted. If you use uppercase letters, the old buffer contents are not overwritten; the new text is appended to the old one instead.

Error and signal handling

When an error occurs during an ex session, ex sends the BEL character (acoustic signal; see section “ASCII character set (ISO 646)”) to the terminal and prints an error message.

If an interrupt signal is received, ex returns to command mode, allowing you to enter a new ex command.
If the editor input is from a file, ex exits at the interrupt signal.

Entering commands

Unlike most vi commands, which are interpreted and executed by vi immediately, ex commands must be "dispatched" by pressing the enter key.

Command lines which start with double quotes " are ignored. This means that you can use lines of this kind to insert comment lines in a command script.

Addressing

An address is used to specify a particular line. Some ex commands expect one or more addresses as input. The specified line or line range (an inclusive range of all lines from the first address to the second) is then processed by the ex command.

For ex, there is at all times a current line. This is explicitly addressed by a period (.). As a rule, the current line is the line last processed by a command or the line to which the editor has been explicitly positioned (e.g. by a search command).

Addresses must be separated from one another by a comma or a semicolon. Such address lists are evaluated by ex from left to right.

  • When a semicolon (;) is used as a separator, the current line is set to the value of the first address before the second address is interpreted.

  • When a comma (,) is the separator, all addresses are calculated relative to the current line. The current line is not changed until the command is executed.

If more addresses are given than the command requires, then all but the last address (when a line is expected) or the last two addresses (if a range is expected) are ignored. When a command requires two addresses, the first line that is addressed must precede the second one in the buffer. If you do not specify any address (null address), ex will use the current line by default.

address
. Current line of the buffer.
$ Last line of the buffer.
n nth line in the buffer. The lines are numbered sequentially from 1.
' xThe line marked with the letter x, where x must be a lowercase letter (see “Commands”, ma or k). 'x is the address of the marked line. Before ex executes a movement command, the current line is marked. You can return to it with '' (2 single quotes).
/RE/

A simple regular expression enclosed in /.../ (see section “Regular POSIX shell expressions”) addresses the first line which contains a character string which matches the regular expression, searching forward from the current line. If necessary, ex wraps around from the end of the buffer to its start to continue the search (see “ex options”, wrapscan). If the line matching RE is only to be printed, the second / may be omitted.

RE not specified:
The previous specified pattern is used.

?RE? As RE, but searching backward.
+n
-n
If an address starts with a plus or minus sign followed by a decimal number n (optional), the line that lies n lines after (+) or before (-) the current line will be addressed. Thus, .+3, +3 and +++ have the same effect.
adr+
adr-
If an address ends with a plus or minus sign, the line that is located one line after (+) or before (-) the line identified by the address is referenced. A single - refers to the line before the current line. The + or - signs at the end of an address have a cumulative effect; the address ++ thus addresses the second line after the current line, 3++ addresses line 5, and ++ and .+2 are identical.
% The % (percent) sign stands for the address pair 1,$, i.e. the entire buffer.

Parameters

The following parameters are used with ex commands:

line = address

A single line address, which you can specify in any of the forms described earlier in the Addressing section.

line not specified:
The default value (the period .) applies. The period stands for the current line.

range = address,address 
range = address;address

A pair of line addresses that defines an inclusive range of lines. The addresses can be separated by a comma or a semicolon. A range should not be specified together with a number n, since the last address of the specified range will then be interpreted as the first address of a range of n lines that begins with the last address. In other words, you would end up addressing a range that comes after the one intended. This has been taken into account in the ex command syntax.

range not specified:
The default value .,. applies. 
.,. represents only the current line.

n

A positive integer. In n you specify the number of lines to be processed.

n not specified:
The default value is 1.

flag

One or a combination of the ex commands # (see nu), p, and l. These commands display a line in a specific format and are executed after the preceding ex command completes.

' '

The blanks need not always be specified. However, in the interest of clarity, they have not been indicated as optional here.


Any number of plus or minus characters may be combined with these parameters.

Commands

Overview of ex commands and their abbreviations

CTRL+D(scroll)display n lines, where n=$scroll (see options)
ab abbrevdefine abbreviations
a appendadd text after specified line
ar argsdisplay argument list of command line
c changereplace text line
co copycopy text range
d deletedelete text range
e editchange edited file
f filedisplay current file name
g globalexecute command for all lines matching /RE/
i insertinsert text lines
j joinjoin two lines
l listoutput text with non-printing characters 
map ---define macros
k markmark lines
m movemove line range
ma markmark lines
n nextedit next file
nu numbernumber and output lines
# numbernumber and output lines
pre preservepreserve contents of buffer
p printprint range of text
pu putput lines back in buffer
q quitquit the editor
r readread copy of a file into the buffer
rec recoverreconstruct file after system crash
rew rewindrewind argument list (cf. ar)
se setdisplay and set options
sh shellcall shell
so sourceread command from file
s substitutesearch and replace
unab unabbrevcancel word from abbreviation list made with do
u undoundo effect of previous command
unm unmapremove macro definition made with map
v ---like g, but for all lines that do not match /RE/
ve versionoutput version number of the editor
vi visualswitch to vi
w writewrite buffer contents to file
x exitquit editor after saving buffer content
ya yankcopy lines to buffer
z (window)output specific text range
! (escape)execute shell command
&s (resubst)repeat the last substitute command
< (lshift)shift text range to left
> (rshift)shift text range to right
= (line no)display the line number


No command specified

If you specify only a line or a range, the command p (print) is automatically assumed and executed. If a null line is entered, the next line is printed (equivalent to .+1p)

CTRL+D

(ASCII EOT) prints the next n lines, where n is the value of the ex option scroll.

ab[brev] word text

(abbreviate) This command is only effective in vi mode!

If you enter word in the vi input mode as a complete word (i.e. not as a word fragment), it is replaced by the string text. If text is to include special characters, e.g.newline, these characters must be preceded by CTRL+V.

[line] a[ppend][!]

(append) Causes ex to enter input mode, placing the input text after the specified line. Use a line value of 0 to put a line at the beginning of the buffer. The last line of new input becomes the new current line. If there is no new input, the target line becomes the current line.
Input is terminated by a period (.) entered in column 1. 

ar[gs]

(arguments) Prints the argument list from the command line of the ex call, enclosed in square brackets [...].

line  c[hange][!] n
range  c

(change) ex switches to input mode. n lines or the lines in range are replaced by the lines you enter next.
The last input line becomes the current line. If no text lines are entered, the specified range is deleted, i.e. c has the same effect as the d (delete) command. Input is terminated by a period (.) entered in column 1.

chd[ir][!] [directory]
cd[!] [directory]

(chd/cd - change directory) ex changes the current working directory to directory.

directory not specified:
The directory name in the HOME environment variable will become the new working directory.

[range] co[py] line[ flags]
[range] t line[ flags]

(copy) Copies the lines specified by range to after line. If line is assigned a value of 0, the range of lines (i.e. text) will be copied to the beginning of the buffer.

[line] d[elete][ buffer][ n]
[bereich] d[elete][ buffer]

(delete) The lines in range, or n lines from the given line, are deleted from the buffer. If a named buffer is specified, the deleted text is saved in it. You can use the ex command pu to retrieve the contents of this buffer if you wish. The first line following the deleted material becomes the current line, unless the deleted lines were at the end of the buffer. In that case, the last line in the buffer becomes the current line.

e[dit][!][ +line][ file]
ex[!][ +line][ file]

(edit) The named file is edited. If the current buffer has been modified since the last w (write) command, ex warns you and aborts the command. You can, however, force the execution of the e command by appending an exclamation mark to the e: e! file. The current line is the last line of the buffer; however, if this command is called from vi, the current line becomes the first line of the buffer.

+line

The specified line becomes the current line. line can be given as:

  • a line number n

  • the dollar symbol $ (for end of file)

  • a regular expression in the form /RE or ?RE.

f[ile][ name]

(file) Prints the current file name and other information, e.g. the number of lines and the position of the current line.

name specified:
name becomes the current file name

range g[lobal]/RE/cmds

(global) First marks all lines within the given range that match the pattern specified in RE; then applies the given cmds to the each of marked lines in turn, with each marked line becoming the current line while it is being edited.

The commands given in cmds may be specified on separate lines, provided newline characters are escaped with a backslash. If cmds are omitted, all lines will be printed. The a(ppend), c(hange), and i(nsert) commands are allowed; the terminating period at the end of cmds may be omitted. The visual command is also permitted, and takes input from the display terminal.

The following commands are not permitted in cmds: global and undo.

The following edit options are not allowed in cmds: autoprint, autoindent and report.

Example

1,$g/^\*/s//+/

All occurrences of * in the first column ^ of line 1 and of all lines through to the end of the file ($) are replaced by a plus character (+) by the substitute command (s).

[line] i[nsert][!]

(insert) ex enters input mode; the input text is placed before the specified line. The last line inserted becomes the current line, but if no text is inserted, the line before the target line becomes the current line. Input is terminated by a period (.) entered in column 1.

[line] j[oin][!][ n][ flag]
[range] j[oin][!][ flag]

(join) Joins the lines in range, or n lines from line, into one line. The command inserts at least one blank between two previously split lines; two blanks are provided if a line ends with a period. If a following line begins with a right parenthesis, no blanks are inserted. Extra white space at the start of a line is discarded.

Appending an exclamation mark to the j command (j!) produces a simpler join with no white space processing.

[line] l[ist][ n][ flag]
[range] l[ist][ flag]

(list) Displays the specified lines. Tab characters are replaced by ^I, and the end of each line is marked with a trailing $. The only useful flag is #, when the lines are to be preceded by their line numbers. The last line printed becomes the current line.

map[ x commands] (Format 1)
map![ x text]    (Format 2)

Only works with vi!

Format 1: Macro definitions for vi command mode

This format of the map command is used to define macros for use in the vi command mode. The first argument x is a single character, or the sequence #n, where n is a digit that refers to function key n. When this character or function key is typed in vi command mode, vi responds as if the corresponding commands were entered. The specified commands are interpreted as a sequence of vi commands. If special characters, white space, or newline characters are to be used in the commands, they must be escaped with CTRL+V.

Example 1

You want to define a macro which replaces an * occurring at the beginning of a line (column 1) by a blank ' ' throughout the entire file. The name of this macro is to be *. Enter the following in your .exrc file (see section Presetting ex) so that you can call the macro when required:

:map * :1,$s/^\*/  /

Example 2

You now want to define a macro and bind it to function key F1. This macro is to search for the next line that begins with a particular regular expression RE and then automatically delete this line.
To do this, you enter (from vi command mode):

:map CTRL+V F1 /^rA CTRL+<Enterdd <Enter>

Format 2: Macro definitions for vi input mode

This format of the map command defines macros for use in vi input mode (see
Format 1 above). Every x entered in vi input mode is replaced by text.
(In the ab command, by contrast, x is replaced only when alone).

If x is not to be replaced by text, it must be escaped by preceding it with CTRL+V.

Formats 1 and 2:

Macro definitions can be cancelled with:

unmap x or unmap! x

[line] ma[rk] x
[line] k x

(mark) Marks the specified line with the character x, which must be a single lowercase letter. The x must be preceded by a blank or tab. The current line position is not affected.

[range] m[ove] line

(move) Moves the specified lines (range) to after the target line. The first of the moved lines becomes the current line.

n[ext][!][ file]...

file not specified:
(next) The next file from the command line argument list is edited (cf. f and ar). If the current buffer has been modified since the last w (write) command, ex warns you and aborts the command. You can, however, force the execution of the n command by entering an exclamation mark after the n: n!.

file specified:
The previous argument list is replaced by the specified file(s), and the first specified file is edited.

[line] nu[mber][ n][ flag]
[range] nu[mber][ flag]
[line] #[ n][ flag]
[range] #[ flag]

(number) Prints the lines, each preceded by its line number. The only useful flag in this case is l. The last line printed becomes the current line.

pre[serve]

(preserve) The current editor buffer is saved as though the system had just crashed. The pre command is for use in emergencies, e.g. when a w (write) does not work because the disk is full, and the buffer contents cannot be saved in any other way.

[line] p[rint][ n]
[range] p[rint]

(print) Prints the specified lines, with non-printing characters printed as control characters in the form ^x; DEL is represented as ^?. The last line printed becomes the current line.

[line] pu[t][ buffer]

(put) Lines deleted with d (delete) or copied to a buffer with y (yank) are put back in after the specified line. buffer represents a buffer name from a to z.

buffer not specified:
The text in the unnamed buffer is retrieved.

q[uit][!]

(quit) Quits the editor. If the buffer has been modified since the last w (write) command, a warning is printed and the q command fails. You can, however, force the execution of the q command by appending an exclamation mark to the q: q!. All changes not saved with w will then be discarded.

[line] r[ead][!][ file]

(read) Places a copy of the specified file in the buffer after the target line. Use a value of 0 to place text at the beginning of the buffer. If there is no current file at this point (e.g. when ex is called without a file name), then file becomes the current file. The last line read becomes the current line; in vi mode, the first line read becomes the current line.

file not specified:
The current file is read.

If file is given as !cmd, then cmd is interpreted as a POSIX command and passed to the shell. The output of the command is then read in to the buffer.

rec[over] file

Recovers file from the save area after an editor or system crash.

rew[ind][!]

(rewind) The argument list (see ar) is rewound, and the first file in the list is edited. If the buffer has been modified since the last w (write) command, a warning is printed and the rew command is not executed. You can, however, force the execution of the rew command by appending an exclamation mark to the rew command: rew!. All changes not saved with w are then discarded.

se[t][ parameter]

(set) The set command can be used to display or set options. There are two types of options: those that have "Boolean" (i.e. on or off) values and those that do not. One example of a Boolean parameter is the number option. If this option is set, line numbers are displayed; when nonumber is set, no line numbers are displayed. By contrast, the report option is non-Boolean. The value of this option (default = 5) defines the number of lines that must be changed by a command before ex and vi issue a message.

parameter

all

The values of all options are displayed.

option?

The current value of the specified option is displayed.

option

Only for options with Boolean values: The option is set.

nooption

Only for options with Boolean values: The option is not set.

option = value

Only for options with non-Boolean values: The option is assigned the specified value.


parameter not specified:
set displays user-defined ex options, i.e. options that deviate from default settings.

More detailed information on ex options is provided in the corresponding section.

sh[ell]

If the SHELL variable is set, the shell defined in that variable is invoked; otherwise, the Bourne shell sh. When you exit the shell, you will be returned to the editing session at the point you left off.

so[urce] file

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

[line] s[ubstitute][/RE/repl/[options][ n][ flags]]
[range] s[ubstitute][/RE/repl/[options][ flags]]

(substitute) The first occurrence of a pattern that matches RE (regular expression) in each line of the given range is replaced by the string repl (see “Regular expressions” and “Replacement strings”). The range is either given in range or specified as n lines from the specified line.

/RE/repl not specified:
The /RE/repl from the previous substitute command will be used.


options


g (global) All strings that match RE in the line are substituted.
c (confirm) Before each substitution, the line is output with the pattern to be replaced shown in the line below it, marked by a ^ character. Entering y causes the substitution to be made; any other input causes the command to abort. The last line that contains a substitution becomes the current line.


una[bbrev] word

(unabbreviate) word is deleted from the list of abbreviations set up by ab.

u[ndo]

(undo) Reverses the changes made by the previous editing command. g (global) and vi (visual) are considered single commands in this case. Commands which affect the external environment, such as w (write), e (edit), and n (next), cannot be undone.

An undo can itself be reversed with a follow-up u.

All markers for lines changed and subsequently restored are lost with u.

unm[ap][!] x

(unmap) Removes the macro definition created by map for x.

[range] v[ice]/RE/cmds

(vice versa) This is the same as the global command, except that cmds are applied to all lines that do not match the pattern RE.

ve[rsion]

Prints the current version of the editor.

[line] vi[sual][ type][ n]

Enters vi mode at the specified line. The type is optional and may be given as a minus sign (-) or a period (.), as in the z command, to specify the position of the specified line on the screen window. The default is to place the line at the top of the screen window. n specifies an initial window size; the default is the value of the ex option window. The command Q exits vi mode and returns you to ex. For further information, see vi on "vi screen oriented (visual) display editor".

[range ]w[rite][!][ file]   (Format1)
[range ]wq[!][ file]        (Format 2)

Format 1: Write to a file

(write) The specified range is written to the named file, and the number of lines and characters written is printed.

If an alternate file is specified, and the file exists, the command will fail in order to prevent the alternate file from being accidentally overwritten. The execution of w can, however, be forced by appending an exclamation mark to the command: w!.

To append to the file, use the form w>>. If the file does not exist, an error message is issued.

If !cmd is specified instead of file, then cmd is interpreted as a POSIX command. The command interpreter is invoked, and the specified range is passed as standard input to the command.

range not specified:
The entire current file is written to file.

file not specified:
The current file is used by default. The w command cannot be executed if there is no current file and no file is specified.

Format 2: Write to a file and quit ex

(write and quit) The command wq is equivalent to a w followed by a q; wq! is equivalent to w! followed by q.

x[it][!]

(exit) Writes out the buffer if changes have been made since the last w and then (in any case) quits.

line ya[nk][ buffer][ n]
range ya[nk][ buffer]

Copies (yanks) the specified range, or n lines from the specified line, to the named buffer.

buffer not specified:
If no buffer is specified, the unnamed buffer is used.

[line] z[ type][ n]

(window) Displays n lines from the area in which line is located.

n not specified:
n defaults to the value of the window variable.


type

type is an optional parameter and can be specified as follows:


- A - causes the line to be placed at the bottom of the screen.

+

A + causes the line to be placed at the top of the screen.

.A . causes the line to be placed at the bottom of the screen.

^

A ^ writes out n lines starting n*2 lines before the addressed line. The net effect of this will be that a z^ command following another z command writes the previous page.

=

A =centres the addressed line on the screen with a line of hyphens written immediately before and after it. The number of preceding and following lines of text written will be reduced to account for these lines of hyphens.


The last line printed becomes the current line.

type not specified:
line is placed at the top of the displayed area.

Caution: ex prints the number of logical rather than physical lines. More than a screen full of output may result if there are lines which are longer than the screen width.

!command

(escape) The remainder of the line after the exclamation mark is passed to the system command interpreter for execution. A warning is issued if the buffer has been changed since the last w (write) command. The specified command is then executed. If the command output has not been redirected, it is displayed on the screen, but does not change the file. The command itself may, however, modify the file, e.g.
!cat /etc/profile>> %.
A single ! is printed when the command completes. The current line position is not affected.

Within the text of command, the percent sign % and the hash character # are expanded as file names (see “Current and alternate file” ).

An exclamation mark in command is replaced with the text of the previous ! command. Thus, !! means "repeat the preceding command". If any such expansion is done, the expanded line will be echoed.

[range]!command

(escape) In this form of the ! command, the lines specified in range are passed to the command as standard input and replaced by the output of the command.

range not specified:
range is not replaced by the current line (.,.); the other form of the ! command applies.

"  Command lines that start with double quotes " are ignored. This means that you can use lines of this kind to insert comment lines in a command script.

line &options n flags
range&options flags
line s[ubstitute]options n flags
ranges[ubstitute]options flags

(resubst) Repeats the previous s (substitute) command, as if & were replaced by the previous s/RE/repl/. The same effect is obtained by omitting the /RE/repl/ string in an s command.

line < n
range <

(lshift) Shifts the defined line range (or the n lines that follow line) to the left by the number of spaces specified by the shiftwidth option. White space (blanks and tabs) is lost in the shift process; other characters are not affected. The last line changed becomes the current line.

line > n
range >

(rshift) Shifts the defined line range (or the n lines that follow line) to the right by the number of spaces specified by the shiftwidth option. White space (blanks and tabs) is not lost, but inserted as required.

line=

(line number) Prints the line number of the specified line. The current line position is not affected.

line not specified:
The line number of the last line is displayed.

ex options

ex provides a number of options with which you can govern the behavior of both the ex and (more importantly) the vi editors (see vi, “Customizing the vi environment” (vi screen oriented (visual) display editor)).

All options have default settings; these settings are valid at the time ex or vi is invoked. Use the ex command se (set) if you wish to have all options displayed:
set all 

The above command can also be used to change one or more options:
set showmode scroll=15 

This command shows the vi input mode in the status line and sets the number of lines scrolled by the CTRL+D key to 15.

The command:
set 

displays all options set to non-default values.

There are two types of options: those that have "Boolean" (i.e. on or off) values and those that do not. One example of a Boolean parameter is the showmode option. When this option is not set, it is named noshowmode. The scroll option, by contrast, is an example of a non-Boolean parameter.


autoindent, ai
noautoindent, noai (default)

If autoindent is set, each line in insert mode is aligned with the beginning of the previous line; tabs and blanks are inserted to produce this alignment. The starting indentation of a line is determined by the line it is appended after (a), the line it is inserted before (i), or the first line to be changed (c). Extra indentation can be provided as usual; succeeding lines will automatically be indented to the new alignment.

To reduce the level of indentation, you can press CTRL+D one or more times to move the start of the line to the left to positions which are multiples of shiftwidth. Thus if the current cursor position is 25 and shiftwidth is set to 10, pressing CTRL+ once will move the cursor to the left to position 20, while pressing it twice will take the cursor to position 10. A caret ^followed by a CTRL+D removes all indetation temporarily for the current line; a 0 (zero) followed by a CTRL+D removes all indentation.

autoprint, ap
noautoprint, noap (default)

Prints the current line after each command that changes buffer text. autoprint is suppressed during global search and replace operations (see g, s and v).

autowrite, aw
noautowrite, noaw (default)

Writes the buffer to the current file if the buffer has been modified, and an e (edit),n (next), rew (rewind) or ! (escape) command has been given.

beautify, bf
nobeautify, nobf (default)

Causes all control characters other than tab, newline and form feed to be discarded from the input text.

directory=dirdir=dir

The value of dir specifies the directory which is to hold the editor buffer. If the user does not have write permission for this directory, the editor quits.

edcompatible, ed
noedcompatible, noed

Causes the presence of g and c suffixes on s (substitute) commands to be remembered for use as toggles.

errorbells, eb
noerrorbells, noeb

Rings the terminal bell before displaying error messages in the last line (usually in reverse video).

exrc, ex

This option has been deactivated for security reasons. It normally causes .exrc files in the current directory to be evaluated.

flash, fl (default)
noflash, nofl

If the terminfo description contains an entry for flashing the screen to indicate an error, this option can be used to inform users when they have made an invalid entry.

hardtabs=numberht=number

If the terminal has hardware tabs, this option can be used to set the number of spaces to which the tab stops are set (to optimize screen output).

ignorecase, ic
noignorecase, noic (default)

Treats all uppercase characters in the text as lowercase when a search is made. Uppercase characters in regular expressions are also mapped to lowercase, except in character class expressions.

lisp
nolisp (default)

Activates the autoindent option and modifies the vi commands left parenthesis (, right parenthesis ), left brace {, right brace }, double left brackets [[, and double right brackets ]] suitably.

list
nolist (default)

All printed lines will be displayed with tabs shown as ^I, and the end of line marked by a $.

magic (default)
nomagic

Changes interpretation of characters in regular expressions and substitution replacement strings (see the relevant sections).

mesg (default)
nomesg

Grants/denies other users permission to write to the terminal (e.g. with the write command).

modelines (default)
nomodelines

The first and last five lines of a file that is read in are interpreted as editor commands and executed if they are in the form:
ex: command :

novice
nonovice (default)

Defines the level of detail for error messages.

number, nu
nonumber, nonu (default)

Causes lines to be printed with line numbers.

optimize, opt
nooptimize, noopt (default)

Allows you to set the terminal with or without automatic carriage returns (to optimize output).

paragraphs=string
para=string

The value of this option is a string in which each successive pair of characters specifies the name of an nroff macro that begins a paragraph. A macro appears in the text in the form .XX, where the . is the first character in the line.

prompt (default)
noprompt

When prompt is set, the : prompt is displayed in command mode; otherwise, there is no command-mode prompt.

readonly
noreadonly (default)

If readonly is set, the editor can only be used to read files, not to make changes to the text. If noreadonly is set, you can make changes to files.

redraw (default)
noredraw

The editor simulates an intelligent terminal on a dumb terminal. Since this is likely to require a large amount of output to the terminal, it is useful only at high transmission speeds.

remap (default)
noremap

If remap is set, macro translation allows for macros defined in terms of other macros; translation continues until the final product is obtained. Only one step of translation is performed if noremap is set.

report=n

When the number of lines changed, deleted, or copied by the last command exceeds n, a message is issued in the status line.

scroll=n

The value n specified for scroll determines the number of lines that the screen will scroll when a CTRL+D command is entered. It also controls the number of lines displayed by a z command (twice the value of scroll).

sections=string
sect=string

The value assigned to this option is a string in which each successive pair of characters specifies the name of an nroff macro which begins a section. A macro appears in the text in the form .XX, where the . is the first character in the line.

shiftwidth=n
sw=n

The value n specified for sw is used for the spacing of tab stops used by the tab key, the autoindent option, and by the < and > (shift) commands.

showmatch, sm
noshowmatch, nosm (default)

When a right parenthesis ) or right brace } is entered in vi, the matching left parenthesis ( or left brace { is shown, provided the matching character is still on the screen.

showmode, smd
noshowmode, nosmd (default)

When smd is set, an indication is provided in the status line as to whether vi is in vi input mode.

slowopen, slow
noslowopen, noslow (default)

In vi, prevents screen updates during input to improve throughput on unintelligent terminals.

tabstop=n
ts=n

n specifies the software tab spacing to be used by the editor when expanding tabs in the input file.

taglength=number
tl=number

Defines how many characters are to be considered significant in searches with the :tag command. A value of 0 means that all characters are to be considered.

tags=files

Names of tag files, separated by blanks.

term=string

string tells vi what type of terminal is being used. (The default is the value of the environment variable TERM.)

terse
noterse (default)

Setting terse causes shorter error messages to be issued.

timeout
notimeout

Sets/cancels a timeout period.

ttytype=string

string tells vi what type of terminal is being used. (The default is the value of the environment variable TERM.)

warn (default)
nowarn

When warn is set, the warning No write since last change is issued if a :! command is given before the most recent changes to the editor buffer have been saved with w.

window=n

The number of lines in a vi text window.

wrapmargin=n
wm=n

Only effective in vi. If n>0, a newline is automatically added to an input line at a word boundary such that lines end at least n spaces from the right margin of the terminal screen.

The setting of automatic line breaks can be deactivated by setting the value of n to 0 (default).

wrapscan, ws (default)
nowrapscan, nows

Setting wrapscan causes searches begun by the /.../ or ?...? commands to wrap around from the end of a buffer to the beginning (or vice-versa). When nows is set, the search runs through to the end or beginning of the buffer as appropriate and then terminates.

writeany, wa
nowriteany, nowa (default)

When nowa is set, a check is performed before a w command to determine whether the file exists. This is to prevent an existing file from being accidentally overwritten. You can override the overwrite protection with w!. Setting wa inhibits the normally provided overwrite protection.

Error

When an error occurs, ex sends the acoustic signal BEL (see section “ASCII character set (ISO 646)”) to the terminal and prints a message. If an interrupt signal is received, ex returns to command level, allowing you to enter a new ex command. If the editor input is from a file, ex exits at the interrupt signal.

If ex detects an internal error, it attempts to preserve the buffer if the most recent changes have not been saved. If you wish to access the backed up data, you must call ex with the-r option.

File

$HOME/.exrc

File containing default values for ex and vi. These defaults are overridden if conflicting settings have been assigned to EXINIT.

Variable

TERM

The type of data terminal used must be defined in the environment variable TERM.

EXINIT

Environment variable with default values for ex and vi. These defaults always apply.

PATH

Determine the search path for the shell command specified in the editor commands shell, read and write.

HOME

Determine a pathname of a directory that will be searched for an editor startup file named .exrc.

SHELL

Determine the preferred command-line interpreter for use in !, shell, read and other commands with an operand of the form !string. For the shell command, the program will be invoked with the single argument -i, for all others it will be invoked with the two arguments -c and string. If no SHELL variable is set, or it is set to a null string, the sh utility will be used.

COLUMNS

Override the system-selected horizontal screen size.

LINES

Override the system-selected vertical screen size, used as the number of lines in a screenful and the vertical size in visual mode.

Locale

The following environment variables affect the execution of ex:

LANG

Provide a default value for the internationalization variables that are unset or null. If LANG is unset of null, the corresponding value from the implementation-specific default locale will be used. If any of the internationalization variables contains an invalid setting, the utility will behave as if none of the variables had been defined.

LC_ALL

If set to a non-empty string value, override the values of all the other internationalization variables.

LC_COLLATE

Determine the locale for the behavior of ranges, equivalence classes and multicharacter collating elements within regular expressions.

LC_CTYPE

Determine the locale for the interpretation of sequences of bytes of text data as characters (for example, single- as opposed to multi-byte characters in arguments and input files), the classification of characters as upper- to lower-case, and the mapping of characters from one case to the other.

LC_MESSAGES

Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error.

NLSPATH

Determine the location of message catalogs for the processing of LC_MESSAGES.

See also

ed, vi