Previous Table of Contents Next



Command Description

[.]a
text
.
The append command places you in insert mode. The text you type is entered into the buffer after the address you provide. If you do not provide an address, the current line is assumed. If you specify address 0, the text is inserted before the first line of the buffer. To exit insert mode and return to the command mode you must type a . (period) at the beginning of a line followed by a Return.
C An encryption key is prompted for and is used to decrypt text read and written by all subsequent e, r, and w commands. An empty (null) key turns encryption off. See the -C option.
[.]c The change command allows you to change existing text with new text. The text you enter replaces the text specified by the address. The new text you enter may be any number of lines. For example, if you specify two lines of text to change, you can immediately enter a . (period) and press Return. This would replace the two lines of text with no lines, the same as deleting them. Or you could enter 20 lines of text. It doesn't matter how many lines you specify in the address and how many new lines you enter. The current line becomes the last new line of text. If no new lines were entered, the current line becomes the previous line.
[.,.]d The delete command deletes the specified lines from the buffer. The default address is the current line. The current line becomes the previous line.
e file The edit command causes ed to edit a new file. The current contents of the buffer are deleted. The contents of the specified file is then read into the buffer. If you try using this command and ed responds with a ? then you have made modification to the buffer since the last save to disk. You can override the changes by typing e again. Your changes to the current buffer will be lost and the new file will be edited. You can save the changes by using the w command. Then re-enter the e file command. If no file is specified, the current filename is used. Thus you can re-edit the current file if you really foul things up in the buffer. Just type e Return and e Return again or use the E command.
E file Same as the e command except ed does not check to see if you have outstanding modifications that need to be saved to disk. This command is useful if you decide to start over on the current editing session.
f file The file command changes the current filename to file. If you do not specify a file then the current filename is displayed.
[1,$]g/RE/ed-cmds
The global command locates all of the lines that contain a string matching the specified regular expression RE. Each of the lines matched are set to the current line and the specified edit command ed-cmd is performed. For example,
5,25g/SVR4.0/d
searches from line 5 to line 25 for the string "SVR4.0" and deletes any lines containing a match. If you do not specify an address, the entire buffer is searched. If you do not specify an ed-cmd, the p command is assumed. The g, G, v, and V, commands cannot be used as ed-cmds. Multiple ed-cmds may be specified by placing a \ (backslash) at the end of each line except for the last one. For instance,
g/UNIX/s//UNIX SVR3.2/\
i\
NOTE: A New Release of UNIX\
.
locates each line containing "UNIX." A substitution replaces "UNIX" with "UNIX SVR3.2" and a note is inserted before each of these lines.
[1,$]G/RE/ This is the global interactive command. It locates all lines that match the given regular expression RE. Each of the lines matched is set to the current line. The editor displays each line and you are required to enter one and only one ed command. You cannot use the a, c, i, g, G, v, or V command. The command you specify is performed on the current line. After the command is completed the next line containing a match is displayed. To have no command executed on the current line press Return. You can repeat the last command by typing the & (ampersand) key and pressing Return. To abort the interactive global command press the Delete or Break key.
h Displays a brief error message explaining the cause of the last ? diagnostic message. If something is wrong with a command, ed displays a ? (question mark). You must type h and press Return to get an explanation.
H Turns the help feature on and off. If the help feature is on, then a brief message is displayed instead of the ?. If the help feature is off, the ? is displayed and you must use the h command to display the brief explanation.
[.]i The insert command is like the a (append) command, except test the text is inserted before the addressed line instead of after it. You cannot use the i command in an empty file. Nor can you specify address 0. The last inserted line becomes the new current line when you return to the command mode. If no new text is inserted, then the address specified becomes the new current line.
[.,+1]j The join command removes the new-line character at the end of each line. Thus the addressed lines become one long line.
[.]kc The mark command marks the addressed line with letter c. The letter c must be lowercase. You use the k command to set new addresses in the buffer. Then you can reference the address with the 'c address convention. The c may be any character from a to z.
[.,.]l The list command displays the nonprintable characters in a visual representation. Control characters are displayed as ^c or as octal numbers, depending on the function they perform. You can use the l command with any other edit command except the e, f, r, or w command.
[.,.]madr The move command moves the addressed lines to reside after the line specified by adr. You can specify an address of 0 to move the lines to the beginning of the buffer. The address adr cannot be within the specified range of lines being moved. The current line becomes the last line moved.
[.,.]n The number command displays each addressed line preceded by a number and a tab. The numbers are not part of the buffer contents; they are for your information only. The current line becomes the last line displayed. You can use the n command in conjunction with any command except the e, f, r, or w command. If no address is given, the current line is displayed.
[.,.]p The print command displays each addressed line. The current line becomes the last line displayed. You can use the p command in conjunction with any command except the e, f, r, or w command. If no address is given the current line is displayed.
P The prompt command turns the default prompt on or off. The default prompt is an * (asterisk). The prompt is initially turned off.
q The quit command causes ed to exit. If you have saved the contents of the buffer to disk since the last modification, ed exits. If you have outstanding modifications, ed returns a ? or "file not saved" message. You can use the w command to save the buffer contents to disk, then retype q. If you do not want to save the changes to disk, you can repeat the q command again and ed will exit.
Q The nonchecking quit command. This command is equivalent to typing two q commands. The editor does not check for unsaved changes. It immediately exits the buffer whether you have saved modifications to disk or not.
[$]r file The read command reads in the specified file and places the contents after the addressed line. By default, the file is appended to the end of the buffer. If you specify address 0, the file is inserted at the beginning of the buffer. If the file is read in without error, the number of bytes read in are displayed. If you do not specify a file, the currently remembered filename is used. The r command does not reset the remembered filename unless it is the first filename used in the current editing session.
[$]r !command The read command can also read the standard output of any UNIX command. This is like the r file command except the input is read from a command instead of a file. For example,
r!ls
reads the output of the ls command executed in the current directory.
[.,.]s/RE/rstr/
[.,.]s/RE/rstr/g
[.,.]s/RE/rstr/n
The substitute command searches each specified line for a matching regular expression RE. If a match occurs, the first RE on the line is replaced with the specified rstr string. If you use the g suffix, all occurrences of RE are replaced on each line (global line substitute). The n suffix causes only the nth occurrence of RE to be replaced on each line. The last line a substitution occurs on becomes the new current line.
The delimiter character may be any character except a space or new-line. For example, instead of s/old/new/ you can use s#old#new#.
The & character may be used in the RE or rstr (replacement string) to represent the last RE. For example,
s/UNIX/&-TM/
substitutes UNIX to be UNIX-TM.
The % character may be used in the RE or rstr to represent the last rstr. For example,
s/OS/%/
substitutes OS to be UNIX-TM, assuming you just performed the previous example s/UNIX/&-TM/.
[.,.]tadr The copy command works like the move command (m) except a copy of the addressed text is placed after the adr address. The current line becomes the last line of the copied text.
u The undo command reverses the effects of the last text manipulation command.
[1,$]v/RE/ed-cmds
The global exception command functions like the g command except it selects lines that do not match the specified RE. For example,
v/^$/p
displays all lines in the buffer that are not empty lines. The ^$ regular expression means beginning of line followed by end of line, which is equal to an empty line. Refer to the g command for further information.
[1,$]V/RE/ Like the interactive G command, except it selects lines that do not match the specified RE. Refer to the G command for further details.
[1,$]w file The write command writes the specified lines from the buffer to the file. If the write is successful, the editor returns the number of bytes (characters) written to the file. If you do not specify an address range, the entire buffer is saved to disk. If you do not specify a file, the current remembered filename is used. This command does not change the remembered filename unless the specified filename happens to be the first filename used in the editor.
[1,$]W file The same as the w command except the lines are appended to the specified file.
[1,$]w!command
The same as the w command except the lines are written to the standard input of the specified UNIX command. For example,
10,50w!pr -t2
writes lines 10 through 50 to the UNIX pr command. The pr command formats the input into two-column output.
X Same as -C but an educated guess is made as to whether or not the text read (in by e and r commands) is encrypted.
[$]= The line number command displays the line number of the specified address. If no address is given, the last line is used, thereby giving you the total number of lines in the file. Another common use is the .= format which returns the current line number.
!command The escape command escapes from the editor and invokes the shell to execute the specified UNIX command. For example,
!grep mylogin /etc/passwd
greps for the string mylogin from the passwd file. This command does not read the output of the command into the buffer. To do that you must use the r! command.
The % character may be used in the command to represent the currently remembered filename. For example,
!%
executes the current file you are editing. This is useful if you are editing a shell script and need to check it before exiting the editor.
Address Commands
<CR> Pressing Return on a line by itself displays the next line and increments the current line number.
n A number command may be used to change the current line position to the given line number.
. The dot represents the current line and thus displays the current line when used by itself.
;
>
The ; and > characters represent addresses .,$, which is from the current line to the end of the buffer.
,
<
The , and < characters represent addresses 1,. which is from the first line to the current line.
& The repeat command repeats the last command you entered.

In most cases you can only specify one command per line. The l, n, and p commands are exceptions to this rule. You can specify these three commands with other commands. This is because they are all display oriented commands. But you cannot use them with the e, E, f, or w commands.


Previous Table of Contents Next