ed: Commands
6 Commands
**********
All 'ed' commands are single characters, though some require additonal
parameters. If a command's parameters extend over several lines, then
each line except for the last must be terminated with a backslash ('\').
In general, at most one command is allowed per line. However, most
commands accept a print suffix, which is any of 'p' (print), 'l'
(list), or 'n' (enumerate), to print the last line affected by the
command. It is not portable to give more than one print suffix, but
'ed' allows any combination of non-repeated print suffixes and combines
their effects.
An interrupt (typically <Control-C>) has the effect of aborting the
current command and returning the editor to command mode.
'ed' recognizes the following commands. The commands are shown
together with the default address or address range supplied if none is
specified (in parenthesis).
'(.)a'
Appends text to the buffer after the addressed line. The address
'0' (zero) is valid for this command; it places the entered text at
the beginning of the buffer. Text is entered in input mode. The
current address is set to the address of the last line entered or,
if there were none, to the addressed line.
'(.,.)c'
Changes lines in the buffer. The addressed lines are deleted from
the buffer, and text is inserted in their place. Text is entered
in input mode. The current address is set to the address of the
last line entered or, if there were none, to the new address of
the line after the last line deleted; if the lines deleted were
originally at the end of the buffer, the current address is set to
the address of the new last line; if no lines remain in the
buffer, the current address is set to zero.
'(.,.)d'
Deletes the addressed lines from the buffer. The current address
is set to the new address of the line after the last line deleted;
if the lines deleted were originally at the end of the buffer, the
current address is set to the address of the new last line; if no
lines remain in the buffer, the current address is set to zero.
'e FILE'
Edits FILE, and sets the default filename. If FILE is not
specified, then the default filename is used. Any lines in the
buffer are deleted before the new file is read. The current
address is set to the address of the last line in the buffer.
If FILE is prefixed with a bang (!), then it is interpreted as a
shell command whose output is to be read, (shell escape
command '!' below). In this case the default filename is
unchanged.
A warning is printed if any changes have been made in the buffer
since the last 'w' command that wrote the entire buffer to a file.
'E FILE'
Edits FILE unconditionally. This is similar to the 'e' command,
except that unwritten changes are discarded without warning.
'f FILE'
Sets the default filename to FILE. If FILE is not specified, then
the default unescaped filename is printed.
'(1,$)g/RE/COMMAND-LIST'
Global command. The global command makes two passes over the file.
On the first pass, all the addressed lines matching a regular
expression RE are marked. Then, going sequentially from the
beginning of the file to the end of the file, the given
COMMAND-LIST is executed for each marked line, with the current
address set to the address of that line. Any line modified by the
COMMAND-LIST is unmarked. The final value of the current address
is the value assigned by the last command in the last COMMAND-LIST
executed. If there were no matching lines, the current address is
unchanged.
The first command of COMMAND-LIST must appear on the same line as
the 'g' command. All lines of a multi-line COMMAND-LIST except the
last line must be terminated with a backslash ('\'). Any commands
are allowed, except for 'g', 'G', 'v', and 'V'. The '.'
terminating the input mode of commands 'a', 'c', and 'i' can be
omitted if it would be the last line of COMMAND-LIST. By default,
a newline alone in COMMAND-LIST is equivalent to a 'p' command. If
'ed' is invoked with the command-line option '-G', then a newline
in COMMAND-LIST is equivalent to a '.+1p' command.
'(1,$)G/RE/'
Interactive global command. Interactively edits the addressed lines
matching a regular expression RE. For each matching line, the line
is printed, the current address is set, and the user is prompted to
enter a COMMAND-LIST. The final value of the current address is
the value assigned by the last command executed. If there were no
matching lines, the current address is unchanged.
The format of COMMAND-LIST is the same as that of the 'g' command.
A newline alone acts as a null command list. A single '&' repeats
the last non-null command list.
'h'
Prints an explanation of the last error.
'H'
Toggles the printing of error explanations. By default,
explanations are not printed. It is recommended that ed scripts
begin with this command to aid in debugging.
'(.)i'
Inserts text in the buffer before the addressed line. The address
'0' (zero) is valid for this command; it is equivalent to address
'1'. Text is entered in input mode. The current address is set to
the address of the last line entered or, if there were none, to the
addressed line.
'(.,.+1)j'
Joins the addressed lines, replacing them by a single line
containing their joined text. If only one address is given, this
command does nothing. If lines are joined, the current address is
set to the address of the joined line. Else, the current address
is unchanged.
'(.)kx'
Marks a line with a lower case letter 'x'. The line can then be
addressed as ''x' (i.e., a single quote followed by 'x') in
subsequent commands. The mark is not cleared until the line is
deleted or otherwise modified. The current address is unchanged.
'(.,.)l'
List command. Prints the addressed lines unambiguously. The end of
each line is marked with a '$', and every '$' character within the
text is printed with a preceding backslash. Special characters are
printed as escape sequences. The current address is set to the
address of the last line printed.
'(.,.)m(.)'
Moves lines in the buffer. The addressed lines are moved to after
the right-hand destination address. The destination address '0'
(zero) is valid for this command; it moves the addressed lines to
the beginning of the buffer. It is an error if the destination
address falls within the range of moved lines. The current address
is set to the new address of the last line moved.
'(.,.)n'
Number command. Prints the addressed lines, preceding each line by
its line number and a <tab>. The current address is set to the
address of the last line printed.
'(.,.)p'
Prints the addressed lines. The current address is set to the
address of the last line printed.
'P'
Toggles the command prompt on and off. Unless a prompt is
specified with command-line option '-p', the command prompt is by
default turned off.
'q'
Quits 'ed'. A warning is printed if any changes have been made in
the buffer since the last 'w' command that wrote the entire buffer
to a file.
'Q'
Quits 'ed' unconditionally. This is similar to the 'q' command,
except that unwritten changes are discarded without warning.
'($)r FILE'
Reads FILE and appends it after the addressed line. If FILE is not
specified, then the default filename is used. If there is no
default filename prior to the command, then the default filename
is set to FILE. Otherwise, the default filename is unchanged. The
address '0' (zero) is valid for this command; it reads the file at
the beginning of the buffer. The current address is set to the
address of the last line read or, if there were none, to the
addressed line.
If FILE is prefixed with a bang (!), then it is interpreted as a
shell command whose output is to be read, (shell escape
command '!' below). In this case the default filename is
unchanged.
'(.,.)s/RE/REPLACEMENT/'
Substitute command. Replaces text in the addressed lines matching a
regular expression RE with REPLACEMENT. By default, only the first
match in each line is replaced. The 's' command accepts any
combination of the suffixes 'g', 'COUNT', 'l', 'n', and 'p'. If
the 'g' (global) suffix is given, then every match is replaced.
The 'COUNT' suffix, where COUNT is a positive number, causes only
the COUNTth match to be replaced. 'g' and 'COUNT' can't be
specified in the same command. 'l', 'n', and 'p' are the usual
print suffixes. It is an error if no substitutions are performed
on any of the addressed lines. The current address is set to the
address of the last line on which a substitution occurred. If a
line is split, a substitution is considered to have occurred on
each of the new lines. If no substitution is performed, the
current address is unchanged.
RE and REPLACEMENT may be delimited by any character other than
<space>, <newline> and the characters used by the form of the 's'
command shown below. If the last delimiter is omitted, then the
last line affected is printed as if the print suffix 'p' were
specified. The last delimiter can't be omitted if the 's' command
is part of a 'g' or 'v' COMMAND-LIST and is not the last command
in the list, because the meaning of the following escaped newline
becomes ambiguous.
An unescaped '&' in REPLACEMENT is replaced by the currently
matched text. The character sequence '\M' where M is a number in
the range [1,9], is replaced by the Mth backreference expression
of the matched text. If the corresponding backreference expression
does not match, then the character sequence '\M' is replaced by
the empty string. If REPLACEMENT consists of a single '%', then
REPLACEMENT from the last substitution is used.
A line can be split by including a newline escaped with a backslash
('\') in REPLACEMENT, except if the 's' command is part of a 'g'
or 'v' COMMAND-LIST, because in this case the meaning of the
escaped newline becomes ambiguous. Each backslash in REPLACEMENT
removes the special meaning (if any) of the following character.
'(.,.)s'
Repeats the last substitution. This form of the 's' command accepts
the 'g' and 'COUNT' suffixes described above, and any combination
of the suffixes 'p' and 'r'. The 'g' suffix toggles the global
suffix of the last substitution and resets COUNT to 1. The 'p'
suffix toggles the print suffixes of the last substitution. The
'r' suffix causes the regular expression of the last search to be
used instead of that of the last substitution (if the search
happened after the substitution).
'(.,.)t(.)'
Copies (i.e., transfers) the addressed lines to after the
right-hand destination address. If the destination address is '0'
(zero), the lines are copied at the beginning of the buffer. The
current address is set to the address of the last line copied.
'u'
Undoes the effect of the last command that modified anything in the
buffer and restores the current address to what it was before the
command. The global commands 'g', 'G', 'v', and 'V' are treated as
a single command by undo. 'u' is its own inverse.
'(1,$)v/RE/COMMAND-LIST'
This is similar to the 'g' command except that it applies
COMMAND-LIST to each of the addressed lines not matching the
regular expression RE.
'(1,$)V/RE/'
This is similar to the 'G' command except that it interactively
edits the addressed lines not matching the regular expression RE.
'(1,$)w FILE'
Writes the addressed lines to FILE. Any previous contents of FILE
is lost without warning. If there is no default filename, then the
default filename is set to FILE, otherwise it is unchanged. If no
filename is specified, then the default filename is used. The
current address is unchanged.
If FILE is prefixed with a bang (!), then it is interpreted as a
shell command and the addressed lines are written to its standard
input, (shell escape command '!' below). In this case the
default filename is unchanged. Writing the buffer to a shell
command does not prevent the warning to the user if an attempt is
made to overwrite or discard the buffer via the 'e' or 'q'
commands.
'(1,$)wq FILE'
Writes the addressed lines to FILE, and then executes a 'q'
command.
'(1,$)W FILE'
Appends the addressed lines to the end of FILE. This is similar to
the 'w' command, except that the previous contents of file is not
clobbered. The current address is unchanged.
'(.)x'
Copies (puts) the contents of the cut buffer to after the addressed
line. The current address is set to the address of the last line
copied.
'(.,.)y'
Copies (yanks) the addressed lines to the cut buffer. The cut
buffer is overwritten by subsequent 'c', 'd', 'j', 's', or 'y'
commands. The current address is unchanged.
'(.+1)zN'
Scrolls N lines at a time starting at addressed line, and sets
window size to N. If N is not specified, then the current window
size is used. Window size defaults to screen size minus two lines,
or to 22 if screen size can't be determined. The current address
is set to the address of the last line printed.
'!COMMAND'
Shell escape command. Executes COMMAND via 'sh (1)'. If the first
character of COMMAND is '!', then it is replaced by the text of
the previous '!COMMAND'. Thus, '!!' repeats the previous
'!COMMAND'. 'ed' does not process COMMAND for backslash ('\')
escapes. However, an unescaped '%' is replaced by the default
filename. When the shell returns from execution, a '!' is printed
to the standard output. The current address is unchanged.
'(.,.)#'
Begins a comment; the rest of the line, up to a newline, is
ignored. If a line address followed by a semicolon is given, then
the current address is set to that address. Otherwise, the current
address is unchanged.
'($)='
Prints the line number of the addressed line. The current address
is unchanged.
'(.+1)<newline>'
Null command. An address alone prints the addressed line. A
<newline> alone is equivalent to '+1p'. The current address is set
to the address of the printed line.