Edit text like a pro with ed(1)
In this post I will present counterpoints with easy &
but useful tricks using
commands and, in the Unix philosophy, external tools.
You might ask, why
ed(1)is always* there. Since
ed(1)is required by POSIX, you should find it in any *nix/Linux distribution. Well, it should be there. Sadly, many distros are now dropping
ed(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, while
nanois 2-4x as large,
vi/vimis 3-20x as large, and
emacsclocks in at over 8 megs. Yet
ed(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 how
ed(1)is configured. Yes, there are some subtle differences between GNU
ed(1)but the vast majority is the same.
Getting some help
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
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
add the suffix "n" to your command, for example
Execute an external command from within
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"
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,
lets you use the "r !" command to read the output
of an external program:
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.