top | item 4120513

Actually using Ed

139 points| kryptiskt | 13 years ago |blog.sanctum.geek.nz | reply

89 comments

order
[+] chernevik|13 years ago|reply
I'm glad ed is there. I'm glad articles like this are there. But there is no way I'll have retained that article in the moment my terminal has borked and I actually need ed. I wish that something like this available in the man pages, or something like them, or at least referred to in them.

Because maybe I'm not clever enough but I don't find the man page articles to be very good introductions. As reminders of syntax and options and functionality they're great, but I don't find them useful as orientations to how the various utilities work. They are short on examples and demonstrations, which are often the quickest orientations to the basic operation of a tool.

The info tool is sometimes better but not so frequently I'm accustomed to using it. On Ubuntu 'info ed' actually looks pretty good from an example standpoint. But 'info awk' looks like a copy of the man page. And 'info vi' offers a discussion of the emacs vi emulator, about which vi enthusiasts can only mutter "well played".

It would be nice if there were a 'tutorial' command (or 'dontpanic' or 'towel') which gave a brief orientation to how these tools actually worked, with examples and demos, and then some lead into resources on the host (man pages, info, help available within the particular tool) and on the web and elsewhere.

[+] luriel|13 years ago|reply
Linux man pages are notoriously verbose, and often obtuse and incomplete, this is in great part due to the extra complexity of GNU tools and their preference for the infamous info documentation system.

OpenBSD has probably the best Unix manual pages:

http://www.openbsd.org/cgi-bin/man.cgi?query=ed

And Plan 9 has a manual that is a pleasure to read, this is helped in part because the commands themselves have been cleaned up and simplified in many cases:

http://man.cat-v.org/p9p/1/ed

[+] jff|13 years ago|reply
Info pages are utterly useless--I don't think I've ever had anything useful from there. If I've screwed my system up so badly I need to use ed, even "man" might not necessarily work (these days GNU commands are just so damn clever). Best thing is usually to grab another computer or your smartphone and start Googling.
[+] sashahart|13 years ago|reply
On many systems you should be able to unbork the terminal with 'reset' or 'tput reset'. Then you do not need ed ;)
[+] sunkencity|13 years ago|reply
My survival kit for linux man pages is "/" for search and then "n" until option is found.
[+] bodyfour|13 years ago|reply
I think a lot of C programmers would find reading the source to the V6 ed interesting:

  http://www.tuhs.org/Archive/PDP-11/Trees/V6/usr/source/s1/ed.c
1333 lines of C, basically no library use at all (no printf, not even malloc -- the only thing dynamically allocated is the document, and it uses sbrk() to manage the memory itself), compiles down to about 6K on a PDP-11. And that includes a regex engine!

It contains a lot of 1970 C-isms that won't work today ("=+" instead of "+=" for example) but an experienced C programmer shouldn't have a hard time following it.

As a bonus, look at the commented-out getpid() implementation at the bottom of the file.

[+] ralph|13 years ago|reply
It probably also has vestiges of the assembler version in its structure and symbols. troff(1) certainly has; I had trouble groking one particular function's logic and asked Kernighan. He kindly replied and said that one had always troubled him too and was just a transliteration from Ossanna's assembler. Glad it wasn't just me. :-)
[+] dalke|13 years ago|reply
I remember doing a=+1 in K&R days under IRIX and getting a warning that "=+" was deprecated. I had just started with Unix then, and had to get someone else to explain the source of that message.

How does the "goto errlab" work? It looks like it calls errfunc() then gets to the reset(), which doesn't return. Does setexit() determine the entry point for a reset?

This really is a bare bones system. An insert at the start shifts every character to the right by one, and memory growth is only 1024 bytes at a time. This leads to quadratic times. I wonder if that performance was noticeable on the PDP-11 when editing a large-for-the-time file.

[+] gelisam|13 years ago|reply
I used to work at a place where their mission critical software was running on ancient HP-UX boxes which we feared to turn off because we didn't know if they would turn back on. My job was to port this software to modern Ubuntu VMs. Once I was done, we had a celebration in which we turned off the HP-UX boxes forever... except of course there was a bug in my version, so we soon had to turn them back on while I worked on a fix.

To everybody's surprise, the machines did turn back on, but they couldn't connect to the network because a few configuration files were wrong. To our dismay, after we tried to use Vi to edit the first one, we discovered that the ESC key did not work anymore, and we were stuck in edit mode forever! We had to reboot the machine, which takes about an hour on these old machines.

And then of course I configured the files using Ed and saved the day! Surprisingly, I was the youngest programmer there and I was the only one who knew how to use it.

