ED(1POSIX)
๐ PROLOG ๐
okay bestie, welcome to ED(1POSIX) โ the POSIX Programmer's Manual page for ed โจ 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. so if Linux is doing its own thing, go check the Linux man page, and if it's not there at all, that's also a whole possibility.
๐ธ NAME ๐ธ
ed โ edit text
โจ SYNOPSIS โจ
ed [-p string] [-s] [file]
๐ DESCRIPTION ๐
So ed is a line-oriented text editor, and it's giving two modes: command mode and input mode ๐ In command mode, the input characters have to be interpreted as commands. In input mode, they have to be interpreted as text. See the EXTENDED DESCRIPTION section for the deep dive.
If an operand is '-', the results are unspecified. no cap, POSIX is not telling you what happens there.
๐ OPTIONS ๐
ed has to conform to the Base Definitions volume of POSIX.1โ2017, Section 12.2, Utility Syntax Guidelines, except for the unspecified usage of '-'.
The following options have to be supported:
-p stringโ Usestringas the prompt string when you're in command mode. By default, there is no prompt string.-sโ Suppress the writing of byte counts bye,E,r, andwcommands, and suppress the'!'prompt after a!command.
๐ OPERANDS ๐
The following operand has to be supported:
fileโ If thefileargument is given,edsimulates anecommand on the file named by the pathname,file, before it accepts commands from the standard input.
๐ฆ STDIN ๐ฆ
The standard input is a text file consisting of commands, as described in the EXTENDED DESCRIPTION section.
๐ INPUT FILES ๐
The input files are text files.
๐ง ENVIRONMENT VARIABLES ๐ง
These environment variables affect how ed runs:
HOMEโ Determines the pathname of the user's home directory.LANGโ Provides a default value for the internationalization variables that are unset or null. (See the Base Definitions volume of POSIX.1โ2017, 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, it overrides the values of all the other internationalization variables.LC_COLLATEโ Determines the locale for the behavior of ranges, equivalence classes, and multi-character collating elements within regular expressions.LC_CTYPEโ Determines 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โ Determines 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โ Determines the location of message catalogs for the processing ofLC_MESSAGES.
๐ ASYNCHRONOUS EVENTS ๐
ed takes the standard action for all signals (see the ASYNCHRONOUS EVENTS section in Section 1.4, Utility Description Defaults), with these exceptions:
SIGINTโedinterrupts its current activity, writes the string"?\n"to standard output, and returns to command mode (see the EXTENDED DESCRIPTION section).SIGHUPโ If the buffer is not empty and has changed since the last write,edattempts to write a copy of the buffer in a file. First, it uses the file nameded.hupin the current directory; if that fails, it uses the file nameded.hupin the directory named by theHOMEenvironment variable. In any case,edexits without writing the file to the currently remembered pathname and without returning to command mode.SIGQUITโedignores 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 is used only for diagnostic messages.
๐ฅ OUTPUT FILES ๐ฅ
The output files are text files whose formats depend on the editing commands given.
๐ EXTENDED DESCRIPTION ๐
ed operates on a copy of the file it's editing; changes made to the copy have no effect on the file until a w (write) command is given. That 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 the addresses very often can be omitted. If the -p option is specified, the prompt string is 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's said to be in input mode. In this mode, no commands are 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 ๐ฆ
ed supports basic regular expressions, as described in the Base Definitions volume of POSIX.1โ2017, 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's no way for a regular expression to match a <newline>.
A null RE is 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 is the last line in the edit buffer; otherwise, zero.
Addresses are constructed as follows:
- The
<period>character ('.') addresses the current line. - The
<dollar-sign>character ('$') addresses the last line of the edit buffer. - The positive decimal number
naddresses thenth line of the edit buffer. - The
<apostrophe>-xcharacter pair ("'x") addresses the line marked with the mark name characterx, which is a lowercase letter from the portable character set. It's 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. - A BRE enclosed by
<slash>characters ('/') addresses 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 addresses 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 ("\/") represents a literal<slash>instead of the BRE delimiter. If necessary, the search wraps around to the beginning of the buffer and continues up to and including the current line, so that the entire buffer is searched. - A BRE enclosed by
<question-mark>characters ('?') addresses 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 ("??") addresses 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 ("\?") represents a literal<question-mark>instead of the BRE delimiter. If necessary, the search wraps around to the end of the buffer and continues up to and including the current line, so that the entire buffer is searched. - A
<plus-sign>('+') or<hyphen-minus>character ('-') followed by a decimal number addresses the current line plus or minus the number. A<plus-sign>or<hyphen-minus>character not followed by a decimal number addresses 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-minus>character followed by a decimal number adds or subtracts, respectively, the indicated number of lines to or from the address. A<plus-sign>or<hyphen-minus>character not followed by a decimal number adds or subtracts 1 to or from the address. - A decimal number adds the indicated number of lines to the address.
It's not an error for an intermediate address value to be less than zero or greater than the last line in the edit buffer. It's an error for the final address value to be less than zero or greater than the last line in the edit buffer. It's 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's an error. Otherwise, if more than the required number of addresses are provided to a command, the addresses specified first are evaluated and then discarded until the maximum number of valid addresses remain, for the specified command.
Addresses are separated from each other by a <comma> (,) or <semicolon> character (;). In the case of a <semicolon> separator, the current line (.) is 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.
okay so you can literally ghost an address on either side of a <comma> or <semicolon> and ed will just fill in the blanks for you like the bestie it is. Here's the tea on what you get:
,โ1 , $, addrโ1 , addraddr ,โaddr , addr;โ. ; $; addrโ. ; addraddr ;โaddr ; addr
And any <blank> characters chillin' between addresses, address separators, or address offsets? Ignored. Completely. Periodt.
๐ธ Commands in ed ๐ธ
So in this next list, the default addresses are shown in parentheses, bestie. And like โ the number of addresses shown in the default is the number the command expects. The parentheses are not part of the address, they're just there like a little highlighter going "this is the default, iykyk".
General rule: it's usually invalid to have more than one command on a line. HOWEVER โ literally any command (except e, E, f, q, Q, r, w, and !) can be suffixed with the letter l, n, or p. And when you do that? Except for the l, n, and p commands themselves, the command runs first and then the new current line gets written exactly like described below under the l, n, and p commands. slay.
Now when you use an l, n, or p suffix on an l, n, or p command, the command writes to standard output as described below, but โ and this is the chaotic part โ it is unspecified whether the suffix writes the current line again in the requested format or whether the suffix has no effect at all. Like, it's giving mystery. Example: the pl command (that's the 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. Pick your fighter, idk.
Also, g, G, v, and V commands take a command as a parameter. Just so u know. ๐
Every address component can be preceded by zero or more <blank> characters. The command letter can be preceded by zero or more <blank> characters too. But if a suffix letter (l, n, or p) is given, the application shall ensure it immediately follows the command โ no gap, no space, no nothing. Clingy suffix, we respect it.
The e, E, f, r, and w commands take an optional file parameter, separated from the command letter by one or more <blank> characters.
๐ซถ the warning era ๐ซถ
If changes have been made in the buffer since the last w command that wrote the entire buffer, ed will warn you if you try to destroy the editor buffer via the e or q commands. Like, it's literally looking out for you. The ed utility shall write the string:
"?\n"
(followed by an explanatory message if help mode has been enabled via the H command) to standard output, and then it shall continue in command mode with the current line number unchanged. And if you repeat the e or q command with no intervening command? Then it takes effect. You said what you said. ๐ญ
๐ฆ terminal disconnect vibes ๐ฆ
If a terminal disconnect is detected (see the Base Definitions volume of POSIX.1โ2017, Chapter 11, General Terminal Interface, Modem Disconnect and Closing a Device Terminal):
- If it's accompanied by a
SIGHUPsignal, ed shall operate as described in the ASYNCHRONOUS EVENTS section for aSIGHUPsignal. - If it's not accompanied by a
SIGHUPsignal, ed shall act as if an end-of-file had been detected on standard input.
And if an end-of-file is detected on standard input:
- If ed 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 ed is in command mode, it shall act as if a
qcommand had been entered.
โจ the omitted delimiter trick โจ
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 โ and in that case the addressed line shall be written. Obsessed. For example, these pairs of commands are equivalent:
s/s1/s2 s/s1/s2/p
g/s1 g/s1/p
?s1 ?s1?
And if an invalid command is entered, ed shall write the string:
"?\n"
(followed by an explanatory message if help mode has been enabled via the H command) to standard output, then continue in command mode with the current line number unchanged. Just a lil ? and a dream. ๐
๐ Append Command ๐
Synopsis:
(.)a
<text>
.
The a command shall read the given text and append it after the addressed line. The current line number shall become the address of the last inserted line โ or, if there were none, the addressed line. And address 0 is valid for this command: it shall cause the appended text to be placed at the beginning of the buffer. Front of the line energy. ๐
๐ Change Command ๐
Synopsis:
(.,.)c
<text>
.
The c command shall delete the addressed lines, then accept input text that replaces these lines. The current line shall be set to the address of the last line input; or, if there were none, at the line after the last line deleted; if the lines deleted were originally at the end of the buffer, the current line number shall be set to the address of the new last line; if no lines remain in the buffer, the current line number shall be set to zero. And address 0 is valid for this command too โ it shall be interpreted as if address 1 were specified. Glow-up transformation fr.
๐ฅ Delete Command ๐ฅ
Synopsis:
(.,.)d
The d command shall delete the addressed lines from the buffer. The address of the line after the last line deleted shall become the current line number; if the lines deleted were originally at the end of the buffer, the current line number shall be set to the address of the new last line; if no lines remain in the buffer, the current line number shall be set to zero. Gone. Not me deleting the whole buffer and acting surprised. ๐
๐ Edit Command ๐
Synopsis:
e [file]
The e command shall delete the entire contents of the buffer and then read in the file named by the pathname file. The current line number shall be set to the address of the last line of the buffer. If no pathname is given, the currently remembered pathname, if any, shall be used (see the f command).
The number of bytes read shall be written to standard output, unless the -s option was specified, in the following format:
"%d\n", <number of bytes read>
The name file shall be remembered for possible use as a default pathname in subsequent e, E, r, and w commands. And if file is replaced by !, the rest of the line shall be taken to be a shell command line whose output is to be read. Such a shell command line shall not be remembered as the current file. All marks shall be discarded upon the completion of a successful e command. And if the buffer has changed since the last time the entire buffer was written, the user shall be warned, as described previously. โญ
๐ Edit Without Checking Command ๐
Synopsis:
E [file]
The E command shall possess all properties and restrictions of the e command, except that the editor shall not check to see whether any changes have been made to the buffer since the last w command. Live dangerously, bestie.
๐ Filename Command ๐
Synopsis:
f [file]
If file is given, the f command shall change the currently remembered pathname to file. Whether the name is changed or not, it shall then write the (possibly new) currently remembered pathname to the standard output in the following format:
"%s\n", <pathname>
The current line number shall be unchanged. Staying put, as it should. ๐ซถ
๐ Global Command ๐
Synopsis:
(1,$)g/RE/command list
In the g command, step one: mark every line for which the line excluding the terminating <newline> matches the given RE. Then, going sequentially from the beginning of the file to the end of the file, the given command list shall be executed for each marked line, with the current line number set to the address of that line. Any line modified by the command list shall be unmarked. When the g command completes, the current line number shall have the value assigned by the last command in the command list. If there were no matching lines, the current line number shall not be changed.
A single command, or the first of a list of commands, shall appear on the same line as the global command. All lines of a multi-line list except the last line shall be ended with a <backslash> preceding the terminating <newline>; the a, i, and c commands and associated input are permitted. The . terminating input mode can be omitted if it would be the last line of the command list. An empty command list shall be equivalent to the p command.
โ ๏ธ The use of theg,G,v,V, and!commands in the command list produces undefined results. Do not do it. I'm serious. ๐ญ
Any character other than <space> or <newline> can be used instead of a <slash> to delimit the RE. And within the RE, the RE delimiter itself can be used as a literal character if it is preceded by a <backslash>. โจ
๐ง Interactive Global Command ๐ง
Synopsis:
(1,$)G/RE/
In the G command, step one again: mark every line for which the line excluding the terminating <newline> matches the given RE. Then, for every such line, that line shall be written, the current line number shall be set to the address of that line, and any one command (other than one of the a, c, i, g, G, v, and V commands) shall be read and executed.
A <newline> shall act as a null command (causing no action to be taken on the current line); an & shall cause the re-execution of the most recent non-null command executed within the current invocation of G. Note that the commands input as part of the execution of the G command can address and affect any lines in the buffer. Any line modified by the command shall be unmarked. The final value of the current line number shall be the value set by the last command successfully executed. (Note that the last command successfully executed shall be the G command itself if a command fails or the null command is specified.) If there were no matching lines, the current line number shall not be changed.
The G command can be terminated by a SIGINT signal. Any character other than <space> or <newline> can be used instead of a <slash> to delimit the RE and the replacement. Within the RE, the RE delimiter itself can be used as a literal character if it is preceded by a <backslash>. ๐ฆ
๐ Help Command ๐
Synopsis:
h
The h command shall write a short message to standard output that explains the reason for the most recent ? notification. The current line number shall be unchanged. It's giving "here's what went wrong, bestie". ๐ฅบ
โญ Help-Mode Command โญ
Synopsis:
H
The H command shall make ed slide into a whole mode where help messages (peep the h command) get written to standard output for every single '?' notification that comes after. The H command flips this mode on and off, alternating, and it starts off โ like, it's not on by default bestie. If you're turning help-mode on, the H command also explains the previous '?' notification, if there was one. The current line number stays exactly where it was. ๐ซถ
โจ Insert Command โจ
Synopsis:
(.)i
<text>
.
The i command inserts the text you give it before the addressed line; the current line gets set to the last inserted line, or if there wasn't any, to the addressed line. The way this differs from the a command is literally just where the input text lands. Address 0 is valid here โ it gets interpreted as if you'd said address 1. ๐
๐ธ Join Command ๐ธ
Synopsis:
(.,.+1)j
The j command joins contiguous lines together by yeeting the appropriate <newline> characters. If exactly one address is given, this command does nothing โ like, sit there and think about it. If lines actually got joined, the current line number is set to the address of the joined line; otherwise the current line number doesn't budge.
๐ Mark Command ๐
Synopsis:
(.)kx
The k command marks the addressed line with the name x, which the application shall ensure is a lowercase letter from the portable character set. After that, the address 'x refers to this line. Current line number: unchanged, periodt. โญ
๐ฆ List Command ๐ฆ
Synopsis:
(.,.)l
The l command writes the addressed lines to standard output in a visually unambiguous form. The characters listed in the Base Definitions volume of POSIX.1โ2017, Table 5-1, Escape Sequences and Associated Actions ('\\', '\a', '\b', '\f', '\r', '\t', '\v') get written as the corresponding escape sequence; the '\n' in that table is not applicable here, okay? Non-printable characters that aren't in the table get written as one three-digit octal number (with a preceding <backslash> character) for each byte in the character, most significant byte first.
Long lines get folded, and the point of folding is marked by a <newline> preceded by a <backslash>; the length at which folding happens is unspecified, but it should be appropriate for the output device. The end of each line is marked with a '$', and any '$' characters within the text get written with a preceding <backslash> so you can tell them apart. An l command can be appended to any other command other than e, E, f, q, Q, r, w, or !. The current line number gets set to the address of the last line written. ๐
๐ฅ Move Command ๐ฅ
Synopsis:
(.,.)maddress
The m command repositions the addressed lines to after the line addressed by address. Address 0 is valid for address and makes the addressed lines move to the very beginning of the buffer. It's an error if address falls within the range of moved lines โ like, you can't move a thing into itself, bestie. The current line number gets set to the address of the last line moved.
๐ Number Command ๐
Synopsis:
(.,.)n
The n command writes the addressed lines to standard output, putting each line's line number and a <tab> in front of it; the current line number gets set to the address of the last line written. The n command can be appended to any command other than e, E, f, q, Q, r, w, or !.
๐ Print Command ๐
Synopsis:
(.,.)p
The p command writes the addressed lines to standard output; the current line number gets set to the address of the last line written. The p command can be appended to any command other than e, E, f, q, Q, r, w, or !. Classic. Iconic. No notes. ๐
โจ Prompt Command โจ
Synopsis:
P
The P command makes ed prompt you with an <asterisk> ('*') โ or a string, if -p was specified โ for all commands from then on. The P command alternates this mode on and off; it's initially on if the -p option was specified, otherwise off. The current line number stays unchanged.
๐ Quit Command ๐
Synopsis:
q
The q command makes ed exit. But bestie โ if the buffer has changed since the last time the entire buffer was written, the user gets warned, as described previously. It's giving "are you SURE."
๐ Quit Without Checking Command ๐
Synopsis:
Q
The Q command makes ed exit without checking whether changes have been made in the buffer since the last w command. No vibes check, no warning, just gone. ๐จ
๐ Read Command ๐
Synopsis:
($)r [file]
The r command reads in the file named by the pathname file and appends it after the addressed line. If no file argument is given, the currently remembered pathname gets used, if there is one (see the e and f commands). The currently remembered pathname shall not be changed unless there is no remembered pathname. Address 0 is valid for r and makes the file get read at the beginning of the buffer. If the read is successful and -s was not specified, the number of bytes read gets written to standard output in the following format:
"%d\n", <number of bytes read>
The current line number gets set to the address of the last line read in. If file is replaced by '!', the rest of the line gets taken as a shell command line whose output is to be read. Such a shell command line shall not be remembered as the current pathname. ๐
๐ Substitute Command ๐
Synopsis:
(.,.)s/RE/replacement/flags
The s command searches each addressed line for an occurrence of the specified RE and replaces either the first or all (non-overlapped) matched strings with the replacement; peep the description of the g suffix below. It's an error if the substitution fails on every addressed line. Any character other than <space> or <newline> can be used instead of a <slash> to delimit the RE and the replacement. Inside the RE, the RE delimiter itself can be used as a literal character if it's preceded by a <backslash>. The current line gets set to the address of the last line on which a substitution occurred.
An <ampersand> ('&') showing up in the replacement gets replaced by the string matching the RE on the current line. The special meaning of '&' in this context can be suppressed by putting a <backslash> in front of it. And as a more general feature, the characters '\n', where n is a digit, get replaced by the text matched by the corresponding back-reference expression. If the corresponding back-reference expression doesn't match, then those '\n' characters get replaced by the empty string. When the character '%' is the only character in the replacement, the replacement used in the most recent substitute command gets used as the replacement in the current substitute command; if there was no previous substitute command, using '%' like this is an error. The '%' loses its special meaning when it's in a replacement string of more than one character or is preceded by a <backslash>. For each <backslash> encountered while scanning replacement from beginning to end, the following character loses its special meaning (if it had any). It is unspecified what special meaning is given to any character other than <backslash>, '&', '%', or digits. ๐
A line can be split by substituting a <newline> into it. The application shall ensure it escapes the <newline> in the replacement by preceding it with a <backslash>. Such a substitution cannot be done as part of a g or v command list. The current line number gets set to the address of the last line on which a substitution is performed. If no substitution is performed, the current line number stays unchanged. If a line gets split, a substitution shall be considered to have been performed on each of the new lines for the purpose of figuring out the new current line number. And a substitution shall be considered to have been performed even if the replacement string is identical to the string it replaces โ yes, even a no-op counts, bestie. ๐ซ
The application shall ensure that the value of flags is zero or more of:
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 bothgand count are specified, the results are unspecified.lโ write to standard output the final line in which a substitution was made. The line gets written in the format specified for thelcommand.nโ write to standard output the final line in which a substitution was made. The line gets written in the format specified for thencommand.pโ write to standard output the final line in which a substitution was made. The line gets written in the format specified for thepcommand.
๐ง Copy Command ๐ง
Synopsis:
(.,.)taddress
The t command is equivalent to the m command, except a copy of the addressed lines gets placed after address (which can be 0); the current line number gets set to the address of the last line added. Same energy, different outcome. ๐
๐ Undo Command ๐
Synopsis:
u
The u command nullifies the effect of the most recent command that modified anything in the buffer โ namely the most recent a, c, d, g, i, j, m, r, s, t, u, v, G, or V command. All changes made to the buffer by a g, G, v, or V global command get undone as a single change; if no changes were made by the global command (like with g/RE/p), the u command has no effect. The current line number gets set to the value it had immediately before the command being undone even started. It's giving time travel, honestly. โช
โญ Global Non-Matched Command โญ
Synopsis:
(1,$)v/RE/command list
This command is equivalent to the global command g except that the lines marked during the first step are the ones for which the line, excluding the terminating <newline>, does not match the RE.
๐ธ Interactive Global Not-Matched Command ๐ธ
Synopsis:
(1,$)V/RE/
This command is equivalent to the interactive global command G except that the lines marked during the first step are the ones for which the line, excluding the terminating <newline>, does not match the RE.
๐ Write Command ๐
Synopsis:
(1,$)w [file]
Okay so bestie, when you hit w, it writes the addressed lines into the file named by the pathname file. If that file doesn't exist yet? It creates it, no biggie. Already exists? It straight up replaces the contents, like a glow-up but for files. ๐
The currently remembered pathname does not get changed โ unless there's no remembered pathname at all. And if you don't give a pathname, it just uses the currently remembered one, if there is one (peep the e and f commands for that). The current line number? Untouched. Iconic behavior, honestly.
If it all works out, the number of bytes written gets printed to standard output โ unless you passed the -s option โ in this exact format:
"%d\n", <number of bytes written>
Not me being obsessed with byte counts โจ
Now. If file starts with !, plot twist: the rest of the line is treated as a shell command line, and its standard input shall be the addressed lines. That shell command line is not remembered as the current pathname. And heads up โ this usage of the write command with ! shall not count as a "last w command that wrote the entire buffer" like we talked about earlier. So by itself, it shall not prevent the warning to the user if you try to destroy the editor buffer via the e or q commands. ๐ซฃ
Line Number Command ๐
Synopsis:
($)=
The line number of the addressed line gets written to standard output, like this:
"%d\n", <line number>
And the current line number stays exactly the same. No notes. ๐
Shell Escape Command ๐ฆ
Synopsis:
!command
Everything after the ! gets sent to the command interpreter to be interpreted as a shell command line. Now here's the fun part โ inside that shell command line text:
- an unescaped
%gets replaced with the remembered pathname ๐ - if a
!shows up as the first character of the command, it gets replaced with the text of the previous shell command executed via!
So "!!" literally just repeats the previous !command. Slay, minimal effort. โจ
If any replacements of % or ! happen, the modified line gets written to standard output before command is executed. And when it's done, the ! command shall write:
"!\n"
to standard output upon completion โ unless the -s option was specified. Current line number? Unchanged, bestie, it's staying right where it is. ๐
Null Command ๐ธ
Synopsis:
(.+1)
An address alone on a line shall cause the addressed line to be written. A <newline> alone is equivalent to "+1p". And the current line number gets set to the address of the written line. Periodt. ๐
๐ EXIT STATUS ๐
Here's what you can get back:
0โ Successful completion without any file or command errors. ๐>0โ An error occurred. ๐ญ
๐ฑ CONSEQUENCES OF ERRORS ๐ฑ
When there's an error in the input script, or an error gets detected that's a consequence of the data (not) present in the file, or because of some external condition like 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,
edshall terminate with a non-zero exit status. ๐
The following sections are informative.
๐ APPLICATION USAGE ๐
Because the default error messages are, and I quote, "extremely terse" (read: they tell you basically nothing), the prudent script writer begins their ed input commands with an H command. That way, if any errors do occur, at least you get some clue as to the cause. Wise move, honestly. ๐ง
Also, in earlier versions of this standard, an obsolescent - option was described. That's no longer specified. Applications should use the -s option instead. And using - as a file operand now produces unspecified results โ which is a whole choice, but it's intentional, because it lets implementations keep supporting the former required behavior. ๐
โญ EXAMPLES โญ
None.
Like, literally none. Nothing. Nada. The void. ๐๐
๐ RATIONALE ๐
okay so bestie, gather round, because this is the โจloreโจ section and honestly it's giving history-class-but-make-it-cute ๐
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โ2017. - BSD does not support the
'!'character part of thee,r, orwcommands. - A failed
gcommand 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
pcommand. - BSD does not support the
G,h,H,n, orVcommands. ๐ - 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
Pcommand; moreover, in BSD it is synonymous with thepcommand. - BSD does not support the undo of the commands
j,m,r,s, ort. - The Version 7
edcommandW, and the BSDedcommandsW,wq, andzare not present in this volume of POSIX.1โ2017.
The -s option was added to allow the functionality of the removed - option in a manner compatible with the Utility Syntax Guidelines. periodt ๐
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โ2017. Since this limit was removed, this volume of POSIX.1โ2017 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 "a\0011" 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 "\\" 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โ2017 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. (iykyk)
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โ2017 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โ2017.
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, "1,2,3,4,5p" 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 "3;/foo/;+2p" will display the first line after line 3 that contains the pattern foo, plus the next two lines. Note that the address "3;" must still be evaluated before being discarded, because the search origin for the "/foo/" command depends on this. ๐ง
Historically, ed disallowed address chains, as discussed above, consisting solely of <comma> or <semicolon> separators; for example, ",,," or ";;;" were considered an error. For consistency of address specification, this restriction is removed. The following list shows some of the address forms now possible:
- Address
7,โ Addr17, Addr27โ Status: Historical - Address
7,5,โ Addr15, Addr25โ Status: Historical - Address
7,5,9โ Addr15, Addr29โ Status: Historical - Address
7,9โ Addr17, Addr29โ Status: Historical - Address
7,+โ Addr17, Addr28โ Status: Historical - Address
,โ Addr11, Addr2$โ Status: Historical - Address
,7โ Addr11, Addr27โ Status: Extension - Address
,,โ Addr1$, Addr2$โ Status: Extension - Address
,;โ Addr1$, Addr2$โ Status: Extension - Address
7;โ Addr17, Addr27โ Status: Historical - Address
7;5;โ Addr15, Addr25โ Status: Historical - Address
7;5;9โ Addr15, Addr29โ Status: Historical - Address
7;5,9โ Addr15, Addr29โ Status: Historical - Address
7;$;4โ Addr1$, Addr24โ Status: Historical โ Comment: Valid, but erroneous. - Address
7;9โ Addr17, Addr29โ Status: Historical - Address
7;+โ Addr17, Addr28โ Status: Historical - Address
;โ Addr1., Addr2$โ Status: Historical - Address
;7โ Addr1., Addr27โ Status: Extension - Address
;;โ Addr1$, Addr2$โ Status: Extension - Address
;,โ Addr1$, Addr2$โ Status: Extension
Historically, ed accepted the '^' character as an address, in which case it was identical to the <hyphen-minus> character. POSIX.1โ2008 does not require or prohibit this behavior. ๐
๐ฎ FUTURE DIRECTIONS ๐ฎ
None.
ngl, iconic of it to just say none and dip ๐
๐ SEE ALSO ๐
- Section 1.4, Utility Description Defaults,
ex,sed,sh,vi - The Base Definitions volume of POSIX.1โ2017, 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 Guidelines
๐ COPYRIGHT ๐
okay bestie, legal tea time but make it sparkle โจ. Portions of this text are reprinted and reproduced in electronic form from IEEE Std 1003.1-2017, Standard for Information Technology -- Portable Operating System Interface (POSIX), The Open Group Base Specifications Issue 7, 2018 Edition, Copyright (C) 2018 by the Institute of Electrical and Electronics Engineers, Inc and The Open Group. no cap, that's the whole copyright situation ๐ซถ.
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, periodt ๐. The original Standard can be obtained online at http://www.opengroup.org/unix/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, lowkey ๐ญ. To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html . ๐
and the cute lil footer vibe: this page is IEEE/The Open Group, dated 2017, and it's ED(1POSIX) โ slay, bye bestie ๐ โจ
๐ฆ where this page comes from
- debian bookworm: manpages-posix 2017a-2 ยท Other
- debian trixie: manpages-posix 2017a-2 ยท Other
- ubuntu jammy: manpages-posix 2017a-2 ยท Other
- ubuntu noble: manpages-posix 2017a-2 ยท Other
- ubuntu resolute: manpages-posix 2017a-2build1 ยท Other
- ubuntu stonking: manpages-posix 2017a-2.1 ยท Other
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms