Further Adventures in Using the Ed Editor

I've been poking around the internet in my continuing quest to get better at (and better understand) some of the things that can be done with the ed editor, and found several interesting tips and tricks in the comments of this YouTube video and at the ed(1) conf mastodon feed. Thanks to both for the information and enlightenment.

One of the superpowers of ed is the ability to run shell commands from within the program, letting the user modify and manipulate the buffer on the fly. In fact, this superpower allows ed to be Turing-complete. But most of us mere mortals need only less cutting-edge powers when it comes to ed use. Say you've opened a file and realized (before you modify the buffer) that you want to backup the original. There are two ways to do this:

f [filename].bak w f [original filename]

A quicker way to do this may be:

!cp % %.bak

Or say you want to check and see what changes you've made in the buffer since the last time you saved your file. (You do use "w" to write your changes periodically so that they're not lost, don't you?) Try this:

w !diff -u % -

Want a little bit of grep-like context for a search term? Try:

g/[regular expression]/-1,+1n

Is your screen getting cluttered with the results of searches and substitutions? How about clearing the screen and re-printing your file from the beginning?

!clear ,n

If you want to get really fancy, you can use the trick "LoomisTook" has in his YouTube video and write a shell script called something like "cl" or "clr" which clears the screen, then uses an echo or printf command to put a 80-character-wide ruler at the top.

Or you can just forget the whole ruler-thing and make sure your text is formatted to 80-characters wide by issuing this command when you're done modifying the buffer:

!fold -s -w80 %

If you set a mark (by typing kx or ka or k-and-any-letter-of-the-alphabet) before you clear the screen, you can just print from that point forward in the text with the following command:

'x,$p (or n or l)

Ed is a line editor: it edits an entire line at one time, making word-changes within lines a bit of work. However, adding text to the end (or beginning) of a line isn't hard:

[line number]s/$/[things which you want to add to the end of the line, which can include line breaks if you end each subsequent line with a "\"] [line number]s/\</[things which you want to add before the first word of the line. Use "^" if you want to add to the very beginning of the line]

If you want to change text *within* a line, you can use the substitute command. To add words within a line, one easy way is to use "&" which represents the search term within the substitution. For example:

[line number]s/this and/& that/ [line number]s/read/&ing/

The first substitution would replace "this and" with "this and that." The second would replace "read" with "reading."

I'm still not convinced that the ed editor would work well for some lengthy writing, such as a novel; but for scripts and even more involved things such as this web-page, it's more than adequate.


~thumos thumos@tilde.club April 2024