This book is new. If you'd like to go through it, then join the Learn Code Forum to get help while you attempt it. I'm also looking for feedback on the difficulty of the projects. If you don't like public forums, then email help@learncodethehardway.org to talk privately.

Exercise 48: ed

If your process is working then you should be able to focus on one long project for a few weeks at a time. In this project your goal is to create the most accurate faithful copy of the ed command as you possibly can. The goal of this exercise is to not be creative at all but to do a methodical exact copy of another piece of software. Think of this as a forgery exercise. You'd want something so good that you could put it in place of the original ed and nobody would know.

This exercise will be to create a master copy of the ed command as exactly as you can, which means your test suite should run the real ed and your version against the same scripts to compare the output. This is like the Master Copy exercise you did when learning algorithms, except you're copying the behavior of an existing piece of software rather than trying to memorize it. The process is similar, but you get to use test suites to help make it go faster.

Exercise Challenge

The ed tool is one of the very first Unix text editors in existence, and frankly, it sucks. I actually can't imagine how anyone used ed to edit text as it is one of the most user hostile pieces of software in existence. If you can't imagine how much computing sucked back in the bad old days of Unix, forging a copy of ed will give you a taste.

Something to realize about ed is that, while it does support scripting, it was originally used interactively. It was like a MUD for text files. You first ran ed and it started in a command mode that let you modify the text with commands. When you did a command that required input it would enter input mode until that command ended. You also had to know the addresses of lines to edit. This may seem like a pain but compared to other text editors at the time this was magic from a unicorn horn.

To complete your ed copy you'll need to rely heavily on the re library in Python to do your regular expressions. We used this library in Exercise 31 <ex31.html>, so you should be familiar with it and RegEx in general.

I also suggest you attempt to use ed for one 45 minute session to write some code for your ed project. The pain of doing this will teach you much about how to copy it.

Other than that, you will want to read the man ed page to get the basics of the command and possibly watch tutorials on using it. A good first step would be to find different example scripts you find online and try to make them work as your first test case.


I'll give you a clue that you'll want to use a Finite State Machine to handle the modal nature of the ed command.

Study Drills

  1. Find the source code to GNU ed and take a look, even if you don't know C.
  2. Turn your ed implemenation into a module that you can then use in other projects. You'll need this for later exercises.
  3. Never make a piece of software like this again unless you're just bored.

Pre-order Learn More Python The Hard Way

When you pre-order Learn More Python The Hard Way, you'll receive the Python 3 Edition as it's being created. All files are DRM free and you can download them to your computer for offline viewing. Digital Download Only! You do not get a physical book.


Buy the Online Course

Or, you can read Learn More Python the Hard Way for free right here, video lectures not included.

Other Buying Options

Other buying options coming soon.

No content available for this exercise. You can view all available downloads at your customer account page.