Editing with ed(1)
If you’re at all insterested in UNIX, you’ve surely run across mention of ed
, and maybe you’ve even tried to use it. After all, it is the standard editor. If you’ve tried it, you probably thought it was a relic. Hokey religions and ancient text editors are no match for a good IDE at your side, right?
But writing and editing in ed
can be fun and, indeed, fast! It’s a great program for taking notes. Since, unlike modern apps like vi
and notepad.exe, it’s not exactly fast to get out of edit mode, once you start typing, you’re encouraged to just keep going! Additionally, since ed
doesn’t work on characters, but instead on lines, it encourages you to think of what you’re writing in terms of blocks of text, rather than characters.
Okay, you’ve sold me. How do I actually use this thing?
That’s the great part, if you know how to use vi
, you already know how to use ed
! This is because vi
was started as a visual mode for ed
. Pretty neat, huh? So, to get into insert mode, you do the same thing as in vi
, and type i
. Then enter
, which I suppose you don’t have to do in vi
, but what’s one extra key? Start typing, and when you’re done, put a single dot (.
) on a line by itself to get out of insert mode. Other commands are similar. /
to search, c
to change (But remember: ed
operates on lines, not characters!), and wq
to write and quit.
So that’s the basics, learn that and you’re well on your way to ed
mastery! Need to replace a word in the middle of a line? Well, use sed
-like syntax to do that. Let’s examine that. Say I have a block of text, “Henlo world.” Oops! I meant to type “Hello world.” Easy enough:
s/Henlo/Hello/p
“Wait,” you might say, “I’ve seen this syntax before!” Well, I did just tell you this was sed
syntax, right? A lot of programs use it with the assumption that you’re familiar with it already.
What about spell check?
What about it? Your system probably already has aspell(1)
installed, so why would you want an editor that includes yet another spell checker? Just use the one you already have!
Some other things you might want to know
Okay, so here’s one of my favorie things about ed
. If I’m writing, but then notice an error on a previous line, I can simple exit insert mode (ret+.
) and then address the line I want to change, for example: 29s/foo/bar/p
. This moves to the 29th line, replaces the first occurance of foo
with bar
, then p
rints the line so I can see what I did. If I’m happy with the edit, I can just type $a
to resume typing at the end of the file. $
returns to the last line of the document, and a
tells ed
to append (That is, put this text on the next line).
What to print the last n lines? $-n,$p
.
Need line numbers? Replace p
with an n
.
ed
is so very useful, even today. ed
is fast, and likely has (when combined with your other system utilities) all the functionality you could want! ed
is fun, ed
is the standard editor.
using-ed.md
- 2021-08-14 | Fixed a formatting issue.
- 2019-09-26 | First published.