[+] pkrumins|13 years ago|reply
[+] tyrmored|13 years ago|reply
Awesome stuff! I love your blog, by the way. It gives me a standard to which to aspire.
[+] jbuzbee|13 years ago|reply
I guess I'll date myself by saying that when I started studying CS, I used ed and thought nothing of writing programming assignments with it. It seemed totally natural. I recall a typical sequence of writing a few lines and then printing out a block of lines to the screen to check. Then jumping somewhere else printing out a block of lines, editing some more etc. I recall hearing about vi and thinking it was overly complex and obviously wouldn't have worked on the hard-copy terminals (really, hard-copy terminals - no screen!) that were scattered about the lab. I don't think I could have conceived of eclipse...
[+] cturner|13 years ago|reply
I use ed frequently for writing quick scripts. Its edge is that you can use it while retaining visual history of the prompt.

Though, you could instead just redirect cat to a file and then do any tidy-up in your usual editor.

It's also sometimes useful when you're in a remote system that is behaving strangely due to screwed-up terminal definitions or some other catastrophe.

I'd be interested to read a sam tutorial focussed on using just the console version of the tool (i.e. not using the windowing system). I understand it takes a lot of ed's features and beefs them up.

[+] ralph|13 years ago|reply
Yes, I'm a user of ed regularly as well when I would like to mingle interacting with it with my other before and after command line use. If you use vim then `:set t_ti= t_te=' may be of interest to stop it switching to the terminal's other buffer and back; that way on exiting the file's content remains to copy-and-paste from.

The article was OK. I'm not sure it mentioned addressing line 0 for the start of the file, and comma is more common than % for all lines. ed(1) is brief and lots can be learned from it.

As for structural regular expressions as used in sam(1), see Rob Pike's paper. http://doc.cat-v.org/bell_labs/structural_regexps/

[+] luriel|13 years ago|reply
A Sam language tutorial by Rob Pike:

http://doc.cat-v.org/bell_labs/sam_lang_tutorial/

A cool thing about sam is that it has a client-server design, so you can run the GUI on your local system and still edit stuff remotely. This works very well even over very high latency connections.

See the -r flag: http://man.cat-v.org/p9p/1/sam

I still use ed for any short/small edits, it is hard to beat.

It works even better if you have a terminal like in rio/9term where you can edit/send history very easily.

cat also works quite well in such terminals thanks to 'hold mode' (cat > foo, press Esc, edit your text freely, press Esc again and ^D).

