On using ed(1)



My favorite editor is {gnu ed(1)}, a line editor released in 1969. It’s still actively maintained, currently at version 1.17. It has a small set of commands that can be very effectively combined. It is—in my opinion—the best small editor in the absence of a graphical environment.

I wrote my PhD thesis with ed(1). These days, I only use ed(1) on NERSC and other remote machines and prefer {gnu emacs} for everything else.

I am not really a {software minimalist}. I am also not interested in {sam}, {(neo)vim}, or other more obscure text editors.

General usage

The general usage of ed(1) is very terse and relies on regular expression operators, which are applied linewise.

For example, to change arbitrary text inside brackets on a given line, one can execute s/(.*)/(reg). Similarly, we can put a blank line after lines matching a pattern (g/reg/s/$/\), trim leading whitespace (s/^[ ]*/), or trailing whitespace (s/[ ]*$/).

ed(1) has some special operators. For example, \( and \) can be used to capture regex. The command g/canon/s/\(canon\/\)/\1index.html, where '\1' evaluates to 'canon/', substitutes canon with canon/index.html throughout the text. The operator '%' evaluates to the current file name. You can type !cc % to compile the current file.

ssh support

Remote editing via ssh is possible:

      r !ssh user@host "cat /path/to/local/file"
      // make some editing
      w !ssh user@host "cat > /path/to/remote/file"

External tools

ed(1) can utilize other command line tools for different purposes. It can use fmt to break lines at 72 chars, use e !fmt -w72 %, which is straight forward. A more complicated use-case is formatting a selection. This can be achieved by combining sed with fmt as r !sed -n '<n>,<m>p' % | fmt -w 72, where <n> and <m> are line numbers.


The easiest way to script ed is using here scripts:

      ed -s file <<< $'H\n<ed commands>\nw'

Here, H lets ed print out help messages.


If you need to work with multiple files, {gnu emacs} is a nice editor with a graphical user interface.


{The manual} is a good place to start and to look things up. A soft introduction to ed(1) can be found in the excellent article {Actually using ed} by T. Ryder. Examples for scripting ed(1) are given in {Can you use ed as a replacement for vim, grep & sed?} by R. Elder.

Last modified: Thu May 20 16:18:20 PDT 2021