ed(1) is The Right Tool (II)
==================================
Let's get on with our mission of learning the basics of ed(1), the
standard text editor of the unix environment.
In the first installment of this mini-series of phlogs [1] we saw a few
basic commands to add some lines ("a"), print lines ("p", "n") and save
a file after you are done ("w"). Using those commands we were able to
compile a simple TODO list:
$ ed TODO.txt
94
,n
1 TODO LIST
2 - phlog about ed(1)
3 - put rubbish bins outside
4 - make a tea
5 - check postgrey hiccup
Now imagine that we want to insert a new item (buy some tea) just before
the line that reminds us to make a tea. We will use the command "i" (for
"insert"):
4i
- buy some pea
.
,n
1 TODO LIST
2 - phlog about ed(1)
3 - put rubbish bins outside
4 - buy some pea
5 - make a tea
6 - check postgrey hiccup
Easy, right? While "a" is used to "append" lines after a given line, "i"
is instead used to "insert" lines before a given line. As with "a", the
"i" commands is ended by a line containing only a ".". If you have ever
used vi(1) or one of its many clones, you know that "i" is also the
command needed to move to "insert" mode in vi(1). Well, this is not a
coincidence.
Notice that when we inserted the new line we also introduced a typo: we
definitely want to buy some "tea" (not a "pea"). We can "change" the
content of a line using the command "c":
4c
- buy some tea
.
,n
1 TODO LIST
2 - phlog about ed(1)
3 - put rubbish bins outside
4 - buy some tea
5 - make a tea
6 - check postgrey hiccup
Notice that ed(1) acts consistently: the three commands used to append,
insert, or change lines are all terminated by a line containing a single
".". Actually, the command "c" in general allows us to replace one line
(or a range of lines) with another line (or with a range of lines). So
if you prefer coffee instead of tea you might use:
4,5c
- buy some coffee
- buy sugar
- make a coffee
.
,n
1 TODO LIST
2 - phlog about ed(1)
3 - put rubbish bins outside
4 - buy some coffee
5 - buy sugar
6 - make a coffee
7 - check postgrey hiccup
Well, it's evident that having to retype a line just to correct a typo
is not exactly what one would call an efficient editing session. Indeed,
the command "c" is mostly used when the content of a range of lines has
to be substantially modified. Luckily, ed(1) has another command ("s",
for "substitute") which allows to easily correct typos, as well as to
perform complicate substitutions across any range of lines. That will be
the focus of the next phlog in this series :)
-+-+-+-
[1]
gopher://republic.circumlunar.space/0/~katolaz/phlog/20190505_ed_lists.txt