ed (1)
PROLOG
This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (consult the corresponding Linux manual page for details of Linux behavior), or the interface may not be implemented on Linux.NAME
ed --- edit textSYNOPSIS
ed [-p string] [-s] [file]
DESCRIPTION
The ed utility is a line-oriented text editor that uses two modes: command mode and input mode. In command mode the input characters shall be interpreted as commands, and in input mode they shall be interpreted as text. See the EXTENDED DESCRIPTION section. If an operand is '-', the results are unspecified.OPTIONS
The ed utility shall conform to the Base Definitions volume of POSIX.1-2008, Section 12.2, Utility Syntax Guidelines, except for the unspecified usage of '-'. The following options shall be supported:- -p string
- Use string as the prompt string when in command mode. By default, there shall be no prompt string.
- -s
- Suppress the writing of byte counts by e, E, r, and w commands and of the '!' prompt after a !command.
OPERANDS
The following operand shall be supported:- file
- If the file argument is given, ed shall simulate an e command on the file named by the pathname, file, before accepting commands from the standard input.
STDIN
The standard input shall be a text file consisting of commands, as described in the EXTENDED DESCRIPTION section.INPUT FILES
The input files shall be text files.ENVIRONMENT VARIABLES
The following environment variables shall affect the execution of ed:- HOME
- Determine the pathname of the user's home directory.
- LANG
- Provide a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1-2008, Section 8.2, Internationalization Variables for the precedence of internationalization variables used to determine the values of locale categories.)
- 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 multi-character 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-byte as opposed to multi-byte characters in arguments and input files) and the behavior of character classes within regular expressions.
- LC_MESSAGES
-
Determine the locale that should be used to affect the format and contents of diagnostic messages written to standard error and informative messages written to standard output. - NLSPATH
- Determine the location of message catalogs for the processing of LC_MESSAGES.
ASYNCHRONOUS EVENTS
The ed utility shall take the standard action for all signals (see the ASYNCHRONOUS EVENTS section in Section 1.4, Utility Description Defaults) with the following exceptions:- SIGINT
- The ed utility shall interrupt its current activity, write the string dq?\ndq to standard output, and return to command mode (see the EXTENDED DESCRIPTION section).
- SIGHUP
- If the buffer is not empty and has changed since the last write, the ed utility shall attempt to write a copy of the buffer in a file. First, the file named ed.hup in the current directory shall be used; if that fails, the file named ed.hup in the directory named by the HOME environment variable shall be used. In any case, the ed utility shall exit without writing the file to the currently remembered pathname and without returning to command mode.
- SIGQUIT
- The ed utility shall ignore this event.
STDOUT
Various editing commands and the prompting feature (see -p) write to standard output, as described in the EXTENDED DESCRIPTION section.STDERR
The standard error shall be used only for diagnostic messages.OUTPUT FILES
The output files shall be text files whose formats are dependent on the editing commands given.EXTENDED DESCRIPTION
The ed utility shall operate on a copy of the file it is editing; changes made to the copy shall have no effect on the file until a w (write) command is given. The copy of the text is called the buffer. Commands to ed have a simple and regular structure: zero, one, or two addresses followed by a single-character command, possibly followed by parameters to that command. These addresses specify one or more lines in the buffer. Every command that requires addresses has default addresses, so that the addresses very often can be omitted. If the -p option is specified, the prompt string shall be written to standard output before each command is read. In general, only one command can appear on a line. Certain commands allow text to be input. This text is placed in the appropriate place in the buffer. While ed is accepting text, it is said to be in input mode. In this mode, no commands shall be recognized; all input is merely collected. Input mode is terminated by entering a line consisting of two characters: a <period> ('.') followed by a <newline>. This line is not considered part of the input text.Regular Expressions in ed
The ed utility shall support basic regular expressions, as described in the Base Definitions volume of POSIX.1-2008, Section 9.3, Basic Regular Expressions. Since regular expressions in ed are always matched against single lines (excluding the terminating <newline> characters), never against any larger section of text, there is no way for a regular expression to match a <newline>. A null RE shall be equivalent to the last RE encountered. Regular expressions are used in addresses to specify lines, and in some commands (for example, the s substitute command) to specify portions of a line to be substituted.Addresses in ed
Addressing in ed relates to the current line. Generally, the current line is the last line affected by a command. The current line number is the address of the current line. If the edit buffer is not empty, the initial value for the current line shall be the last line in the edit buffer; otherwise, zero. Addresses shall be constructed as follows:- 1.
- The <period> character ('.') shall address the current line.
- 2.
- The <dollar-sign> character ('$') shall address the last line of the edit buffer.
- 3.
- The positive decimal number n shall address the nth line of the edit buffer.
- 4.
- The <apostrophe>-x character pair (dq'xdq) shall address the line marked with the mark name character x, which shall be a lowercase letter from the portable character set. It shall be an error if the character has not been set to mark a line or if the line that was marked is not currently present in the edit buffer.
- 5.
- A BRE enclosed by <slash> characters ('/') shall address the first line found by searching forwards from the line following the current line toward the end of the edit buffer and stopping at the first line for which the line excluding the terminating <newline> matches the BRE. The BRE consisting of a null BRE delimited by a pair of <slash> characters shall address the next line for which the line excluding the terminating <newline> matches the last BRE encountered. In addition, the second <slash> can be omitted at the end of a command line. Within the BRE, a <backslash>-<slash> pair (dq\/dq) shall represent a literal <slash> instead of the BRE delimiter. If necessary, the search shall wrap around to the beginning of the buffer and continue up to and including the current line, so that the entire buffer is searched.
- 6.
- A BRE enclosed by <question-mark> characters ('?') shall address the first line found by searching backwards from the line preceding the current line toward the beginning of the edit buffer and stopping at the first line for which the line excluding the terminating <newline> matches the BRE. The BRE consisting of a null BRE delimited by a pair of <question-mark> characters (dq??dq) shall address the previous line for which the line excluding the terminating <newline> matches the last BRE encountered. In addition, the second <question-mark> can be omitted at the end of a command line. Within the BRE, a <backslash>-<question-mark> pair (dq\?dq) shall represent a literal <question-mark> instead of the BRE delimiter. If necessary, the search shall wrap around to the end of the buffer and continue up to and including the current line, so that the entire buffer is searched.
- 7.
- A <plus-sign> ('+') or <hyphen> character ('-') followed by a decimal number shall address the current line plus or minus the number. A <plus-sign> or <hyphen> character not followed by a decimal number shall address the current line plus or minus 1. Addresses can be followed by zero or more address offsets, optionally <blank>-separated. Address offsets are constructed as follows:
- *
- A <plus-sign> or <hyphen> character followed by a decimal number shall add or subtract, respectively, the indicated number of lines to or from the address. A <plus-sign> or <hyphen> character not followed by a decimal number shall add or subtract 1 to or from the address.
- *
-
A decimal number shall add the indicated number of lines to the
address.
It shall not be an error for an intermediate address value to be less
than zero or greater than the last line in the edit buffer. It shall be
an error for the final address value to be less than zero or greater
than the last line in the edit buffer. It shall be an error if a search
for a BRE fails to find a matching line.
Commands accept zero, one, or two addresses. If more than the required
number of addresses are provided to a command that requires zero
addresses, it shall be an error. Otherwise, if more than the required
number of addresses are provided to a command, the addresses specified
first shall be evaluated and then discarded until the maximum number of
valid addresses remain, for the specified command.
Addresses shall be separated from each other by a
<comma>
(',')
or
<semicolon>
character (';').
In the case of a
<semicolon>
separator, the current line ('.')
shall be set to the first address, and only then will the second
address be calculated. This feature can be used to determine the
starting line for forwards and backwards searches; see rules 5. and
6.
Addresses can be omitted on either side of the
<comma>
or
<semicolon>
separator, in which case the resulting address pairs shall be as
follows:
Any <blank> characters included between addresses, address separators, or address offsets shall be ignored.
; addr . ; addr addr ; addr ; addr
Commands in ed
In the following list of ed commands, the default addresses are shown in parentheses. The number of addresses shown in the default shall be the number expected by the command. The parentheses are not part of the address; they show that the given addresses are the default. It is generally invalid for more than one command to appear on a line. However, any command (except e, E, f, q, Q, r, w, and !) can be suffixed by the letter l, n, or p; in which case, except for the l, n, and p commands, the command shall be executed and then the new current line shall be written as described below under the l, n, and p commands. When an l, n, or p suffix is used with an l, n, or p command, the command shall write to standard output as described below, but it is unspecified whether the suffix writes the current line again in the requested format or whether the suffix has no effect. For example, the pl command (base p command with an l suffix) shall either write just the current line or write it twice---once as specified for p and once as specified for l. Also, the g, G, v, and V commands shall take a command as a parameter. Each address component can be preceded by zero or more <blank> characters. The command letter can be preceded by zero or more <blank> characters. If a suffix letter (l, n, or p) is given, the application shall ensure that it immediately follows the command. The e, E, f, r, and w commands shall take an optional file parameter, separated from the command letter by one or more <blank> characters. If changes have been made in the buffer since the last w command that wrote the entire buffer, ed shall warn the user if an attempt is made to destroy the editor buffer via the e or q commands. The ed utility shall write the string:
-
"?\n"
- *
- If accompanied by a SIGHUP signal, the ed utility shall operate as described in the ASYNCHRONOUS EVENTS section for a SIGHUP signal.
- *
- If not accompanied by a SIGHUP signal, the ed utility shall act as if an end-of-file had been detected on standard input. If an end-of-file is detected on standard input:
- *
- If the ed utility is in input mode, ed shall terminate input mode and return to command mode. It is unspecified if any partially entered lines (that is, input text without a terminating <newline>) are discarded from the input text.
- *
-
If the
ed
utility is in command mode, it shall act as if a
q
command had been entered.
If the closing delimiter of an RE or of a replacement string (for
example,
'/')
in a
g,
G,
s,
v,
or
V
command would be the last character before a
<newline>,
that delimiter can be omitted, in which case the addressed line shall
be written. For example, the following pairs of commands are
equivalent:
-
s/s1/s2 s/s1/s2/p g/s1 g/s1/p ?s1 ?s1?
-
"?\n"
-
Append Command
- Synopsis:
-
-
-
(.)a <text> .
-
-
Change Command
- Synopsis:
-
-
-
(.,.)c <text> .
-
-
Delete Command
- Synopsis:
-
-
-
(.,.)d
-
-
Edit Command
- Synopsis:
-
-
-
e [file]
-
-
"%d\n", <number of bytes read>
-
Edit Without Checking Command
- Synopsis:
-
-
-
E [file]
-
-
Filename Command
- Synopsis:
-
-
-
f [file]
-
-
"%s\n", <pathname>
-
Global Command
- Synopsis:
-
-
-
(1,$)g/RE/command list
-
-
Interactive Global Command
- Synopsis:
-
-
-
(1,$)G/RE/
-
-
Help Command
- Synopsis:
-
-
-
h
-
-
Help-Mode Command
- Synopsis:
-
-
-
H
-
-
Insert Command
- Synopsis:
-
-
-
(.)i <text> .
-
-
Join Command
- Synopsis:
-
-
-
(.,.+1)j
-
-
Mark Command
- Synopsis:
-
-
-
(.)kx
-
-
List Command
- Synopsis:
-
-
-
(.,.)l
-
-
Move Command
- Synopsis:
-
-
-
(.,.)maddress
-
-
Number Command
- Synopsis:
-
-
-
(.,.)n
-
-
Print Command
- Synopsis:
-
-
-
(.,.)p
-
-
Prompt Command
- Synopsis:
-
-
-
P
-
-
Quit Command
- Synopsis:
-
-
-
q
-
-
Quit Without Checking Command
- Synopsis:
-
-
-
Q
-
-
Read Command
- Synopsis:
-
-
-
($)r [file]
-
-
"%d\n", <number of bytes read>
-
Substitute Command
- Synopsis:
-
-
-
(.,.)s/RE/replacement/flags
-
-
- count
- Substitute for the countth occurrence only of the RE found on each addressed line.
- g
- Globally substitute for all non-overlapping instances of the RE rather than just the first one. If both g and count are specified, the results are unspecified.
- l
- Write to standard output the final line in which a substitution was made. The line shall be written in the format specified for the l command.
- n
- Write to standard output the final line in which a substitution was made. The line shall be written in the format specified for the n command.
- p
- Write to standard output the final line in which a substitution was made. The line shall be written in the format specified for the p command.
Copy Command
- Synopsis:
-
-
-
(.,.)taddress
-
-
Undo Command
- Synopsis:
-
-
-
u
-
-
Global Non-Matched Command
- Synopsis:
-
-
-
(1,$)v/RE/command list
-
-
Interactive Global Not-Matched Command
- Synopsis:
-
-
-
(1,$)V/RE/
-
-
Write Command
- Synopsis:
-
-
-
(1,$)w [file]
-
-
"%d\n", <number of bytes written>
-
Line Number Command
- Synopsis:
-
-
-
($)=
-
-
"%d\n", <line number>
-
Shell Escape Command
- Synopsis:
-
-
-
!command
-
-
"!\n"
-
Null Command
- Synopsis:
-
-
-
(.+1)
-
-
EXIT STATUS
The following exit values shall be returned:- 0
- Successful completion without any file or command errors.
- >0
- An error occurred.
CONSEQUENCES OF ERRORS
When an error in the input script is encountered, or when an error is detected that is a consequence of the data (not) present in the file or due to an external condition such as a read or write error:- *
- If the standard input is a terminal device file, all input shall be flushed, and a new command read.
- *
- If the standard input is a regular file, ed shall terminate with a non-zero exit status.
The following sections are informative.
APPLICATION USAGE
Because of the extremely terse nature of the default error messages, the prudent script writer begins the ed input commands with an H command, so that if any errors do occur at least some clue as to the cause is made available. In earlier versions of this standard, an obsolescent - option was described. This is no longer specified. Applications should use the -s option. Using - as a file operand now produces unspecified results. This allows implementations to continue to support the former required behavior.EXAMPLES
None.RATIONALE
The initial description of this utility was adapted from the SVID. It contains some features not found in Version 7 or BSD-derived systems. Some of the differences between the POSIX and BSD ed utilities include, but need not be limited to:- *
- The BSD - option does not suppress the '!' prompt after a ! command.
- *
- BSD does not support the special meanings of the '%' and '!' characters within a ! command.
- *
- BSD does not support the addresses ';' and ','.
- *
- BSD allows the command/suffix pairs pp, ll, and so on, which are unspecified in this volume of POSIX.1-2008.
- *
- BSD does not support the '!' character part of the e, r, or w commands.
- *
- A failed g command in BSD sets the line number to the last line searched if there are no matches.
- *
- BSD does not default the command list to the p command.
- *
- BSD does not support the G, h, H, n, or V commands.
- *
- On BSD, if there is no inserted text, the insert command changes the current line to the referenced line -1; that is, the line before the specified line.
- *
- On BSD, the join command with only a single address changes the current line to that address.
- *
- BSD does not support the P command; moreover, in BSD it is synonymous with the p command.
- *
- BSD does not support the undo of the commands j, m, r, s, or t.
- *
- The Version 7 ed command W, and the BSD ed commands W, wq, and z are not present in this volume of POSIX.1-2008. The -s option was added to allow the functionality of the removed - option in a manner compatible with the Utility Syntax Guidelines. In early proposals there was a limit, {ED_FILE_MAX}, that described the historical limitations of some ed utilities in their handling of large files; some of these have had problems with files larger than 100000 bytes. It was this limitation that prompted much of the desire to include a split command in this volume of POSIX.1-2008. Since this limit was removed, this volume of POSIX.1-2008 requires that implementations document the file size limits imposed by ed in the conformance document. The limit {ED_LINE_MAX} was also removed; therefore, the global limit {LINE_MAX} is used for input and output lines. The manner in which the l command writes non-printable characters was changed to avoid the historical backspace-overstrike method. On video display terminals, the overstrike is ambiguous because most terminals simply replace overstruck characters, making the l format not useful for its intended purpose of unambiguously understanding the content of the line. The historical <backslash>-escapes were also ambiguous. (The string dqa\0011dq could represent a line containing those six characters or a line containing the three characters 'a', a byte with a binary value of 1, and a 1.) In the format required here, a <backslash> appearing in the line is written as dq\\dq so that the output is truly unambiguous. The method of marking the ends of lines was adopted from the ex editor and is required for any line ending in <space> characters; the '$' is placed on all lines so that a real '$' at the end of a line cannot be misinterpreted. Earlier versions of this standard allowed for implementations with bytes other than eight bits, but this has been modified in this version. The description of how a NUL is written was removed. The NUL character cannot be in text files, and this volume of POSIX.1-2008 should not dictate behavior in the case of undefined, erroneous input. Unlike some of the other editing utilities, the filenames accepted by the E, e, R, and r commands are not patterns. Early proposals stated that the -p option worked only when standard input was associated with a terminal device. This has been changed to conform to historical implementations, thereby allowing applications to interpose themselves between a user and the ed utility. The form of the substitute command that uses the n suffix was limited in some historical documentation (where this was described incorrectly as ``backreferencing''). This limit has been omitted because there is no reason why an editor processing lines of {LINE_MAX} length should have this restriction. The command s/x/X/2047 should be able to substitute the 2047th occurrence of 'x' on a line. The use of printing commands with printing suffixes (such as pn, lp, and so on) was made unspecified because BSD-based systems allow this, whereas System V does not. Some BSD-based systems exit immediately upon receipt of end-of-file if all of the lines in the file have been deleted. Since this volume of POSIX.1-2008 refers to the q command in this instance, such behavior is not allowed. Some historical implementations returned exit status zero even if command errors had occurred; this is not allowed by this volume of POSIX.1-2008. Some historical implementations contained a bug that allowed a single <period> to be entered in input mode as <backslash> <period> <newline>. This is not allowed by ed because there is no description of escaping any of the characters in input mode; <backslash> characters are entered into the buffer exactly as typed. The typical method of entering a single <period> has been to precede it with another character and then use the substitute command to delete that character. It is difficult under some modes of some versions of historical operating system terminal drivers to distinguish between an end-of-file condition and terminal disconnect. POSIX.1-2008 does not require implementations to distinguish between the two situations, which permits historical implementations of the ed utility on historical platforms to conform. Implementations are encouraged to distinguish between the two, if possible, and take appropriate action on terminal disconnect. Historically, ed accepted a zero address for the a and r commands in order to insert text at the start of the edit buffer. When the buffer was empty the command .= returned zero. POSIX.1-2008 requires conformance to historical practice. For consistency with the a and r commands and better user functionality, the i and c commands must also accept an address of 0, in which case 0i is treated as 1i and likewise for the c command. All of the following are valid addresses:
- +++
- Three lines after the current line.
- /pattern/-
- One line before the next occurrence of pattern.
- -2
- Two lines before the current line.
- 3 ---- 2
- Line one (note the intermediate negative address).
- 1 2 3
-
Line six.
Any number of addresses can be provided to commands taking addresses;
for example,
dq1,2,3,4,5pdq
prints lines 4 and 5, because two is the greatest valid number of
addresses accepted by the
print
command. This, in combination with the
<semicolon>
delimiter, permits users to create commands based on ordered patterns
in the file. For example, the command
dq3;/foo/;+2pdq
will display the first line after line 3 that contains the pattern
foo,
plus the next two lines. Note that the address
dq3;dq
must still be evaluated before being discarded, because the search
origin for the
dq/foo/dq
command depends on this.
Historically,
ed
disallowed address chains, as discussed above, consisting solely of
<comma>
or
<semicolon>
separators; for example,
dq,,,dq
or
dq;;;dq
were considered an error. For consistency of address specification,
this restriction is removed. The following table lists some of the
address forms now possible:
Historically, ed accepted the '^' character as an address, in which case it was identical to the <hyphen> character. POSIX.1-2008 does not require or prohibit this behavior.
Address Addr1 Addr2 Status Comment 7, 7 7 Historical 7,5, 5 5 Historical 7,5,9 5 9 Historical 7,9 7 9 Historical 7,+ 7 8 Historical , 1 $ Historical ,7 1 7 Extension ,, $ $ Extension ,; $ $ Extension 7; 7 7 Historical 7;5; 5 5 Historical 7;5;9 5 9 Historical 7;5,9 5 9 Historical 7;$;4 $ 4 Historical Valid, but erroneous. 7;9 7 9 Historical 7;+ 7 8 Historical ; . $ Historical ;7 . 7 Extension ;; $ $ Extension ;, $ $ Extension
FUTURE DIRECTIONS
None.SEE ALSO
Section 1.4, Utility Description Defaults, ex, sed, sh, vi The Base Definitions volume of POSIX.1-2008, Table 5-1, Escape Sequences and Associated Actions, Chapter 8, Environment Variables, Section 9.3, Basic Regular Expressions, Chapter 11, General Terminal Interface, Section 12.2, Utility Syntax GuidelinesCOPYRIGHT
Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1, 2013 Edition, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, Copyright (C) 2013 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. (This is POSIX.1-2008 with the 2013 Technical Corrigendum 1 applied.) In the event of any discrepancy between this version and the original IEEE and The Open Group Standard, the original IEEE and The Open Group Standard is the referee document. The original Standard can be obtained online at www.unix.org/online.html .Any typographical or formatting errors that appear in this page are most likely to have been introduced during the conversion of the source files to man page format. To report such errors, see www.kernel.org/doc/man-pages/reporting_bugs.html .