ED(1) | User Commands | ED(1) |
ed, red - text editor
/usr/bin/ed [-s | -] [-p string] [-x] [-C] [file]
/usr/xpg4/bin/ed [-s | -] [-p string] [-x] [-C] [file]
/usr/xpg6/bin/ed [-s | -] [-p string] [-x] [-C] [file]
/usr/bin/red [-s | -] [-p string] [-x] [-C] [file]
The ed utility is the standard text editor. If file is specified, ed simulates an e command (see below) on the named file. That is, the file is read into ed's buffer so that it can be edited.
The ed utility operates on a copy of the file it is editing. Changes made to the copy have no effect on the file until a w (write) command is given. The copy of the text being edited resides in a temporary file called the buffer. There is only one buffer.
The red utility is a restricted version of ed. It will only allow editing of files in the current directory. red prohibits executing shell commands via !shell command. Attempts to bypass these restrictions result in an error message (restricted shell).
Both ed and red support the fspec(5) formatting capability. The default terminal mode is either stty -tabs or stty tab3, where tab stops are set at eight columns (see stty(1)). If, however, the first line of file contains a format specification, that specification will override the default mode. For example, tab stops would be set at 5, 10, and 15, and a maximum line length of 72 would be imposed if the first line of file contains
<:t5,10,15 s72:>
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 can very often be omitted.
In general, only one command may appear on a line. Certain commands allow the input of text. 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 are recognized; all input is merely collected. Leave input mode by typing a period (.) at the beginning of a line, followed immediately by a carriage return.
If ed executes commands with arguments, it uses the default shell /usr/bin/sh (see sh(1)).
The ed utility supports a limited form of regular expression notation. Regular expressions are used in addresses to specify lines and in some commands (for example, s) to specify portions of a line that are to be substituted. To understand addressing in ed, it is necessary to know that at any time there is a current line. Generally speaking, the current line is the last line affected by a command. The exact effect on the current line is discussed under the description of each command.
Internationalized Basic Regular Expressions are used for all system-supplied locales. See regex(7).
Commands may require zero, one, or two addresses. Commands that require no addresses regard the presence of an address as an error. Commands that accept one or two addresses assume default addresses when an insufficient number of addresses is given; if more addresses are given than such a command requires, the last one(s) are used.
Typically, addresses are separated from each other by a comma (,). They may also be separated by a semicolon (;). In the latter case, the first address is calculated, the current line (.) is set to that value, and then the second address is calculated. This feature can be used to determine the starting line for forward and backward searches (see Rules 5 and 6, above). The second address of any two-address sequence must correspond to a line in the buffer that follows the line corresponding to the first address.
For /usr/xpg6/bin/ed, the address can be omitted on either side of the comma or semicolon separator, in which case the resulting address pairs are as follows:
Specified | Resulting |
, | 1 , $ |
, addr | 1 , addr |
addr , | addr , addr |
; | 1 ; $ |
; addr | 1 ; addr |
addr ; | addr ; addr |
Any <blank>s included between addresses, address separators, or address offsets are ignored.
In the following list of ed commands, the parentheses shown prior to the command are not part of the address. Rather, the parentheses show the default address(es) for the command.
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, 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 blank characters.
If changes have been made in the buffer since the last w command that wrote the entire buffer, ed warns the user if an attempt is made to destroy the editor buffer via the e or q commands. The ed utility writes the string:
"?\n"
(followed by an explanatory message if help mode has been enabled via the H command) to standard output and continues in command mode with the current line number unchanged. If the e or q command is repeated with no intervening command, ed takes effect.
If an end-of-file is detected on standard input when a command is expected, the ed utility acts as if a q command had been entered.
It is generally illegal for more than one command to appear on a line. However, any command (except e, f, r, or w) may be suffixed by l, n, or p in which case the current line is either listed, numbered or written, respectively, as discussed below under the l, n, and p commands.
(.)a
<text>
.
(.,.)c
<text>
.
/usr/xpg4/bin/ed
/usr/xpg6/bin/ed
C
(.,.)d
e file
"%d\n" <number of bytes read>
file is remembered for possible use as a default file name in subsequent e, E, r, and w commands. If file is replaced by !, the rest of the line is taken to be a shell ( sh(1)) command whose output is to be read. Such a shell command is not remembered as the current file name. See also DIAGNOSTICS below. All marks are discarded upon the completion of a successful e command. If the buffer has changed since the last time the entire buffer was written, the user is warned, as described previously.
E file
f file
"%s\n"pathname
The current line number is unchanged.
(1,$)g/RE/command list
(1,$)G/RE/
h
H
(.,.)i
<text>
.
/usr/xpg4/bin/ed
/usr/xpg6/bin/ed
(.,.+1)j
(.)kx
(.,.)l
Long lines are folded, with the point of folding indicated by writing backslash/newline character. The length at which folding occurs is unspecified, but should be appropriate for the output device. The end of each line is marked with a $. When using the /usr/xpg6/bin/ed command, the end of each line is marked with a $ due to folding, and $ characters within the text are written with a preceding backslash. An l command can be appended to any other command other than e, E, f, q, Q, r, w, or !. The current line number is set to the address of the last line written.
(.,.)ma
(.,.)n
(.,.)p
P
q
Q
($)r file
%d\n, <number of bytes read>
The current line (.) is set to the last line read. If file is replaced by !, the rest of the line is taken to be a shell command (see sh(1)) whose output is to be read. For example, $r !ls appends the current directory to the end of the file being edited. Such a shell command is not remembered as the current file name.
(.,.)s/RE/replacement/
(.,.)s/RE/replacement/count,
count=[1-2047]
(.,.)s/RE/replacement/g
(.,.)s/RE/replacement/l
(.,.)s/RE/replacement/n
(.,.)s/RE/replacement/p
An ampersand (&) appearing in the replacement is replaced by the string matching the RE on the current line. The special meaning of & in this context may be suppressed by preceding it by \. As a more general feature, the characters \n, where n is a digit, are replaced by the text matched by the n-th regular subexpression of the specified RE enclosed between \( and \). When nested parenthesized subexpressions are present, n is determined by counting occurrences of \( starting from the left. When the character % is the only character in the replacement, the replacement used in the most recent substitute command is used as the replacement in the current substitute command. If there was no previous substitute command, the use of % in this manner is an error. The % loses its special meaning when it is in a replacement string of more than one character or is preceded by a \. For each backslash (\) encountered in scanning replacement from beginning to end, the following character loses its special meaning (if any). It is unspecified what special meaning is given to any character other than &, \, %, or digits.
A line may be split by substituting a new-line character into it. The new-line in the replacement must be escaped by preceding it by \. Such substitution cannot be done as part of a g or v command list. The current line number is set to the address of the last line on which a substitution is performed. If no substitution is performed, the current line number is unchanged. If a line is split, a substitution is considered to have been performed on each of the new lines for the purpose of determining the new current line number. A substitution is considered to have been performed even if the replacement string is identical to the string that it replaces.
The substitute command supports the following indicators:
count
g
l
n
p
(.,.)ta
u
(1,$)v/RE/command list
(1,$)V/RE/
(1,$)w file
"%d\n",<number of bytes written>
If file is replaced by !, the rest of the line is taken to be a shell (see sh(1)) command whose standard input is the addressed lines. Such a shell command is not remembered as the current path name. This usage of the write command with ! is to be considered as a ``last w command that wrote the entire buffer''.
(1,$)W file
X
($)=
"%d\n"<line number>
The current line number is unchanged by this command.
!shell command
"!\n"
to standard output upon completion, unless the -s option is specified. The current line number is unchanged.
(.+1)<new-line>
If an interrupt signal (ASCII DEL or BREAK) is sent, ed writes a "?\n" and returns to its command level.
The ed utility takes the standard action for all signals with the following exceptions:
SIGINT
SIGHUP
Some size limitations are in effect: 512 characters in a line, 256 characters in a global command list, and 255 characters in the path name of a file (counting slashes). The limit on the number of lines depends on the amount of user memory. Each line takes 1 word.
When reading a file, ed discards ASCII and NUL characters.
If a file is not terminated by a new-line character, ed adds one and puts out a message explaining what it did.
If the closing delimiter of an RE or of a replacement string (for example, /) would be the last character before a new-line, that delimiter may be omitted, in which case the addressed line is written. The following pairs of commands are equivalent:
s/s1/s2
g/s1
?s1
If an invalid command is entered, ed writes the string:
"?\n"
(followed by an explanatory message if help mode has been enabled by the H command) to standard output and continues in command mode with the current line number unchanged.
-C
-pstring
-s | -;
-x
The following operand is supported:
file
See largefile(7) for the description of the behavior of ed and red when encountering files greater than or equal to 2 Gbyte ( 2^31 bytes).
See environ(7) for descriptions of the following environment variables that affect the execution of ed: HOME, LANG, LC_ALL, LC_CTYPE, LC_COLLATE, LC_MESSAGES, and NLSPATH.
The following exit values are returned:
0
>0
$TMPDIR
/var/tmp
/tmp
ed.hup
See attributes(7) for descriptions of the following attributes:
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
CSI | Enabled |
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
CSI | Enabled |
Interface Stability | Standard |
ATTRIBUTE TYPE | ATTRIBUTE VALUE |
CSI | Enabled |
Interface Stability | Standard |
edit(1), ex(1), grep(1), ksh(1), sed(1), sh(1), stty(1), umask(1), vi(1), fspec(5), attributes(7), environ(7), largefile(7), regex(7), standards(7)
?
?file
If changes have been made in the buffer since the last w command that wrote the entire buffer, ed warns the user if an attempt is made to destroy ed's buffer via the e or q commands. It writes ? and allows one to continue editing. A second e or q command at this point will take effect. The -s command-line option inhibits this feature.
The - option, although it continues to be supported, has been replaced in the documentation by the -s option that follows the Command Syntax Standard (see Intro(1)).
A ! command cannot be subject to a g or a v command.
The ! command and the ! escape from the e, r, and w commands cannot be used if the editor is invoked from a restricted shell (see sh(1)).
The sequence \n in an RE does not match a new-line character.
If the editor input is coming from a command file (for example, ed file < ed_cmd_file), the editor exits at the first failure.
Loading an alternate malloc() library using the environment variable LD_PRELOAD can cause problems for /usr/bin/ed.
August 13, 2023 | OmniOS |