ed: Commands
6 Commands
**********
All 'ed' commands are single characters, though some require additional
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. If
any suffix letter is given, it must immediately follow the command.
The 'e', 'E', 'f', 'r', and 'w' commands take an optional FILE
parameter, separated from the command letter by one or more whitespace
characters.
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. The lines deleted are copied to the cut buffer.
'(.,.)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. The lines deleted
are copied to the cut buffer.
'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/[I]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. The suffix 'I' is a GNU extension which makes 'ed'
match RE in a case-insensitive manner. 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 execution of COMMAND-LIST stops on the first error.
The first command of COMMAND-LIST must appear on the same line as the
'g' command. The other commands of COMMAND-LIST must appear on
separate lines. 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/[I]'
Interactive global command. Interactively edits the addressed lines
matching a regular expression RE. The suffix 'I' is a GNU extension
which makes 'ed' match RE in a case-insensitive manner. 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 an empty command list. A single '&' repeats the
last non-empty command list.
'h'
Help. 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 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.
'(.,.+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 lines replaced are copied to the cut
buffer and 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 lines to be moved. 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 string is
specified with the command-line option '-p', the command prompt is by
default turned off. The default prompt string is an asterisk ('*').
'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.
'(.,.)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; it can undo only the
last command.
'(1,$)v/RE/[I]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/[I]'
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 are
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 are 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'
Scroll. Prints 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, each
unescaped '%' is replaced with the default filename, and the backslash
is removed from each escaped '%'. 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.