💾 Archived View for gem.splatt9990.com › 20210624-Editing_with_Ed.gmi captured on 2022-06-11 at 20:30:16. Gemini links have been rewritten to link to archived content

View Raw

More Information

⬅️ Previous capture (2021-12-05)

-=-=-=-=-=-=-

Editing with ed

So I've recently been playing around with the 'ed' editor, the great grandaddy of all Unix editors. Why? An excellent question to be fair. Ed is generally considered to be old and cold and so very full of mold by most programmers (if they're even aware of its existence.) It entered into existence during a time period where interacting with a computer either involved using a specialized typewriter to literally punch holes in a card or dialing (and yes, I do actually mean dialing, ed predates touch tone phones) a telephone and setting on top of a different specialized typewriter called a teletype (tty for short). Ed only concerned the second group as they were the only ones that were interacting with the computer in the same sense as we think of it.

If you're not familiar with teletypes or typewriters in general, they work by stamping ink on to paper. As such, they can't really "update the display" (I suppose you could try to use some White Out, but that's going go get expensive, and smelly, fast. Do kids these days even know what White out is? Oh well, aside over.) Because of that, ed is designed to operate entirely on lines.

If you've never had the displeasure of dealing with dial up internet, that was the terrible screeching sounds you may have heard being associated with the 90's and early 2000's . Believe it or not, that was "fast" compared to the type of connections that ed was designed to work on. Dial up from the 90's generally was 56k. That's not 56 kilobytes (or kibibytes if you're pedantic) that's 56k bits. Connections during the 60's and 70's were much slower. Therefore, ed was designed to work with very little feedback to the user (this also used less paper and ink.)

What does all that mean? It means that ed is a very old and very terse editor. You can't actually see the document you're working on unless you specifically use the command to do so. There's no built in help and it doesn't even tell you what went wrong if a command fails. It just prints a '?'. You can see what went wrong by pressing 'h' though. Why would anyone want to use such an editor? Especially when much more featureful editors now exist and we don't use teletypes to program.

You don't have to be crazy, but it helps

My personality is such that I get bored pretty easily with things that I already know about. Because of that, I tend to seek out new (or in this case *very* old) things to learn. So despite already being very proficient with both vim and emacs (of which, emacs is *clearly* superior [don't @ me]) I'm sitting here learning how to use an editor that's been obsolete since before I was born.

So what have I learned so far? Ed isn't all that bad for writing prose. I wouldn't want to use it as my daily driver editor though. Ed has one advantage over visual editors like vim and emacs. You can't constantly see what you've already typed. Because of that, the editing experience is somewhat FreeWrite[a]-esque. I have an issue where if I can see the text I've written, part of my brain is *always* trying to edit it. This makes me a very slow writer. Drafting is definitely a skill that I need to work on as a writer. Ed helps with this as generally, I can only see the lines of the paragraph I'm currently writing. This, so far at least, is helping my brain to not obsess over the details of what I'm writing and just get text out on to the page (or screen in this case.)

[a] FreeWrite

Another "benefit" is that line editing in Ed is non-existant. Whereas with vim or Emacs (and probably even ex, ed's direct successor) you can go back over the lines you're inserting to edit them in case you notice something wrong, that's actually impossible in ed. This is a benefit to me for the aforementioned constant editing drive that my brain has. Since the only way to fix a problem on a line in ed is to backspace back up to it, deleting everything that you typed after it and forcing you to re-type it. This means that if I notice an error in my typing or something I'd like to change, I either have to wait until I'm done with my current thought (i.e. not interrupt my current flow) or backspace all the way back to the issue. More or less, I'm pitting my perfectionist tendencies against my inherent laziness (part of the reason this post is dated as June 24th but won't actually be published until well into July...)

Ed also is about the most distraction free writing experience possible with a computer. There's no UI. You can't mess around with various options. There's no configurations to speak of. There's simply a blank prompt (and I do mean blank, by default ed's prompt is just an empty line.) There's nothing to do in ed except start writing.

It's also relatively easy to use on a mobile phone unlike pretty much every other terminal based editor with termux. Since all the commands are only a few characters at most, the lack of a proper keyboard isn't (as much of) a hindrance as it would be with editors like vim or emacs which rely *heavily* on keyboards to be efficient in. In fact, substantial portions of this post have been written on my phone due to this fact.

Another positive for ed is that it's so minimal that it's not hard to keep all the commands in your head.

How does it work

In fact, there's only a few to remember. If you're familiar with vi/m, you probably already know most of them (except they work a bit differently). Full documentation for ed commands can be found in either the manpage or info docs for your version of ed. You can also find the documentation for GNU ed (likely the version you have) here:

GNU ed manual (http)

As for the basics, the most used commands are:

1. i => insert command, optionally takes a line number to insert after. After entering this on a line by itself, you'll enter insert mode (similar to vi's) and everything you type will be entered into an insert buffer. To exit, enter a '.' on a line by itself. When you do that, everything you type since the original i command will be inserted into the buffer at the end of the file or after the line specified.

This will result in the buffer containing: "This is an example text!" with a newline at the end.

2. a => similar to i but will append to the line in question rather than inserting after it. Also optionally allows a line number to append to instead of the current line.

3. d => deletes a line from the file and copies it to the cut buffer (allowing you to paste (yank) it later). Accepts a line or range of lines (specified with a ',' between them.)

4. w => write the file to disk. Optionally takes a file name/path to write to. If you opened ed by specifying a file (i.e. with something like, `ed file.txt`) it will write back to that file by default.

5. s/RE/replacement/ => edit the line using regular expressions. By default, it will only change the first instance of the regular expression that matches per line in the address range. You can change that by adding a 'g' flag after the last '/' of the substitution. This will make the substitution apply to all instances in the line that match. For reasons that should be obvious, learning regular expressions is left as an exercise for reader.

6. q => the quit command. Exits the editor. Will fail initially if the file you're editing has unsaved changes (so be aware of that if the command fails.) If you type 'q' again after getting a failure (signified by ed printing '?' to your terminal) it will simply exit without saving changes.

7. h => the "help" command. "Help" is in quotes as it's usually not super useful as the command will only output something after a failed command (again signified by ed printing '?') and will then give a very terse error message. It also only shows the error message for the last error. Typing another command which fails will overwrite the error message.

8. p => Print command. Prints the current line or a line or line range if specified.

9. n => Numbered print. Works exactly print but also prints line numbers for the text printed which is generally more useful.

That's it for the basics. I would encourage you to read the documentation if you're interested in learning more as they're really not that long (~2-3 pages) and will give you many more useful (if a bit niche) commands.

Also useful to know, typing a ',' as a line range is shorthand for the entire file so something like ',n' will helpfully print the entire file, including line numbers to your terminal. You can also use regular expressions to address line ranges i.e. `/re1/,/re2/p` would print everything between the first match (re1) and the second match (re2).

P.S. because I'm crazy, this entire post was written and edited exclusively with ed.