ED(1P)

๐ŸŒธ ED(1P) โ€” POSIX Programmer's Manual ๐ŸŒธ

okay bestie, listen, we're doing the POSIX Programmer's Manual today and the page is literally called ED(1P). Buckle up. ๐Ÿ’–


๐Ÿ“œ PROLOG

This manual page is part of the POSIX Programmer's Manual. The Linux implementation of this interface may differ (so like, consult the corresponding Linux manual page for the deets on Linux behavior), or honestly? the interface may not be implemented on Linux at all. ๐Ÿ™ˆ


โœจ NAME โœจ

ed โ€” edit text ๐Ÿ’…


๐ŸŽ€ SYNOPSIS ๐ŸŽ€

ed [-p string] [-s] [file]

๐Ÿฆ‹ DESCRIPTION ๐Ÿฆ‹

So ed is a line-oriented text editor, which means it works line by line, not like your fancy full-screen besties. It has exactly two modes: command mode and input mode. In command mode, the characters you type shall be interpreted as commands. In input mode, those same characters shall be interpreted as text. Wild, right? See the EXTENDED DESCRIPTION section for the whole lore.

If an operand is -, the results are unspecified. (iykyk, don't do it.)

๐Ÿ’… OPTIONS ๐Ÿ’…

The ed utility shall conform to the Base Definitions volume of POSIX.1โ€2017, Section 12.2, Utility Syntax Guidelines โ€” except for the unspecified usage of -. Yeah, that one's just vibes-undefined.

The following options shall be supported:

  • -p string โ€” use string as the prompt string when you're in command mode. By default, there shall be no prompt string at all. like, silence. periodt.
  • -s โ€” suppress the writing of byte counts by the e, E, r, and w commands, and also suppress the ! prompt after a !command. no cap, it's just quiet mode.

๐Ÿ“ 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. so it's like, opening the file first, then listening to you. ๐Ÿซถ

๐ŸŒธ STDIN ๐ŸŒธ

The standard input shall be a text file consisting of commands, as described in the EXTENDED DESCRIPTION section. yes bestie, you're literally typing commands in.


๐Ÿงš INPUT FILES ๐Ÿงš

The input files shall be text files. that's it, that's the whole rule. ๐Ÿ’–


๐ŸŒˆ 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โ€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, override the values of all the other internationalization variables. it's the main character of locale vars.
  • 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 โ€” ed shall interrupt its current activity, write the string "?\n" to standard output, and return to command mode (see the EXTENDED DESCRIPTION section). it's giving "huh? anyway."
  • SIGHUP โ€” if the buffer is not empty and has changed since the last write, ed 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, ed shall exit without writing the file to the currently remembered pathname and without returning to command mode. ๐Ÿ˜ญ it's saving your work in a panic file and then leaving.
  • SIGQUIT โ€” ed shall ignore this event. not today.

๐Ÿ’œ 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. no small talk.


๐ŸŽ€ OUTPUT FILES ๐ŸŽ€

The output files shall be text files whose formats are dependent on the editing commands given. like, it depends bestie.


๐Ÿ‘‘ EXTENDED DESCRIPTION ๐Ÿ‘‘

ed 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. yes, your edits live in the buffer, not on disk, until you write. slay responsibly. ๐Ÿ’…

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โ€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 is literally no way for a regular expression to match a <newline>. it's giving line-by-line only, no exceptions. ๐Ÿ’€

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 *n*th line of the edit buffer.
  4. The <apostrophe>-x character pair ("'x") 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 ("\/") 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 ("??") 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 ("\?") 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-minus> character ('-') followed by a decimal number shall address the current line plus or minus the number. A <plus-sign> or <hyphen-minus> 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-minus> 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-minus> 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. (it just drops the extras, bestie. first in, first out. ๐Ÿ™ˆ)

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. ๐ŸŒŸ

Okay so bestie, lowkey the cutest thing about addresses: you can just omit one side of the <comma> or <semicolon> and ed fills in the blank for you. It's giving โœจimpliedโœจ. Here's the whole vibe:

  • , โ†’ becomes 1 , $
  • , addr โ†’ becomes 1 , addr
  • addr , โ†’ becomes addr , addr
  • ; โ†’ becomes . ; $
  • ; addr โ†’ becomes . ; addr
  • addr ; โ†’ becomes addr ; addr

And any <blank> characters chillin' between addresses, address separators, or address offsets? Ignored, periodt. Don't stress them. ๐ŸŽ€


Commands in ed ๐Ÿ‘‘

So in the list of ed commands below, the default addresses are shown in parentheses โ€” like (.,.) โ€” and ngl the number of addresses in the default is literally the number the command expects. Bestie, the parentheses are not part of the address, they're just there to show you "hey, this is the default." ๐Ÿ’…

Generally it's invalid for more than one command to be on a line. Like, one command per line, that's the rule, no cap. BUT (and this is the slay part) any command โ€” except e, E, f, q, Q, r, w, and ! โ€” can be suffixed with the letter l, n, or p. When that happens, except for the l, n, and p commands themselves, the command shall be executed and then the new current line shall be written, exactly as described below under the l, n, and p commands. ๐Ÿฆ‹

Now here's a lil chaotic corner: when you use an l, n, or p suffix with an l, n, or p command, the command writes to standard output as described, but it is unspecified whether the suffix writes the current line again in the requested format or whether the suffix has no effect at all. ๐Ÿ˜ญ Example bestie: pl (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, honestly.

Also g, G, v, and V shall take a command as a parameter. Just so you know. ๐ŸŒธ

Each 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 blanks between them, bestie, they're attached at the hip. ๐Ÿซถ

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.


โš ๏ธ Warning time, and this one matters: 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. It writes the string:
"?\n"

(followed by an explanatory message if help mode has been enabled via the H command) to standard output, and shall continue in command mode with the current line number unchanged. But if the e or q command is repeated with no intervening command, it shall take effect. So like, it's a double-tap confirm. ๐Ÿ˜ญ

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 SIGHUP signal, ed shall operate as described in the ASYNCHRONOUS EVENTS section for a SIGHUP signal.
  • If it's not accompanied by a SIGHUP signal, ed 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 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 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. Bestie, look how these pairs of commands are literally 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 and shall continue in command mode with the current line number unchanged. It's not mad, it's just disappointed. ๐Ÿ’€


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. Address 0 shall be valid for this command โ€” and it shall cause the appended text to be placed at the beginning of the buffer. Iconic behavior. ๐Ÿ‘‘


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; and if no lines remain in the buffer, the current line number shall be set to zero. Address 0 shall be valid for this command; it shall be interpreted as if address 1 were specified. ๐ŸŽ€


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. Poof, gone, no trace. โœจ


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. 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. Living 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.


Global Command ๐Ÿ‘‘

Synopsis:

(1,$)g/RE/command list

In the g command, the first step shall be to 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.

โš ๏ธ Heads up bestie: the use of the g, G, v, V, and ! commands in the command list produces undefined results. Don't do it. ๐Ÿ˜ญ

Any character other than <space> or <newline> can be used instead of a <slash> to delimit the RE. 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, the first step shall be to 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 literally ed explaining itself. So helpful. ๐Ÿ’–


Help-Mode Command โœจ

Synopsis:

H

The H command shall cause ed to enter a mode where help messages (peep the h command) get written to standard output for all subsequent '?' notifications. And it's a toggle, bestie โ€” H flips this mode on and off, and it starts out off. If you're flipping help-mode on, the H command also explains the previous '?' notification, if there even was one. The current line number shall be unchanged. โœจ

๐ŸŒธ Insert Command ๐ŸŒธ

Synopsis:

(.)i
<text>
.

The i command shall insert the given text before the addressed line; the current line is set to the last inserted line or, if there was none, to the addressed line. The only difference between this and the a command is where the input text lands. Address 0 shall be valid for this command; it shall be interpreted as if address 1 were specified. ๐Ÿ’–

๐ŸŽ€ Join Command ๐ŸŽ€

Synopsis:

(.,.+1)j

The j command shall join contiguous lines by yeeting the appropriate <newline> characters. If exactly one address is given, this command shall do absolutely nothing. If lines are joined, the current line number shall be set to the address of the joined line; otherwise, the current line number shall be unchanged.

๐Ÿฆ‹ Mark Command ๐Ÿฆ‹

Synopsis:

(.)kx

The k command shall mark the addressed line with name x, which the application shall ensure is a lowercase letter from the portable character set. The address "'x" shall then refer to this line; the current line number shall be unchanged. iykyk ๐Ÿ’…

โœจ List Command โœจ

Synopsis:

(.,.)l

The l command shall write to standard output the addressed lines 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') shall be written as the corresponding escape sequence; the '\n' in that table is not applicable. Non-printable characters not in the table shall be written as one three-digit octal number (with a preceding <backslash> character) for each byte in the character (most significant byte first).

Long lines shall be folded, with the point of folding indicated by <newline> preceded by a <backslash>; the length at which folding occurs is unspecified, but should be appropriate for the output device. The end of each line shall be marked with a '$', and '$' characters within the text shall be 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 shall be set to the address of the last line written. ๐Ÿ”ฅ

๐Ÿ’œ Move Command ๐Ÿ’œ

Synopsis:

(.,.)maddress

The m command shall reposition the addressed lines after the line addressed by address. Address 0 shall be valid for address and cause the addressed lines to be moved to the beginning of the buffer. It shall be an error if address falls within the range of moved lines. The current line number shall be set to the address of the last line moved.

๐ŸŒˆ Number Command ๐ŸŒˆ

Synopsis:

(.,.)n

The n command shall write to standard output the addressed lines, preceding each line by its line number and a <tab>; the current line number shall be 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 !.

Synopsis:

(.,.)p

The p command shall write to standard output the addressed lines; the current line number shall be 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 !.

โญ Prompt Command โญ

Synopsis:

P

The P command shall cause ed to prompt with an <asterisk> ('*') (or string, if -p is specified) for all subsequent commands. The P command alternatively shall turn this mode on and off; it shall be initially on if the -p option is specified; otherwise, off. The current line number shall be unchanged.

๐ŸŽ€ Quit Command ๐ŸŽ€

Synopsis:

q

The q command shall cause ed to exit. If the buffer has changed since the last time the entire buffer was written, the user shall be warned, as described previously. ๐Ÿ˜ญ

๐Ÿ’€ Quit Without Checking Command ๐Ÿ’€

Synopsis:

Q

The Q command shall cause ed to exit without checking whether changes have been made in the buffer since the last w command. not me losing all my unsaved work fr fr ๐Ÿ™ˆ

๐Ÿ“ Read Command ๐Ÿ“

Synopsis:

($)r [file]

The r command shall read in the file named by the pathname file and append it after the addressed line. If no file argument is given, the currently remembered pathname, if any, shall be used (see the e and f commands). The currently remembered pathname shall not be changed unless there is no remembered pathname. Address 0 shall be valid for r and shall cause the file to be read at the beginning of the buffer. If the read is successful, and -s was not specified, the number of bytes read shall be written to standard output in the following format:

"%d\n", <number of bytes read>

The current line number shall be set to the address of the last line read in. 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 pathname.

๐Ÿ’… Substitute Command ๐Ÿ’…

Synopsis:

(.,.)s/RE/replacement/flags

The s command shall search each addressed line for an occurrence of the specified RE and replace either the first or all (non-overlapped) matched strings with the replacement; see the following description of the g suffix. It is 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. Within the RE, the RE delimiter itself can be used as a literal character if it is preceded by a <backslash>. The current line shall be set to the address of the last line on which a substitution occurred.

An <ampersand> ('&') appearing in the replacement shall be replaced by the string matching the RE on the current line. The special meaning of '&' in this context can be suppressed by preceding it by <backslash>. As a more general feature, the characters '\n', where n is a digit, shall be replaced by the text matched by the corresponding back-reference expression. If the corresponding back-reference expression does not match, then the characters '\n' shall be replaced by the empty string. When the character '%' is the only character in the replacement, the replacement used in the most recent substitute command shall be used as the replacement in the current substitute command; if there was no previous substitute command, the use of '%' in this manner shall be an error. The '%' shall lose its special meaning when it is in a replacement string of more than one character or is preceded by a <backslash>. For each <backslash> encountered in scanning replacement from beginning to end, the following character shall lose its special meaning (if 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 by <backslash>. Such substitution cannot be done as part of a g or v command list. The current line number shall be set to the address of the last line on which a substitution is performed. If no substitution is performed, the current line number shall be unchanged. If a line is split, a substitution shall be considered to have been performed on each of the new lines for the purpose of determining the new current line number. A substitution shall be considered to have been performed even if the replacement string is identical to the string that it replaces.

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 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

The t command shall be equivalent to the m command, except that a copy of the addressed lines shall be placed after address address (which can be 0); the current line number shall be set to the address of the last line added.

๐Ÿ‘‘ Undo Command ๐Ÿ‘‘

Synopsis:

u

The u command shall nullify 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 shall be undone as a single change; if no changes were made by the global command (such as with g/RE/p), the u command shall have no effect. The current line number shall be set to the value it had immediately before the command being undone started. the way that this is the main character of recovery ๐Ÿ˜ญโœจ

๐Ÿ’œ Global Non-Matched Command ๐Ÿ’œ

Synopsis:

(1,$)v/RE/command list

This command shall be equivalent to the global command g except that the lines that are marked during the first step shall be those for which the line excluding the terminating <newline> does not match the RE.

๐ŸŒธ Interactive Global Not-Matched Command ๐ŸŒธ

Synopsis:

(1,$)V/RE/

This command shall be equivalent to the interactive global command G except that the lines that are marked during the first step shall be those for which the line excluding the terminating <newline> does not match the RE.

๐ŸŒˆ Write Command ๐ŸŒˆ

Synopsis:

(1,$)w [file]

The w command is literally the "save my bestie's work" icon ๐Ÿ’พโœจ โ€” it writes the addressed lines into the file named by the pathname file. If that file doesn't exist yet, it gets created, and if it does exist, its contents get replaced (no merge, no take-backs, the old stuff is GONE).

The currently remembered pathname stays exactly as it is โ€” unless there's no remembered pathname at all. And if you don't hand over a pathname? It'll just use the currently remembered one, if there is one (peep the e and f commands for that whole situation ๐Ÿ‘€). Either way, the current line number does not move. Not even a little.

If the command slays (successfully), the number of bytes written gets written to standard output โ€” unless the -s option was specified โ€” in this exact format:

"%d\n", <number of bytes written>

Now here's the fun twist ๐ŸŒˆ: if file starts with '!', then the rest of the line is taken to be a shell command line, and the addressed lines become its standard input. That shell command line shall not be remembered as the current pathname. And this usage of w with '!' shall not count as a "last w command that wrote the entire buffer," like we described earlier โ€” so on its own it won't stop the editor from warning you if you try to destroy the editor buffer with the e or q commands. ๐Ÿซถ protect your buffer, bestie.


โœจ Line Number Command โœจ

Synopsis:

($)=

The line number of the addressed line gets written to standard output in this format:

"%d\n", <line number>

And the current line number? Unchanged. Frozen. Immovable. Periodt. ๐Ÿ’…


๐Ÿš Shell Escape Command ๐Ÿš

Synopsis:

!command

Everything on the line after the '!' gets sent to the command interpreter to be interpreted as a shell command line. 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 yes, "!!" repeats the previous !command โ€” the laziest repeat button ever and we love it ๐Ÿ˜Œ.

If any replacements of '%' or '!' happen, the modified line gets written to standard output before the command is executed. And the ! command writes:

"!\n"

to standard output when it's done โ€” unless the -s option is specified. Current line number? Still unchanged, bestie. ๐Ÿฆ‹


๐ŸŒธ Null Command ๐ŸŒธ

Synopsis:

(.+1)

An address all by itself on a line makes the addressed line get written out. A <newline> alone is equivalent to "+1p". And the current line number gets set to the address of the written line โ€” so this one actually does move the current line, unlike its chill siblings above. Main character behavior fr fr ๐Ÿ‘‘


๐Ÿ’€ EXIT STATUS ๐Ÿ’€

Here's what you get back:

  • 0 โ€” successful completion without any file or command errors. Slay. โœ…
  • >0 โ€” an error occurred. It's giving chaos. ๐Ÿ˜ญ

๐Ÿ˜ณ CONSEQUENCES OF ERRORS ๐Ÿ˜ณ

So when an error in the input script shows up, or when an error is detected that's a consequence of the data that is (or isn't!) present in the file, or due to an external condition like a read or write error:

  • If standard input is a terminal device file, all input gets flushed, and a new command is read. Fresh start energy โœจ
  • If standard input is a regular file, ed shall terminate with a non-zero exit status. No recovery arc. ๐Ÿ’€

The following sections are informative.


๐Ÿซถ APPLICATION USAGE ๐Ÿซถ

Because the default error messages are extremely terse (like, they tell you nothing, they're so mysterious and for what), the prudent script writer starts their ed input commands with an H command. That way, if errors do happen, at least you get some clue about what caused them. iykyk ๐Ÿ’ก

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 the standard's cute little way of letting implementations keep supporting the former required behavior. Finesse ๐Ÿ’…


๐ŸŒˆ EXAMPLES ๐ŸŒˆ

None. (Not one. Zero. The void. Empty. iconic in its own minimalist way ๐ŸŽ€)

๐Ÿ’… RATIONALE (aka why is it Like This) ๐Ÿ’…

okay bestie, so the very first description of this utility was adapted from the SVID. It's got some features you won't find 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 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โ€2017.

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โ€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. iconic behavior honestly ๐Ÿ’–

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. periodt.

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. (iykyk ๐ŸŽ€)

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, bestie, write these down ๐Ÿ“:

  • +++ โ€” 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. sneaky but correct ๐Ÿฆ‹

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 lists some of the address forms now possible โ€” the table has Address, Addr1, Addr2, Status, and Comment:

  • 7, โ€” Addr1: 7, Addr2: 7 โ€” Status: Historical.
  • 7,5, โ€” Addr1: 5, Addr2: 5 โ€” Status: Historical.
  • 7,5,9 โ€” Addr1: 5, Addr2: 9 โ€” Status: Historical.
  • 7,9 โ€” Addr1: 7, Addr2: 9 โ€” Status: Historical.
  • 7,+ โ€” Addr1: 7, Addr2: 8 โ€” Status: Historical.
  • , โ€” Addr1: 1, Addr2: $ โ€” Status: Historical.
  • ,7 โ€” Addr1: 1, Addr2: 7 โ€” Status: Extension.
  • ,, โ€” Addr1: $, Addr2: $ โ€” Status: Extension.
  • ,; โ€” Addr1: $, Addr2: $ โ€” Status: Extension.
  • 7; โ€” Addr1: 7, Addr2: 7 โ€” Status: Historical.
  • 7;5; โ€” Addr1: 5, Addr2: 5 โ€” Status: Historical.
  • 7;5;9 โ€” Addr1: 5, Addr2: 9 โ€” Status: Historical.
  • 7;5,9 โ€” Addr1: 5, Addr2: 9 โ€” Status: Historical.
  • 7;$;4 โ€” Addr1: $, Addr2: 4 โ€” Status: Historical โ€” Comment: Valid, but erroneous.
  • 7;9 โ€” Addr1: 7, Addr2: 9 โ€” Status: Historical.
  • 7;+ โ€” Addr1: 7, Addr2: 8 โ€” Status: Historical.
  • ; โ€” Addr1: ., Addr2: $ โ€” Status: Historical.
  • ;7 โ€” Addr1: ., Addr2: 7 โ€” Status: Extension.
  • ;; โ€” Addr1: $, Addr2: $ โ€” Status: Extension.
  • ;, โ€” 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. a rare moment of peace, we love to see it ๐Ÿงš


๐Ÿ‘€ 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

okay bestie, legal bit, but it's actually kinda interesting, stay with me ๐Ÿซถ

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, if this version and the original IEEE and The Open Group Standard ever disagree, the original IEEE and The Open Group Standard is the referee document. like, it's the main character, this page is just the group chat recap. ๐Ÿ˜ญ

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. not the vibe, but it happens ๐Ÿ™ˆ To report such errors, see https://www.kernel.org/doc/man-pages/reporting_bugs.html .


and that's a wrap on ED(1P), bestie ๐ŸŽ€โœจ this one came from IEEE/The Open Group, dated 2017 โ€” iconic source, iconic year, iconic page. go forth and edit your text files with main character energy. slay, ed, slay. ๐Ÿ‘‘๐Ÿ’…๐Ÿฆ‹

๐Ÿ“ฆ where this page comes from