Actually using Ed
blog.sanctum.geek.nz
blog.sanctum.geek.nz
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.
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://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.
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.
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.
http://www.catonmat.net/blog/ed-unix-text-editor-cheat-sheet...
And here is the cheat sheet itself:
pdf: http://www.catonmat.net/download/ed.text.editor.cheat.sheet....
txt: http://www.catonmat.net/download/ed.unix.text.editor.cheat.s...
doc: http://www.catonmat.net/download/ed.text.editor.cheat.sheet....
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.
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).
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/
An editor in need is an editor indeed!
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.
-e --ed
Output an ed script.
http://en.wikipedia.org/wiki/Diff#Edit_script 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. 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.My bad. I just assumed he skimmed the article as I often do.
Either way, thanks for the article. I've considered my grip on native Unix tools to be a weakness, but there's generally not much good intro material like this.
In my experience, the closest approximation of an editor which will always be there in the real world is sed, not ed.
They did specify Unix.
then all unix systems have 'ed'
http://pubs.opengroup.org/onlinepubs/009695399/utilities/ed....
If arch linux doesn't include it, it's not a 'unix'; it's not even a good "not unix". So the author wouldn't have needed to correct his article but Arch would need to include 'ed' :)
> If arch linux doesn't include it, it's not a 'unix'
...but according to that version of SUS, I'm sure the original unix isn't unix either. In any case, I don't think you can just assume any particular spec when someone says the word 'unix', most people usually just mean *nix-family (and if the spec is relevant they'll probably cite it).
> it's not even a good "not unix". I imagine arch is much closer to compliance if you install everything in the 'core' repository (which is recommended and includes ed). In any case, if lacking ed makes something a "bad not unix", I'm not sure most people mind being wrong ;)
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.
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.
More relevant is that I already know a bunch of tools for doing deletion of line 5. For example: perl -i -ne 'print unless $.==15' . It's more complicated, but it's a smaller number of tools for me to remember.
Actually, I probably would have used mv to a temp file + awk 'NR!=15' + rm temp file. Even knowing perl and python I still use awk pretty often, and it comes to mind much easier than thinking about ed or sed. Plus, I still have the temp file around in case I need to revert a mistake, like if I accidentally typed '51' instead of '15'.
I guess the benefits of 'ed' depend on your line of work though. For a systems administrator, I would make it a job-interview question.
The reason I found it humorous is my difficulty in coming up with strong reasons for someone to start with ed and then transition to vi, while it's easy to come up with reasons to start with vi and then learn ed.
And your best case examples don't come up often in my experience. I usually manually edit more than one line at a time. So it doesn't seem like a very pressing reason.
Out of curiosity, I looked for what sys admin jobs call for. "Significant experience in the use of at least one Unix-based editor (e.g., ed, vi, Emacs, pico)", "Can edit files using more than one editor", "Use vi editor extensively", "Regardless if you use joe, pico, emacs or MS Word for your daily editing, those will not be available in a rescue system and vi is different." Most fall into the vi camp, many only want a (common) editor, and only a handful say "ed - it's the unix editor!", and then only jokingly.
Oh! I almost forgot to mention. I used to use BSD Mail, and at the start I used the default editor, which was 'ed'.
As to what sysadmin jobs call for, having knowledge of 'ed' is not something I would put in the requirements, but it is something I would ask during an interview as it would hint at knowledge of the myriad of obscure tools that unix has, and/or having tackled delicate problems that would have required a fallback to 'ed' in the past.
As such we're mostly in agreement I think. I agree there are no compelling reasons for learning 'ed' as your primary text-editor. But there are compelling reasons against not having a working knowledge of 'ed', although it depends to a large extent on your field of endeavour.
"Well, it's terse, but let's see if we can't make it laconic"
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?
a
I'm with you!
.
quit
?
exit
?
q
?
qWhen 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.
Ed is as essential to a Unix system as cat or grep.
It also appears to be part of POSIX.
I imagine though that ex would be the more capable alternative, and since vi is also ubiquitous, I guess that makes ex ubiquitous too. So I can see your point.
Even if you prefer vi for interactive edits, I still prefer ed for small edits.
Then there are times when your terminal is borken (or as somebody mentioned, perhaps even your keyboard is broken), this still happens more often than you think.
Recently I had to use some ajax-term thing which was just awful and completely unable to run vi properly for who knows what reason, anything that depends on curses can not be considered reliable.
But more importantly it is the standard and portable tool for scripting the editing of files.
If your terminal is broken, there are plenty of ways (reset, stty sane, whatever) to fix that. I'm also not sure why having an extra binary on my system is a good safety guard against a broken keyboard.
The ajax term case is valid I guess, though I'm not sure how common that is. I was in a case like that a year or so ago and ended up using cat and sed to get the thing back on the network.
Ed reads the entire file. If you need to move back and forth through the file, or move lines from one place to another, or reprocess the same line repeatedly, it can be far more convenient than a scripting language.
I appreciate historical respect for the tool but this falls short of showing it to be indispensable to everybody.
Like burnt in from the 80's.
For better or worse, in some of today's base Linux distributions, you might find ed missing. You will almost always find sed though.
$ sed '$G;1h;1!H;$!d' <<<foo | hd
00000000 66 6f 6f 0a 0a |foo..|
00000005
$It's great for quick fixes and one-offs, but having to engineer with it is rather unpalatable.
There are some vi-like mud editors out there, but they were always wonky when I tried them, and caused more problems than they were worth (weird screen redraws, keys stopped working randomly, etc.). That said, I believe Dead Souls mudlib[1] may ship with one now, but I haven't looked at the lib in a few years, so I can't say for sure.
EDIT: link to dead souls FAQ entry on this topic: http://dead-souls.net/ds-admin-faq.html#144
http://videoteco.sourceforge.net/
And the manual:
http://www.copters.com/teco.html
More information:
Let's look at a typical novice's session with the mighty ed:
golem> ed
?
help
?
?
?
quit
?
exit
?
bye
?
hello?
?
eat flaming death
?
^C
?
^C
?
^D
?
---
Note the consistent user interface and error reportage. Ed is
generous enough to flag errors, yet prudent enough not to overwhelm
the novice with verbosity.