Home > Utility > The Importance of the Editor “ed”

The Importance of the Editor “ed”

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:

,n

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.

1
#PATH=/usr/local/bin:/opt/local/bin:$PATH
/alias
alias cl='clear; ls -lhG'
n
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:

c
alias cl='clear; ls -lG'
.
n
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:

s/lG/lhG
alias cl='clear; ls -lhG'
n
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
s/foo/bar
  Replace foo with bar, this line
1,$s/foo/bar/
  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.

ed-quick-reference-cheat-sheet

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

Categories: Utility Tags: , ,
  1. Keylor
    February 17th, 2010 at 15:23 | #1

    Great article, thanks man.

  1. No trackbacks yet.