ed(1) is The Right Tool (I)
=================================

You know: ed(1) is the standard text editor [1], and indeed you would
find ed(1) installed in any unix-like system. But I guess most Unix
users have never actually tried to open it. The reason is probably that
ed(1) is considered "hard-to-use" and "user-unfriendly". I really see no
reason for such misconceptions and no motivation for their popularity,
so I will try to convince you with concrete examples and scientific
evidence that ed(1) is, almost always, The Right Tool to use.

This proof will unfold through a few phlog posts, in the hope to serve
as an "ed-primer" for beginners. If you are in a rush and would like to
go a proper book, look for the great "Ed Mastery" by Michael Lucas [2].

First things first: ed(1) is a line editor, meaning that it acts on a
line at a time. This is something we are not used to anymore, since our
glass-terminals have allowed full-screen character-based editors to
evolve. But on teletypes line-based editing makes a lot of sense. This
means that we need to tell ed(1) which line we would like to edit and
what to do on that line.

In this series of phlogs we will use ed(1) to compile and manage simple
TODO lists. This will allow us to explore almost all the ed(1) commands.

Let's start by creating the file "TODO.txt" with ed(1):

 $ ed TODO.txt
 TODO.txt: No such file or directory


Well, welcome to ed(1), aka "The Faithful Silent Servant of Unix masters
of old". ed(1) is telling you that it could not find any file named
TODO.txt in the current directory (and this is something we knew
already) and then it prints a new-line to signal that it is ready to
receive commands. Yes, ed(1) has no default "prompt". Actually, it does,
and the prompt is just "no-character-at-all". Now let's add a few items
to our TODO.txt list:

 0a
 TODO LIST
 - phlog about ed(1)
 - put rubbish bins outside
 - make a tea
 - check postgrey hiccup
 .


The first line "0a" tells ed(1) that I want to "a-ppend" lines after the
0-th line. Now, the 0-th line actually indicates the start of the
buffer. ed(1) accepts my command and waits for the lines to be inserted
there. It will keep receiving lines until you input a line containing
only a "." (dot) [3].  Then ed(1) will "print" its prompt (which is
indeed "no-character") and wait for the next command. Let's ask ed(1) to
"p-rint" the whole TODO list then:

 ,p
 TODO LIST
 - phlog about ed(1)
 - put rubbish bins outside
 - make a tea
 - check postgrey hiccup


(notice the ed(1) prompt on the last line!). The command "p" is used to
print lines. It is normally put after the specification of a range of
lines, but in this case "," indicates the range "from the first line
down to the last one". Now what happens if you type "p" alone?

 p
 - check postgrey hiccup


(notice again the ed(1) prompt above). Well, any command in ed(1)
modifies the "current line address", which is the line any ed(1) command
would implicitely work on if you don't provide any line to it. The
command "p" moves the current address to the last line it printed. So
",p" moved the current address to the last line of the buffer, and the
following "p" just printed the current line (the last one).

Another useful command is "n", which prints lines adding line numbers:

 ,n
 1       TODO LIST
 2       - phlog about ed(1)
 3       - put rubbish bins outside
 4       - make a tea
 5       - check postgrey hiccup


Oh now that's convenient! Guess how you ask ed(1) to print the third
line in your buffer:

 3p
 - put rubbish bins outside


While you can use the special marker "$" to indicate "the last line in
the buffer":

 $n
 5       - check postgrey hiccup


Notice that there is no space between line ranges and the command that
applies to them. I guess you have had enough ed(1)-ting for today, and I
am not in the right mood to start working at this todo-list now. So
let's just save our work for later:

 w
 94


ed(1) prints the number of bytes written in the file TODO.txt. We can
now exit ed(1):

 q
 $

and you are back to the shell prompt. Now you can see your brand-new
TODO list with:

 $ cat TODO.txt
 TODO LIST
 - phlog about ed(1)
 - put rubbish bins outside
 - make a tea
 - check postgrey hiccup
 $

or by using:

 $ printf ",p\n" | ed TODO.txt
 94
 TODO LIST
 - phlog about ed(1)
 - put rubbish bins outside
 - make a tea
 - check postgrey hiccup
 $

;-)

-+-+-+-

ed(1) was included in Unix-V1 (1971) and was used to write the Unix
     kernel and all the programs distributed with Unix at least until
     Unix-V7 (1979)

-+-+-+-

[1] https://www.gnu.org/fun/jokes/ed-msg.html
[2] https://www.tiltedwindmillpress.com/product/ed/
[3] I use a dash "-" to indicate tasks that are still outstanding, a
   plus "+" for tasks I have started working on, and a star "*" for
   completed tasks.