Using ed

About a year and a half ago, I wrote, somewhat disparagingly, about the idea of using the ed editor for every day work. The problem is that ed is a line editor meant for use with teletype-like devices. Because it can’t use cursor addressing, editing happens on whole lines. Although a few intrepid souls claim to use it for day-to-day work, it really has only two uses today:

  • Emergency fixes. Because ed is small and lives in /bin, it’s available even if Unix/Linux has problems coming up and wasn’t able to mount /usr/bin, the home of full screen editors such as Vi(m) and Emacs. Many developers and system administrators learn ed for just such occasions.
  • Scripting. Most editors can do some sort of scripting but they aren’t really that good at it. Scripting has always been an important ed use case.

After writing the above post and another that looked at a scripting application of ed, I went back to not thinking about it. Then, I saw the coda to Brian Kernighan’s video about where the name “grep” came from. In it, Kernighan was asked what editor he uses today. He mostly uses either Vi or Sam but said that he occasionally fires up ed for a quick editing job or when he wants to script. For some reason that inspired me to take another look at ed.

I’d never bothered to actually learn ed but I knew enough about it from Vi—which is built on top of ed1—that I could use it in an emergency. There are only a handful of commands to learn (remember there are no cursor movements) so it was pretty easy to get up to speed. Almost all the action is in the line addressing, which, again, is almost the same as that in the Vi(m) ex commands.

I have a log file that I edit everyday—mostly just deleting certain lines that match a regexp—so I’ve taken to doing that in ed. That helps me remember the ed commands and various line addressing functions. Occasionally, I’ll do some other (really unnecessary) edits just for practice. The next time I have a boot emergency, I’ll be ready. Of course, I haven’t had to worry about that in some years but if the day ever comes…

If you’d like to come up to speed with ed (it probably takes less than an hour), here are some resources:

  1. The Gnu ed manual. It’s sort of like an extended man page and covers all the commands.
  2. Kernighan’s ed tutorial which covers just about everything you need to know about ed.
  3. Kernighan’s Advanced Editing on UNIX tutorial, which covers some of the more advanced ed features.

Realistically, you’ll probably never need to use it in anger but if you do learn it, you can tell everyone that you can use the standard Unix editor, and as Eric S Fraga says, familiarity with ed makes using sed scripts easy.

NOTE (Added after this was written but before it was published): Despite popular belief, ed is far from dead and is, in fact, still being actively maintained as a GNU project. Here’s the announcement of the release of GNU ed 1.15. It’s 2019 and the standard Unix editor just keeps on keeping on.

Footnotes:

1

Or actually ex, which is a slightly improved version of ed.

This entry was posted in General and tagged . Bookmark the permalink.