jump to navigation

The Importance of the Editor “ed” June 5, 2009

Posted by Robert Harder in Utility.
Tags: , ,

You know you’re having a rough day when you start editing files with “ed,” a somewhat painful-to-use editor that at least never lets you down. It doesn’t pick you up very far either, but hey, having low expectations is the key to happiness. I keep an ed quick reference sheet the size of a matchbox taped to my laptop for these kinds of days. Here’s the Wikipedia article.

If you’ve never heard of ed, you’re probably a normal person (sorry to insult you). I won’t rehash the history, but here’s a nice excerpt from Gnu.org (http://www.gnu.org/fun/jokes/ed.msg.html) that says it all:

From: patl@athena.mit.edu (Patrick J. LoPresti)
Subject: The True Path (long)
Date: 11 Jul 91 03:17:31 GMT
Newsgroups: alt.religion.emacs,alt.slack

When I log into my Xenix system with my 110 baud teletype, both
vi and Emacs are just too darn slow. They print useless messages
like, ‘C-h for help’ and ‘“foo” File is read only’. So I use the
editor that doesn't waste my VALUABLE time.

Ed, man!  !man ed

When is ed Helpful

Let’s not fool ourselves, ed is almost always not the tool you want to use, but if you’ve ever ssh-ed into a system and discovered that the TERM settings are all messed up, that’s when you start appreciating ed.

Because ed does not require (or support) any of those fancy screen control features (like moving a cursor around), it works even with broken TERM settings. That’s pretty much the entire and only reason why I ever use it — and I’m grateful for it too.

Working with ed

There are ed tutorials out there that are at least as good as ed itself, but here’s a quick idea of how you interact with ed. It is a line editor, so rather than having a traditional cursor position as with a normal text editor, you have an active line on which you can operate. At a command prompt try editing a favorite file (after making a backup) with a command like

ed ~/.bash_profile

Since I’m not stuck with a 110 baud line, I usually start by listing the file to help get my bearing:


That’s right. It’s a comma followed by a lowercase ‘N.’ The comma is shorthand for “all lines,” and the ‘N’ means print with line numbers.

Suppose I want to see all those alias shortcuts I make, I’ll go to the beginning of the file with one command and then jump to the first alias command with a second. I’ll follow it up with a command to reprint that first alias line with a line number, again to help with my bearing. The commands I typed are bold.

alias cl='clear; ls -lhG'
15	alias cl='clear; ls -lhG'

That’s actually pretty easy. You can hit the Return key to advance line by line. I can replace the current line with one or more lines:

alias cl='clear; ls -lG'
15	alias cl='clear; ls -lG'

Now I’m not pestered with that annoying KB, MB, GB notation in my file sizes; I get raw bytes. Note the line with the period which tells ed that I’m done entering text.

There’s a quicker way to make some edits that can keep you from having to retype an entire line. You can do substitutions with regular expressions. To put my command back the way it was:

alias cl='clear; ls -lhG'
15	alias cl='clear; ls -lhG'

Save your changes with the standalone ‘w’ command, and you’re done.

My ed Quick Reference Sheet

My ed quick reference sheet is this, printed with white text on black background:

ed quick reference
1,$_ or ,_ Command _ on all lines
p    Print line
l    Print line with $
n    Print with line numbers
c    Change line
i    Insert before line
a    Append after line
ka   Mark as line a
'a   Goto marked line a
/foo Jump to line with foo
  Replace foo with bar, this line
  Replace all foo with bar

Here’s a PDF with the text already inverted and printed in several sizes to fit different laptops and screen sizes as I go through life.


I hope it serves you as well as it has me.


1. Keylor - February 17, 2010

Great article, thanks man.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: