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).
(.)aAppends 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.
(.,.)cChanges 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.
(.,.)dDeletes 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 fileEdits 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. If file does not exist, a warning is printed in place of a byte count and the resulting buffer is left empty. 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, (see shell escape command ‘!’ below). In this case the default filename is unchanged. To edit a file whose name begins with a bang, prefix the name with ./.
A warning is printed if the buffer modified flag is set. If another ‘e’ or ‘q’ command is given with no intervening error or buffer-modifying command, it is executed without warning, and any changes to the buffer are lost.
E fileEdits file unconditionally. This is similar to the ‘e’ command, except that unwritten changes are discarded without warning.
f fileSets the default filename to file, whether or not file names an existing file. If file is not specified, then the default filename is printed. Tilde expansion is performed on file; if file starts with ~/, the ~ is expanded to specify your home directory $HOME.
(1,$)g/re/[I]command-listGlobal 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.
hPrints a help message explaining the reason for the most recent ‘?’ notification.
HToggles the printing of help messages (see the ‘h’ command above). If the help mode is being turned on, also prints the help message corresponding to the most recent ‘?’ notification. By default, help messages are not printed.
(.)iInserts 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)jJoins 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.
(.)kxMarks the addressed 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.
(.,.)lList 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.
(.,.)nNumber 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.
(.,.)pPrints the addressed lines. The current address is set to the address of the last line printed.
PToggles 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 (‘*’).
qQuits ed. A warning is printed if the buffer modified flag is set.
If another ‘e’ or ‘q’ command is given with no intervening error
or buffer-modifying command, it is executed without warning, and any changes
to the buffer are lost.
QQuits ed unconditionally. This is similar to the ‘q’ command,
except that unwritten changes are discarded without warning.
($)r fileReads 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, (see shell escape command ‘!’ below). In this case the default filename is unchanged. To read a file whose name begins with a bang, prefix the name with ./.
(.,.)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.
uUndoes 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-listThis 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 fileWrites 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. If the whole buffer is written, then the buffer modified flag is cleared. 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, (see shell escape command ‘!’ below). In this case the default filename is unchanged. Writing the buffer to a shell command does not clear the buffer modified flag. To write to a file whose name begins with a bang, prefix the name with ./.
(1,$)wq fileWrites the addressed lines to file, and then executes a ‘q’ command.
(1,$)W fileAppends 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.
(.)xCopies (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.
(.,.)yCopies (yanks) the addressed lines to the cut buffer. The current address is unchanged. The cut buffer is overwritten by subsequent ‘c’, ‘d’, ‘j’, ‘s’, or ‘y’ commands. The cut buffer is deleted when a new file is read with ‘e’ or ‘E’.
(.+1)znScroll. 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 environment variable LINES can be used to set the initial window size. LINES and n are not limited by screen size. The current address is set to the address of the last line printed.
!command[2addr]!commandShell escape command. Executes command via sh. 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.
If one or more addresses are specified, the addressed lines are written to the standard input of command and then replaced with the lines read from the standard output and standard error of command. Two byte counts are printed; one for the bytes written to command and the other for the bytes read from command. The current address is set to the address of the last line read or, if there were none, to the line before the first addressed line.
(.,.)#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)newlineNull 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.