Ed

Ah Ed, the standard editor. ed a simple editor, which usefulness can be found when vi doesn't cut it anymore:

  • vi needs /usr to be mounted, ed doesn't
  • vi is dynamically linked, ed isn't
  • ed can be assumed to almost always exist, vi can be often assumed to exist
  • vi needs $TERM to be set and termcap file exist
  • bsd.rd ships with ed but not vi:
# vi
ksh: vi: not found
# ed
BEHOLD THE POWER OF MIGHTY ED
?
  • ed is cooler
  • ?

Most of these are too niche for a day-to-day life, but there are times, ed worth it's manual page in gold. when crisis happens, ed can be your only friend. ed, like vi is a modal editor, which means it has different modes, input mode and command mode. input is for writing text and command is for commands, such as q, w, s and so on.

Input mode

input mode is where you type text into your document, to exit this mode, type:

.

in a empty line. you will be returned to command mode. to enter command mode, any one of these commands get you into input mode:

aappend after current line
iinsert before current line
creplace current line

Command mode

basic ed commands

Note that some ed commands can be combined, as they did in vi, for example wq.

eopens a file to edit (includes !command notation too). cleans the buffer
Esame as e, but without warning on unsaved file
fsets default filename
Hverbose errors (instead of "?")
hexplain last error ("?")
nprint current line and it's line number
pprint current line
Ptoggles prompt, only if you had -p in arguments of ed
qquits ed
Qq, but without warning for unsaved file
uundo, there is only one level of undo (which includes undo too)
rread from a file (or a !command)
wsave the buffer
=shows how many lines current file has (wc -l)
any numbersets current line

advanced ed commands

m,nddeletes mth line until nth line in buffer
m,nG/re/finds and moves on lines which include regular expression re from mth line, until nth line
m,njjoins all lines from mth line to nth line
m.ng/re/commandfinds all lines containing re regular expression and executes (ed) command on them.
s/re/replacementsreplaces all all "re"'s with replacements
m.nV/re/like G, but reverse (all lines that does not contain re)

ed symbols

, or %all lines from first line until current line
??repeats last search done by ?re?
?re?previous line containing regular expression re
-n or ^nnth previous line
- or ^previous line
nnthn line in buffer
.current line
$last line in buffer
+nn line upper
/re/next line containing regular expression re
//repeats last search done by /re/
+next line
%file name
'lcshows (book?)marked line in buffer (see k command)

Examples

Opening a file

$ ed file

Using ed quietly, handy for scripts

$ ed -s file