ed is a line-oriented text editor that lets you manipulate text files interactively. ed reads the text of a file into memory and stores it in an area called a buffer. Various commands let you edit the text in the buffer. Finally, you can write the contents of the buffer back out to the file, overwriting the old contents of the file.
red is a restricted version of ed. It is intended to protect the novice user by disallowing the ! command and the ability to access files found anywhere but the current directory.
lets you edit larger files by restricting the amount of memory dedicated to paging. This frequently makes ed run slower. This option has no benefit on UNIX or POSIX-compliant systems.
displays the given prompt string prompting you to input a command. By default, ed does not prompt for command input. See the description of the P command for more on command prompting.
puts ed into a quiet mode, in which the E, e, r and w commands do not display file size counts; the e and qe commands do not check buffer modification and ed does not display ! after calling the shell to execute a sub-command. This mode is particularly useful when you invoke ed from within a shell script.
causes file to have only newline instead of the usual carriage return/newline combination (Windows systems only).
performs an X command to handle encrypted files properly. See the description of the X command for more details.
If the optional file argument is present on the command line, ed reads the specified file into the buffer by simulating an efile command.
You can prefix commands in ed with zero, one, or two addresses. These addresses let you reference single lines or ranges of lines in the buffer. You do not need to specify addresses for certain commands that use default addresses. Consult the command's description for the default addresses.
You can construct each address out of the following components:
- The single dot character represents the current line number. Many commands set the current line number. For example, the e command sets it to the last line of the new file being edited.
- The dollar sign refers to the last line in the buffer.
- The number n refers to the nth line in the buffer.
- This searches for a line containing a string that matches the basic regular expression regexp (see regexp). The search begins at the line immediately following the current line. It proceeds forward through the buffer; if ed reaches the end of the buffer without finding a match, it wraps around to the first line of the buffer and continues the search. If ed does not find a match, the search ends when it reaches the original current line. If it finds a match, the address /regexp/ refers to the first matching line. If you omit regexp, the last used regular expression becomes the object of the search. You can omit the trailing /. Within regexp, \/ represents a literal slash and not the regexp delimiter.
- This is similar to the previous address form, except that the search goes backward through the buffer. If the search reaches the first line in the buffer without finding a match, ed wraps around and continues searching backward from the last line in the buffer. If you omit regexp, the last used regular expression becomes the object of the search. You can omit the trailing ?. Within regexp, \? represents a literal question mark and not the regexp delimiter.
- The address is the line marked with the mark name l. The name l must be a lowercase letter set by the k command.
You can combine these basic addresses with numbers using the + and - operators, with the usual interpretation (so .+1 is the current line plus one). Missing left operands (+1) default to . (dot); missing right operands (.+ or +) default to 1. Missing right operands also have a cumulative effect; so an address of -- refers to the current line number less 2.
You can specify address ranges in the following ways:
- a1, a2
- specifies a range of addresses from address a1 to address a2, inclusive. Omitting a1 and a2 (that is, specifying only the comma), is equivalent to the range 1,$.
- a1; a2
is similar to the previous form except that ed resets the
current line number after calculating
so that the second address,
is relative to
(that is, specifying only the semicolon),
is equivalent to
If you specify only a1 and the command requires both a1 and a2, the command operates as though you specified a range of
- displays a page of text, where a page is the height of your display area minus three lines. On a 25 line screen, > is equivalent to .,.+22 except that it never attempts to address any line beyond $.
- is like >, except that it displays the previous page of text. On a 25 line screen, < is equivalent to .-22,. except that it never addresses any line before line 1.
An ed command has the form:
All commands end with a new-line--you must press ENTER. Most commands allow only one command on a line, although most commands can be modified by appending the l, n, and p commands.
Commands generally take a maximum of zero, one, or two addresses, depending upon the particular command. In the following descriptions, we show commands with their default addresses (that is the addresses used when you don't specify any addresses) in a form that shows the maximum number of legal addresses for the command.
The E, e, r, W and w commands allow you to specify a file argument. For these commands, file can be either a path name or a shell escape of the form:
If you use the ! form, ed calls the shell identified by the SHELL environment variable to execute command-line. For the E, e and r commands, ed reads the standard output of this command line in the same way that it read the contents of a file. For the W and w commands, the command line treats the addressed lines as standard input.
The commands G, g, k, m, s, t, V, and v take parameters.
ed accepts the following commands:
appends text after the specified line. Valid addresses range from 0 (text is placed at the beginning of the buffer, before the first line) to $ (text is placed after the last line of the buffer). ed reads lines of text from the terminal until a line consisting solely of an unescaped . (dot) is entered. ed sets the current line number to the last line appended.
changes the addressed range of lines by deleting the lines and then reading new text in the manner of the a or i commands.
deletes the addressed range of lines. The line after the last line deleted becomes the new current line. If you delete the last line of the buffer, ed sets the current line number to the new last line. If no lines remain in the buffer, it sets the current line number to 0.
- E [file]
replaces the contents of the current buffer with the contents of file, like the e command. Unlike e, however, the E command gives no warning if you have changed the current buffer without writing the contents.
- e [file]
replaces the contents of the current buffer with the contents of file. If you did not specify file, ed uses the remembered file name, if any. In all cases, the e command sets the remembered file name to the file that it has just read into the buffer. ed displays a count of the bytes in the file unless it is in quiet mode. If you have changed the current buffer since the last time its contents were written out, ed displays a warning message and does not execute the command. If you enter the e command a second time, ed executes the command.
- f [file]
changes the remembered file name to file; file can be a complete path name. ed displays the new remembered file name. If you do not specify file, ed displays the current remembered file name.
is similar to the g command except that it is interactive. When ed finds a line that matches regexp, it displays the line and waits for you to type in the command to be executed. You cannot use the a , c , i , G , g , V , and v commands. If you enter &, the G command re-executes the last command you typed in. If you press ENTER, G does not execute any command for that line.
- 1,$g/regexp/command list
performs command on all lines that contain strings matching the regular expression regexp. This command works in two passes. In the first pass, ed searches the given range of lines and marks all those that contain strings matching the regular expression regexp, while the second pass actually performs command on those lines. You cannot use !, G, g, V, or v as command. command consists of one or more ed commands, the first of which must appear on the same line as the g commands. All lines except the last of a multi-line command list must end with a backslash (\). If command list is empty, ed assumes it to be the p command. If no lines match regexp, ed does not change the current line number; otherwise, the current line number is the one set by the last command in command. You can use any character other than space or newline instead of the slash (/) to delimit regexp.
tells ed to display descriptive messages when errors occur, or if ed is already displaying descriptive messages, returns ed to terse error messages. Normally, ed indicates error messages by displaying a ?. When you turn on descriptive error messages with this command, ed also displays the descriptive message for the most recent ? message (see the description of the h command).
provides a brief explanation of the last error that occurred. This does not change the current line number.
works like the a command except that ed inserts the text before the addressed line. Valid addresses range from line 1 to $ (the last line). ed sets the current line number to the last inserted line.
joins a range of lines into one line. To be precise, the j command removes all newline characters from the addressed range of lines, except for the last one. ed sets the current line number to the resulting combined line.
marks the addressed line with the mark name l, which is a single lowercase letter of the alphabet. This lets you refer to a marked line with the construct 'l. This is called an absolute address because it always refers to the same line, regardless of changes to the buffer.
displays the addressed range of lines, representing non-printable characters visibly. The end of a line is represented by the $ character, and the characters \\, \a, \b, \f, \r, \t and \v are printed as escape sequences. Each non-printable character is either represented as one of the above escape sequences or as a three-digit octal number. Lines which are too long to fit on one line are folded. You can append the l command to most other commands to check on the effect of those commands. ed sets the current line number to the last line so displayed.
moves the addressed lines to the point immediately following the line given by the address a. The address a must not be in the range of addressed lines. If address a is 0, ed moves the lines to the beginning of the buffer. The last line moved becomes the new current line.
displays the addressed lines in a way similar to the p command, but ed puts the line number and a tab character at the beginning of each line. The last line displayed becomes the new current line. You can append n to any command other than E, e, f, Q, q, r, w, or ! to check on the effect of that command.
turns on command prompting. If you specified the
-pprompt option on the ed command line, ed displays the prompt string whenever it is ready for you to type in another command. If you did not specify -p, ed uses the * character as a prompt. If command prompting is currently turned on, issuing the P command turns it off.
displays the addressed lines. The last line displayed becomes the new current line. You can append p to any command other than E, e, f, Q, q, r, w, or ! to check on the effect of that command.
quits unconditionally, without checking for buffer modifications.
exits ed. If you have made changes to the buffer since the last save, ed issues a warning. Entering the q command again lets you quit, regardless of unsaved changes.
- $r [file]
reads the contents of file into the buffer after the addressed line. If the address is 0, ed places the text before the first line in the buffer. If you do not specify file, ed uses the remembered file name; if no remembered file name exists, file becomes the new remembered name. The r command displays the number of bytes read from file unless you specified the
-soption. The last line read from the file becomes the new current line. If file is replaced by !, the rest of the line is considered a shell command line, the output of which is to be read.
searches the specified range of lines for strings matching the regular expression regexp. Normally the s command replaces the first such matching string in each line with the string new. The s command sets the current line number to the last line on which a substitution occurred. If ed makes no such replacements, ed considers it an error. flags can be zero or more of the following:
- replaces the nth matching string in the line instead of the first one
- replaces every matching string in each line, not just the first one
- displays the new current line in the format of the l command
- displays the new current line in the format of the n command
- displays the new current line in the format of the p command
You can use any printable character other than the space or newline instead of / to separate parts of the command provided that you use the same character to delimit all parts of the command. If you omit the trailing delimiter, ed behaves as though you had used the flag p.
You can include a newline in the new string by putting a \ immediately in front of the newline. This is a good way to split a line into two lines. If new consists only of the % character, s uses the new string from the previous s command. If & appears anywhere in new, ed replaces it with the text matching the regexp. If you want new to contain a literal ampersand or percent sign, put a backslash (\) in front of the & or % character.
copies the addressed lines to the point after the line given by the address a. The address a must not fall in the range of addressed lines. If address a is 0, ed copies the lines to the beginning of the buffer. This sets the current line to the last line copied.
rolls back the effect of the last command that changed the buffer. For the purposes of u, commands that change the buffer are: a, c, d, g, G, i, j, m, r, s, t, v, V and (of course) u. This means that typing u repeatedly flips the most recent change back and forth. ed treats all changes made by a global command (G, g, V, or v) as a single change. As a result, such changes can be easily undone. This command sets the current line number to the value it had immediately before you issued the command being undone.
is similar to the G command except that this command only gives you the chance to edit lines that do not match the given regular expression.
is similar to the g (global) command except that ed only applies the given commands to lines that do not match the given regular expression.
- 1,$W [file]
is similar to the w command except that this command appends data to the given file if the file already exists.
- 1,$w [file]
writes the addressed lines of the buffer to the named file. This does not change the current line number. If you do not provide file, ed uses the remembered file name; if there is no remembered file name, file becomes the remembered name. If the output file does not exist, ed creates it. ed displays the number of characters written unless you had specified the
prompts you to enter an encryption key. All subsequent e, r and w commands use this key to decrypt/encrypt text read from or written to files. To turn off encryption, issue an X command and press RETURN in response to the prompt for an encryption key.
runs command as if you typed it to your chosen command interpreter. If command contains the % character, ed replaces it with the current remembered file name. If you want a command to contain a literal %, put a backslash (\) in front of the character. As a special case, typing !! re-issues the previous command.
displays the line number of the addressed line. This does not change the current line number.
if you supply zero, one, or two addresses without an explicit command, ed displays the addressed lines in the mode of the last display command: p, l, or n. This sets the current line number to the last line displayed.
contains the terminal width in columns. ed folds lines at that point.
contains the path name of your home directory.
contains the full path name of the current shell.
is the path name of the directory being used for temporary files. If it is not set, PTC MKS Toolkit uses /tmp.
This is the paging file. It holds a copy of the file being edited. You can change the directory for temporary files using the environment variable TMPDIR (see envvar).
ed writes the current buffer to this file when it receives a hang-up signal.
Possible exit status values are:
Failure because of any of the following:
- — Addressed line out of range
- — Only one file name is allowed
- — No space for line table
- — Temporary file error
- — Badly constructed regular expression
- — No remembered regular expression
- — File read error
- — Out of memory
- — Unknown command
- — Illegal command suffix
- — No match found for regular expression
- — Wrong number of addresses for command
- — Need space after command
- — Name too long
- — Badly formed name
- — Illegal command redirection
- — Restricted shell
- — No remembered file name
- — Mark name must be lowercase
- — Undefined mark name
- — m and t commands require destination address
- — Destination cannot straddle source in m and t
- — Command not allowed inside g, v, G, or V
- — The x command has become X (uppercase)
- — Global command too long
- — Write error (no disc space)
Usage error, such as unknown command line option.
- Destination cannot straddle source in 'm' and 't'
The range of lines being moved or copied by m or t cannot include the destination address.
- Global command too long
There is a limit on the length of a global instruction (g or v). See the LIMITS section for this limit.
- 'm' and 't' require destination address
You must follow the m or t commands with an address indicating where you want to move or copy text. You omitted this address.
- No remembered file name
You tried to execute a command that used a remembered file name (For example, you used w to write without specifying an output file name). However, there is no remembered file name at present. Issue the command again, but specify a file name this time.
- Restricted shell
The command line invoked the restricted form of ed, but you tried an action that was not allowed in the restricted editor (the ! command).
- Temporary file error
You ran out of space on disk or encountered other errors involving the page file stored in the temporary file.
- Warning: file not saved
You issued a command to quit editing the current file (for example, q or e to edit a new file); however, you have changed the file since the last time you saved it. ed is suggesting that you save the file before you exit it; otherwise, your recent changes are lost. To save the file, use the w command. If you really do not want to save the recent changes, use q to quit or e to edit a new file.
An unspecified error occurred. Use the h or H command for more information.
If the input to ed comes from a script rather than a terminal, ed exits when any error occurs.
POSIX.2. x/OPEN Portability Guide 4.0. All UNIX systems. Windows 7. Windows Server 2008 R2. Windows 8. Windows Server 2012. Windows 10. Windows Server 2016. Windows Server 2019.
The addresses < and >,
The red command is also an extension to the POSIX and XPG standards.
ed allows up to 500,000 lines per file on Windows systems. It does not allow the NUL ('\0') character. The maximum length of a global command is 256 characters, including newlines.
PTC MKS Toolkit for Power Users
PTC MKS Toolkit for System Administrators
PTC MKS Toolkit for Developers
PTC MKS Toolkit for Interoperability
PTC MKS Toolkit for Professional Developers
PTC MKS Toolkit for Professional Developers 64-Bit Edition
PTC MKS Toolkit for Enterprise Developers
PTC MKS Toolkit for Enterprise Developers 64-Bit Edition
PTC MKS Toolkit 10.2 Documentation Build 28.