114 points · by cyborgx7 · 10y ago · 88 comments
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.Wrapping text: I used fmt all the time. But you can't pipe text straight through fmt, so I ended up doing this a lot:
3w |fmt>fmt # create a file called fmd in cwd
3r fmt
3d
Which is OK if you have just one line to wrap. If there are newlines in the text you're wrapping, you have to remember the line numbers of the unwrapped text (or use ka, kb) to delete them.
Inserting text in the middle of a line. Either use s// (which is annoying if your inserted text has a lot of slashes) or split the line, add an in between line with the new text, and join.
s/split text/split text\ # <- backslack escapes newline
Then use a or i to insert a line. Then join three lines with j. It gets old after a while.
And then there was my false hope that I learned a universal editor that was everywhere. Except GNU ed is not BSD ed is not OpenBSD ed is not Solaris ed.
Anyhoo, would be fun to read how someone who stuck with it (unlike myself) uses ed.
It's interesting you mention inconsistencies across implementations, even more (I assume) going from BSD to GNU ed which unsurprisingly is almost a superset of features with heretical commands such as yank (!).
BSD tends to stick more to POSIX, so issuing just `t` won't duplicate the current line, which I've grown used to. I also like the paging command `z`, which seems to have been adopted by all major implementations. I'm sure Ken would find all of this bloatware!
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.
> 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.qmu...
Shockingly, the MUD (3kingdoms) is still around, though I suspect they've moved on from ed.
Still found occasional use in tiny little fixes, but never super comfortable using it.
kalleboo@Synology:~$ ed
-sh: ed: command not found
kalleboo@Synology:~$ vi
~ VIM - Vi IMproved
ahSnark aside, it's a very well-written tutorial! I'm sure I'll end up referring to it at some point in the future.
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).
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.
I would have thought there'd be utility in having a very small last-resort utility like ed by default. But then I don't use Vi much (or ed, normally I'm just touching up a file from the command line, so I use nano), so perhaps I'm out of touch.
And then it turned out that a lot of the platforms people run the scripts on, they had to install ed explicitly! Ha. Sorry, Arch/Debian/Fedora/Centos/OpenSUSE users.
If you don't believe me, look how many times 'ed' is installed on this page. :) https://gitlab.com/gitlab-org/gitlab-development-kit/blob/ma...
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.
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.
% 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.) % ed -s filename <<- EOF
ed commands...
EOF % 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!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.
Also by relying on external tools, you have now forced your users down the dependency hell path.
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...
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.
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"
}pacman -S ed
? The only package on the main repos that pulls in ed as a dependency is `rcs`.
I think vi is a perfectly sensible choice as the always-there editor.
Wheezy/gNewSense 4.0 has it on a default install