wiki/articles/ed.md

2.5 KiB

ed

ed is the standard text editor of Unix.

Unlike modern visual text editors, ed is a line oriented editor. This was because ed was the first text editors made for Unix, and back then the only way to interface with a Unix machine was through a teletype. Video terminals were a luxury in that time.

?

Tutorial

Executing ed:

$ ed
$ # Or specifying a file to open:
$ ed file.txt

If you pass a file to ed and if it exists, it will print its size in bytes, and if the file does not exist ed may complain about it (at least on GNU ed). Unless you specify a prompt with the -p flag, you will only get an empty line. Here, ed is waiting for a command. An example basic session is shown below:

a
Hello from the standard text EDitor!
.
w file.txt
37
q

First we enter into file appending mode, in which ed will begin accepting input for writing into the file. To tell ed that we finished typing text, we write a single . on its own line; this returns ed into command mode, then with the w command we save the text to the file file.txt and finally we quit ed by using the q command.

To select a specific line, we simply type its number; ed will print the line and select it for operations such as substitution. For example, here is how we would use the s command:

37
1
Hello from the standard text EDitor!
s/Hello/Hi/
1
Hi from the standard text EDitor!
wq
34

Command summary

  • a: append a line, after the current line
  • i: insert a line, before the current line
  • d: delete a line
  • p: prints the content of the whole buffer.
  • s: substitute text in the current selected line.
  • w: write buffer to the file, optionally accepts a filename if it wasn't specified when launching ed.
  • q: quits ed
  • wq: write and quit
  • !: execute a shell command
  • r !: appends the output of a shell command into the buffer (the space between r and ! is relevant)
  • ...

Commands like a, i, d, p, s and others can be preceded by a line number or range (in the form x,y), with the character , being equivalent to the current line. For example, here's how we can delete multiple lines:

a
extra line
.
a
another extra line
.
,p
Hi from the standard text EDitor!
extra line
another extra line
2,3d
,p
Hi from the standard text EDitor!

If you ever used a vi editor, ed commands should be quite familiar. After all, vi is a visual form of the ex editor (an extension to ed by Berkley).

TODO