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