10 ms·
Actually using ed (2012)
- tscs37 10y agoI might actually do that, mostly because I really only do quick edits on the console 99% of the time.
- Annatar 10y agoed is perfect in shell scripts: % cat <<- _EOF_ | ed -s filename ed commands... _EOF_ works on all UNIX-like operating systems and it means that temporary files are no longer necessary. (GNU sed -i is an architectural violation of the idea of a stream editor, and it isn't available everywhere.)
- tincholio 10y agoI routinely automate text-mangling tasks with short ex (most of them would probably work in ed, too) scripts. I find it simpler than using sed, most of the time. If you're comfortable using ex commands in vim, then it's almost a seamless transition
- protomyth 10y agoFreeBSD found out it is not perfect since they had to patch patch because it called ed. People figured out how to run arbitrary scripts in a privileged manner.
- Annatar 10y agopatch(1) outputs ed(1) commands. It always worked that way, and it demonstrates the power of the "do one thing and do it well": patch just does the analysis and generates the appropriate editting commands, but the editting is passed on to a program specialized for just that: ed(1). It as also a good showcase for UNIX modularity principle.
- protomyth 10y agoIt's a good demonstration up until it turns into an attack vector. Then it violates the "do one thing well" part.
- 101km 10y agosed -e s/foo/bar/ | sponge filename https://joeyh.name/code/moreutils/ https://joeyh.name/code/moreutils/
- OskarS 10y agoYou can also use the -i flag of sed to edit a file in-place. It leaves a backup file with the original in case you messed up.
- Annatar 10y agoI explicitly wrote: GNU sed -i is an architectural violation of the idea of a stream editor, and it isn't available everywhere. precisely to preclude anyone from suggesting using GNU sed -i.
- Annatar 10y agosponge is found on GNU/Linux, but is not a UNIX standard; when writing shell programs one must never assume GNU/Linux, because, as my ed(1) example shows, it is completely unnecessary and makes the code unportable for no reason whatsoever. Also by relying on external tools, you have now forced your users down the dependency hell path.
- qwertyuiop924 10y agoAssume the environment you're writing scripts for. Most scripts need only be run on one machine, and thus one platform. Most that don't still run only on one platform. If you need a portable shell script, by all means write one, but that's rarely the case.
- Annatar 10y agoAssume you know how to write shell programs which work everywhere without modification; why would you make your program unportable on purpose?
- 10y ago
- kovek 10y agoIt is unclear what this tries to achieve. Could you please explain why this removes the need for temporary files?
- deleted 10y ago[deleted]
- Annatar 10y agoIf you want to perform an in-place edit, and you want to do it in a portable manner (not using GNU sed -i), you'd have to use sed with at least one temporary file: % sed -e 's/something/other/' somefile > /tmp/temp.$$ && mv /tmp/temp.$$ somefile now, using ed (or AWK) and the heredoc with ed commands above, you can mmap(2) the entire file into memory, perform desired editting, and write it all out in one go. No more temporary files!
- falcolas 10y agoSed, when operating on a file, creates a temporary file alongside the target file.
- eriknstr 10y agoUseless use of cat though. You could achieve the same thing by: % ed -s filename <<- EOF ed commands... EOF
- deleted 10y ago[deleted]
- fnj 10y ago> unless you’re using Arch Linux pacman -S ed
- sevensor 10y agoI think the article was trying to say that you're not absolutely guaranteed to have ed on Arch. Although pulling it in as a dependency is almost unavoidable if you're actually trying to do anything, so you have a fair point.
- lucb1e 10y agoMy Debian also doesn't have it, though I can't guarantee not having manually uninstalled it (I also uninstalled nano for example). I think vi is a perfectly sensible choice as the always-there editor.
- OskarS 10y agoWhy did you/would you uninstall ed or nano? It's not like they're taking up a lot of disk space. I mean, I don't ever use them either, but they're not getting in my way or anything.
- kbp 10y agoed is used somewhat commonly by other programs too, so uninstalling it can break things.
- koenigdavidmj 10y agoIf they do then they should have a package manager dependency, or that's a bug with the distribution.
- kbp 10y agoIf you get 100% of your software through the package manager, sure. If not, then manually uninstalling things that have been standard on Unix since version 1 isn't very smart.
- simonh 10y agoed was the first Unix editor I ever learned, back in 1991. The first terminal I was given in my first job didn't support screen control commands so I could only use line oriented commands such as ed. I got so used to it I still use it occasionally to this day just from habit, though I do now mainly use vi. I occasionally use it in scripts using 'here' documents.
- kps 10y agoSimilar here — the system didn't initially support uncooked input. I don't still use it, but my vim idiolect is still heavy on regular expressions.
- tyingq 10y agoMS-DOS had the similarly frustrating line-at-a-time edlin: https://en.wikipedia.org/wiki/Edlin https://en.wikipedia.org/wiki/Edlin
- sevensor 10y agoEdlin was my first text editor! I used it to write BASIC programs for my parents' 286. Fond memories.
- tylerneylon 10y agoI re-wrote ed in C once for fun: https://github.com/tylerneylon/ed2 https://github.com/tylerneylon/ed2 It uses the regex and readline libraries, as well as some container data types (array/map) called cstructs. The main C files sum to around 1,000 lines. It was an interesting exercise in learning some of the pieces of building a text editor.
- kps 10y agoV6 ed (the first in C) was around 1300 lines. http://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/s1/ed.c http://minnie.tuhs.org/cgi-bin/utree.pl?file=V6/usr/source/s... It won't build with a modern compiler, of course.
- breadbox 10y ago> It uses the regex and readline libraries Given that readline is basically a line-oriented version of emacs (or vi, if you set the config), it sounds like you actually wrote a readline editor with an ed-inspired UI.
- mcguire 10y agoThe code for a simple ed-alike is one of the chapters of Software Tools by Kernighan and Plaugher. For entertainment value, here's a version in Haskell (using Parsec to parse regular expressions (!)): https://www.crsr.net/Programming_Languages/SoftwareTools/ch6.html https://www.crsr.net/Programming_Languages/SoftwareTools/ch6...
- cyborgx7 10y agoTo whoever edited the title: I appreciate the addition of the year, forgot to do it when I submitted the story. But I do think the "Actually" is an important part of the title and should be put back in.
- Gruselbauer 10y agoI love OPs tutorials. If you're reading this, you taught me a lot, thank you.
- dwhabcdefg 10y agoIIRC the TRS-80 Model III had a similar single-line editor. Once my little fingers knew all the commands, it was pretty efficient.
- mbrock 10y agoI like ed for simple editing tasks like deleting a range of lines, substituting words, etc. For a while I maintained an HTML blog only with ed, as an experiment... It really forced me to get used to regexps ranges, thinking before I do stuff, etc...
- dgacmu 10y agoI once became surprisingly good at using ed because it was the editor available in a MUD environment (LPMud). I think that was in '92. Shockingly, the MUD (3kingdoms) is still around, though I suspect they've moved on from ed.
- shabble 10y agolikewise, although it rapidly got the boot when I realised there was ftp support and I could just do things locally. Still found occasional use in tiny little fixes, but never super comfortable using it.
- jlebrech 10y agonow if there was a siri for ed we wouldn't need vim, you could just ask it "how do i ..?" and before you know it you're not asking it anymore you're ed'fuing everywhere.
- swiley 10y agoAn ed for siri would be more intieresting. I feel like siri is eventually just going to be the iphone's version of bash.
- blablabla123 10y agoIt's not installed on my Android :(
- kalleboo 10y ago> If you’re using any Unix at all, then ed really will always be there, no matter how old or limited the system. kalleboo@Synology:~$ ed -sh: ed: command not found kalleboo@Synology:~$ vi ~ VIM - Vi IMproved ah Snark aside, it's a very well-written tutorial! I'm sure I'll end up referring to it at some point in the future.
- kps 10y agoPerhaps your system is something Not Unix?
- HCIdivision17 10y ago> Well, unless you’re using Arch Linux. I would love to know if you're using Arch Linux? There's got to be a good story about why it's not there, if not. Might be fun to hunt down if so (I like historical stories about that sort of decision making).
- bananaoomarang 10y agoGoing purely off memory here, but I think the discussion on the mailing list went something like: Why do we include multiple text editors by default? Good point let’s just pick one for the base install. Obvious choice is Vi not Ed because more people are familiar with the former.
- HCIdivision17 10y ago
- mojoe 10y agoEd is the standard text editor. https://www.gnu.org/fun/jokes/ed-msg.en.html https://www.gnu.org/fun/jokes/ed-msg.en.html
- qwertyuiop924 10y agoI've used ed. To write Lisp. Yes, really. It's by no means my favorite editor (You couldn't convince me to use vi - even the original vi, with its excellent support for Lisp: do you really think I'll drop emacs for ed?), but it's usable, and it works.
- yiyus 10y agoed is nice, sam is better: http://man.cat-v.org/plan_9/1/sam http://man.cat-v.org/plan_9/1/sam
- deadpixi 10y agoA version of Sam for X with scalable font support no Plan 9 dependencies: http://GitHub.com/deadpixi/sam http://GitHub.com/deadpixi/sam
- michaelsbradley 10y agoSince bash doesn't provide a prepend operator, I define a shell function for that purpose, making use of ed, without need for a temp/intermediate file: file_prepend () { local ed_cmd="1i" if [ ! -f "$2" ]; then touch "$2" ed_cmd="a" fi printf '%s\n' H "$ed_cmd" "$1" . w | ed -s "$2" }
- dazmax 10y agoIs ed where the slash for regex matching originated or does it go back farther?
- gkya 10y agoYou can use any character instead of / there. I prefer comma as its easier to type IMO. But slash is too and it's quite distinctive and easy to type too.
- kps 10y agoClose; it was one of the Bell Labs versions of QED — https://www.bell-labs.com/usr/dmr/www/qed.html https://www.bell-labs.com/usr/dmr/www/qed.html — I suspect but am not sure that Ken Thompson's CTSS version, that introduced regular expression text editing, used slash. The original Deutsch/Lampson QED used colons or square brackets for searches (fixed strings, not regular expressions) but did use slashes as the substitution delimiter, which may have inspired Thompson to use the same character for both search and replace.
- cylinder714 10y agoDoes QED source code exist anywhere? A senior developer at SCO gave me a copy of the manual years ago but I've never had an opportunity to try it.
- kps 10y agoI'm not aware of any.
- ezequiel-garzon 10y agoHey everyone! I actually use ed almost daily, and at some point I'd like to put up a website devoted to ed oddities. I found in some book that ed was used in the 80's as way to test how fast college students could learn a new technology, under the safe assumption that those students hadn't used ed before. I'm now much more careful when it comes to bookmarking these things, since ed and Google really don't get along, but I lost track of that book or publication. Does anybody know about the study I'm referring to? Any other oddities would be welcome. Thanks in advance.
- noobermin 10y agoed is a god send. Often times, I want to edit a file but don't want to have an editor overwrite the whole screen because I need what was previously printed to edit. When streams aren't enough, ed does the job. Another psychological thing, it's like exiting a room and forgetting what you were supposed to remember in that room. When an editor takes over the whole screen, I feel a little mental context switch that I have to sort of fight (like having to go back into your memory once you leave a room)while ed seems like less of a break in context since it doesn't wipe the screen. That's my feeling at least.
- ezequiel-garzon 10y agoI agree. Ken Thompson put it like this: > I don't want to see the state of the file when I'm editing. In my case at least this is mainly the case for quick edit, though he probably felt this way in general. A full account by George Coulouris about em, a visual enhancement of ed, is more enlightening: > By the way, 'em' stands for 'editor for mortals' - I christened it that after Ken Thompson visited our lab at QMC while I was developing it and said something like: "yeah, I've seen editors like that, but I don't feel a need for them, I don't want to see the state of the file when I'm editing". [1] http://web.archive.org/web/20080103071208/http://www.dcs.qmul.ac.uk/~george/history/ http://web.archive.org/web/20080103071208/http://www.dcs.qmu...
- njt 10y agoI'm not sure if you use tmux, but the way I do it is to open a new pane below the original, edit what I need to, and then close that pane when I'm done. The original screen remains untouched.
- noobermin 10y ago1) Install rlwrap, which wraps readline around any stdin stream. 2) Save this as something (I call it `rled`)somewhere in your path: #!/usr/bin/env bash [ ! -n "$(which rlwrap)" ] && echo "rled: no rlwrap found" >&2 && exit -1 rlwrap ed $@ Now you can use ed with readline which allows line navigation, insertion and history.
- avhon1 10y agoThe first advice (using "h" or "H") doesn't work on Arch's version of ed. It does work on red, which actually a script that calls sed.
- selektah 10y agoI like jed.