[+] wglb|13 years ago|reply
I do so as well. Most often it is sudo ed /etc/hosts.
[+] AaronBBrown|13 years ago|reply
Having been a user of *NIX for over 20 years and a professional Systems Administrator for over 10, I have never once had a reason to use ed. Even the smallest embedded environments usually have some variant of vi available. The only time that I've needed to use ed was edlin in the dark days of early MS-DOS, which is very similar. This is borderline masochistic.
[+] swa14|13 years ago|reply
It's not whether 'vi' is available (it usually is), but whether it is behaving properly. Situations do occur where any fullscreen editor will fail and where 'ed' will work as expected. And at such moments, even though you don't know the full extent of the power of 'ed' you will be happy to know how to change a line and save the config file (and actually quit 'ed'; that's harder than it sounds)

An editor in need is an editor indeed!

[+] bodyfour|13 years ago|reply
You narrowly missed the days when it was useful. When I started regularly sysadmining in the late 80s/early 90s (after being a mere user for the a few years) I found I had to learn ed. /usr was always a separate filesystem so any time there was a problem booting you didn't have vi available. If you didn't have at least rudimentary ed skills fixing the simplest problems would be rough.

In linux at least the separate /usr never seemed to get that popular. If you can mount the root filesystem you've got basically the whole OS at your disposal.

I will say that learning ed (or, better yet, ex) well does make you a more efficient vi user. When all you have is command-mode you get very adept at using it. It's actually sort of fun, too. Just hit "Q" in vim some time and see how you do with only ex commands.

[+] brazzy|13 years ago|reply
I own a Unix introduction book from the early 80s (I think) that describes a customized/extended version of ed as the author's choice of text editor. vi is mentioned as "this newfangled editor that has more features than anyone needs and is such an incredible resource hog that it leaves barely any memory for the actual document on a large PDP-11".
[+] ralph|13 years ago|reply
DOS's edlin is a very crude line editor, unlike ed(1) which has regular expressions, the g// command, etc.
[+] exch|13 years ago|reply

    If you’re using any Unix at all, then ed really will always be there,
    no matter how old or limited the system.
So as it turns out, my Arch Linux setup does not actually have ed.
[+] mhurron|13 years ago|reply
> Arch Linux setup does not actually have ed.

They did specify Unix.

[+] cliffbean|13 years ago|reply
My OpenWRT box does not have ed either.

In my experience, the closest approximation of an editor which will always be there in the real world is sed, not ed.

[+] bmelton|13 years ago|reply

  If you’re using any Unix at all, then ed really 
  will always be there, no matter how old or limited 
  the system. Well, unless you use Arch Linux, anyway.
[+] lubutu|13 years ago|reply
If anyone's interested, a while back I put together a table of commands available in various versions of ed, from 1st edition UNIX up to POSIX, as well as sam. It's largely academic, but I did find it interesting how features crept in.

http://lubutu.com/misc/ed-languages

[+] dalke|13 years ago|reply
That was a good explanation. I hope never to have to use ed.
[+] swa14|13 years ago|reply
Even today 'ed' has practical uses apart from being the default emergency backup of the backup editor.

One situation where I always use 'ed' is when I reinstall a machine (with the same ip) and ssh tells me the keys don't match together with the line number in the file known_hosts. It's easy as: #ed ~/.ssh/known_hosts 15d wq

(assuming the mismatching line was 15). Only sed would be faster if it wasn't for the time spent always looking up the "-i" (edit in place) flag, so I just use ed.

Also knowing a little 'ed' will make working with 'vi' easier, assuming 'vi' is not your usual editor.

[+] wazoox|13 years ago|reply
I had to use it more than once, typically when running on a hosed system, typically from within some miniroot/initrd/ramfs when the machine wouldn't boot and no full screen editor would run because of missing termcaps. However, cat is actually easier to use as a quick emergency editor (if you know how to use ^W,^U,^D and friends). Unless you're stuck using an ASR-33 :)
[+] drostie|13 years ago|reply
You may occasionally want to use its little brother, `sed`, which has substantially similar syntax. It is often the case in Unix programming that you have a pipe full of text data and you would like to transform every line of it; `sed` allows you to write these transforms into one line of instructions on the console line.

So it may sound crazy that you have all of these terse commands, but just imagine that you're trying to write a one-line program to reformat text documents and it suddenly makes a bit more sense.

[+] tyrmored|13 years ago|reply
That's the usual attitude, I think. "I'm glad I know how to use it now, but I hope I never have to."
[+] robertskmiles|13 years ago|reply
I love that they felt the need for ed to have a "quiet mode".

"Well, it's terse, but let's see if we can't make it laconic"

[+] sgt|13 years ago|reply
Monday is a "Ride to work day" for motorcyclists. I think this same thinking should be applied to ed!

Think of it: One day, every year, there should be a "Use ed (the standard editor) day". On that day, all your text editing must happen with 'ed'. Who's with me?

[+] tyrmored|13 years ago|reply

    a
    I'm with you!
    .
    quit
    ?
    exit
    ?
    q
    ?
    q
[+] RexRollman|13 years ago|reply
"ed really will always be there"

When I set out to learn ed, I was surprised that it was not included by default on my Arch Linux install. It was easy enough to add via Pacman but it still surprised me.

[+] luriel|13 years ago|reply
It is a very sad and distressing trend that some Linux distros don't include ed in their base systems.

Ed is as essential to a Unix system as cat or grep.

[+] gordonguthrie|13 years ago|reply
The only commands I can use in Vi/Vim are the ones I remember from ed, lol.

Like burnt in from the 80's.

[+] ythapa|13 years ago|reply
I think sed pretty much displaced ed. I still use ed but sed offers the same functionality and it's easier to script. One exception is the j command for joining lines.

For better or worse, in some of today's base Linux distributions, you might find ed missing. You will almost always find sed though.

[+] ralph|13 years ago|reply
sed is great but it's a stream editor; try moving the last line to the start of the file.
[+] mpd|13 years ago|reply
I was a MUD coder in a past life (and sort of am still); learning ed is essential for that environment. I'm glad I learned to use it, and gladder still I don't have to use it every day.

It's great for quick fixes and one-offs, but having to engineer with it is rather unpalatable.

[+] Twisol|13 years ago|reply
I'm curious, why is it essential? I'm a bit of a MUD enthusiast myself, and I've never had to use ed. On the other hand, my favorite MUD's long-form editor (like for newsposts and journal pages) has an ed-like design.
[+] zygotic|13 years ago|reply
Honestly - punch cards are brill and don't use any of that new fangled electric stuff.