Unsorted ed(1) Tips and Tricks
Edit text like a pro with ed(1)
I came across
Ten
unsorted vi/vim tricks, volumes one
&
Ten
unsorted vi/vim tricks, volume two
and thought I'd compile a similar list for
ed(1)
.
In this post I will present counterpoints with easy &
but useful tricks using
ed(1)
commands and, in the Unix philosophy, external tools.
You might ask, why
ed(1)
?
-
ed(1)
is always* there. Sinceed(1)
is required by POSIX, you should find it in any *nix/Linux distribution. Well, it should be there. Sadly, many distros are now droppinged(1)
from their core installations, making it an optional add-on package. -
ed(1)
is small but mighty. On various systems at my disposal,ed(1)
clocks in at between 51k and 183k, whilenano
is 2-4x as large,vi/vim
is 3-20x as large, andemacs
clocks in at over 8 megs. Yeted(1)
provides a lot of functionality in those meager few bytes. -
ed(1)
needs no configuration. You don't have to worry about sitting down at a foreign machine and now knowing howed(1)
is configured. Yes, there are some subtle differences between GNUed(1)
and BSDed(1)
but the vast majority is the same.
Getting some help
ed(1)
is known for its terse "?" reply to any issue. You can
request in-line help with the "h" command for a
bit of a hint. Additionally, the man
page
should cover everything you need to know.
Search and replace
To search, use the "/" or "?" command followed by your search pattern.
To search and replace on the current line, use the "s" command: or, apply the changes over a range:
Show line numbers
To show line numbers in ed(1)
add the suffix "n" to your command, for example
Execute an external command from within
ed(1)
Use the "!" command to execute
Insert an existing file into the current one
In case you need to insert the contents of a file into the
one you're currently writing, use the "r"
command.
Note that ed(1)
lets you know how many bytes
were added to your file.
Display changes performed since last save
This trick uses diff
to display the difference
between the file on disk and the current editing buffer.
Indendent and un-indenting lines
To indent a range of lines, substitute the beginning of the line with either spaces or tabs: To unindent, strip off that number of leading indentation:
Undo & redo
ed(1)
provides one level of undo/redo, using
the "u" command to toggle between them:
Insert the output of an external command
Similar to the insertion of a file above, ed(1)
lets you use the "r !" command to read the output
of an external program:
Record commands
Command history
Since ed(1)
appends to the terminal window
rather than overwriting and redrawing the screen, the entire
editing history for the given ed(1)
session is
available for review.