Skip to main content

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. 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 nano is 2-4x as large, vi/vim is 3-20x as large, and emacs clocks 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) and BSD ed(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.

s/oof/bar/
?
h
No match

Search and replace

To search, use the "/" or "?" command followed by your search pattern.

/pattern/n
18 this line contains "pattern"

To search and replace on the current line, use the "s" command:

s/foo/bar/g
or, apply the changes over a range:
4,18s/foo/bar/g

Show line numbers

To show line numbers in ed(1) add the suffix "n" to your command, for example

,s/foo/bar/n
g/pattern/n
10zn

Execute an external command from within ed(1)

Use the "!" command to execute

!ls
file.txt
!

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.

r file.txt
1625
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.

w !diff -u file.txt -

Indendent and un-indenting lines

To indent a range of lines, substitute the beginning of the line with either spaces or tabs:

12,18s/^/    /
.,+4s/^/{tab}/
To unindent, strip off that number of leading indentation:
12,18s/^    //
12,18s/^ \{4\}//
.,+4s/^{tab}//

Undo & redo

ed(1) provides one level of undo/redo, using the "u" command to toggle between them:

$ ed
1a
hello
world
.
2d
,n
1 hello
u
,n
1 hello
2 world
u
,n
1 hello
Q

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:

r !ls -l

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.