Text Editing with ed

Although you will often have the luxury of using your favorite text or GUI based text editor, that will not always be the case. When building new Unix systems, for example, you may only have access to the ed editor. This document briefly goes over the basics of this editor so that you can get around the program without causing too much damage.

Using ed

Commonly Used Commands


Command Meaning
!command Executes a shell command (for example, !pwd shows the current directory)
$ Goes to the last line in the buffer
% Applies a command that follows to all lines in the buffer (for example, %p prints all lines)
+ Goes to the next line
+n Goes to the nth next line
, Applies a command that follows to all lines in the buffer (for example, ,p prints all lines); similar to %
- Goes to the preceding line
-n Goes to the nth previous line
. Refers to the current line in the buffer
/text/ Searches forward for the specified text
; Refers to a range of lines: current through last line in the buffer
= Prints the lines number
?text? Searches backward for the specified text
^ Goes to the preceding line; also see the - command
^n Goes to the nth previous line; also see the -n command
a Appends after the current line
c Changes specified lines
d Deletes specified lines
i Inserts text before the current line
n Goes to the line number n
newline Displays the next line and makes that line current
q Quits the editor
Q Quits without saving changes
r file Reads and inserts contents of file after the current line
s/old/new/ Replaces old string with new
u Undoes the last command
W file Appends contents of buffer to the end of the specified file
w file Saves buffer in the specified file (If no file is named, saves in the default file -- the file whose contents ed is currently editing)