Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Text Processing

Linux ed command

Reference for the Linux ed line editor: addresses, the p, d, s, a, i, c, w and q commands, scripting edits from a here-document, and how ed relates to sed and vi.

ed is the original Unix text editor, from 1971. It has no screen: you type commands, it prints results only when asked. Everything you know from sed (addresses such as 3,5, the s/old/new/ command, /regex/ line selection) and from the colon commands in vi (:w, :q, :1,$s/a/b/g) comes straight from ed.

Today it is rarely used interactively, but it is a good scripting tool: it edits a file in place and can use several commands in sequence, including moving and joining lines, which sed -i handles less naturally. It is also what you get on a minimal rescue system when nothing else is installed.

ed works on a buffer of lines. You select lines with an address, then give a one-letter command. The "current line" is the default address.

Terminal
ed file.txt

ed is not installed on the runner behind the live examples on this site, so its examples are read-only. The runnable example uses sed, which accepts the same address and substitute syntax, to show the equivalent edits.

Try it

Read-only: an ed script that edits a file in place. The command stream is read from a here-document, and w writes the file and q quits.

Terminal
printf 'one\ntwo\nthree\nfour\n' > list.txt

ed -s list.txt <<'EOF'
2d
$a
five
.
1,$s/o/0/g
w
q
EOF
cat list.txt

The lines in the buffer after 2d are one, three, four. Then $a appends five after the last line, and 1,$s/o/0/g replaces every o with 0. The final file is 0ne, three, f0ur, five. The command . on a line by itself ends text entry.

Bash The same edits with sed (runnable)

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
ed [-s] [-p PROMPT] [FILE]
Option Description
-s Quiet mode: suppress byte counts and the ! diagnostics, the usual choice for scripts.
-p STRING Show a prompt (such as *) while waiting for commands.
FILE Read this file into the buffer first.

Addresses: N a line number, . the current line, $ the last line, +N/-N relative, /re/ the next line matching a regex, ?re? the previous match, 1,$ or % all lines, 'x a line marked with kx.

Command Description
p, n, l Print lines (n with numbers, l showing invisible characters).
a, i, c Append after, insert before, or change the addressed lines; text follows and ends with a line containing only ..
d Delete the addressed lines.
s/re/text/flags Substitute; flags g and a number work like sed.
m, t Move or copy lines to an address (2m$ moves line 2 to the end).
j Join the addressed lines.
g/re/cmd Run a command on every line matching the regex (the origin of grep: g/re/p).
v/re/cmd The same for lines that do not match.
w [FILE], q, Q Write, quit (refuses if there are unsaved changes), quit without checking.
r FILE, !cmd Read a file into the buffer; run a shell command.
u Undo the last change.

Returns: 0 on a normal exit; a non-zero status if a command failed while reading a script (GNU ed exits with 1 after an error in script mode).

Usage notes

  • No prompt, one-character errors. When a command fails ed prints ? and nothing else. Type h to see the reason.
  • Nothing is written until you type w. q with unsaved changes prints ? once; a second q quits and discards them.
  • Scripting. printf '%s\n' '1,$s/a/b/g' w q | ed -s file or a here-document works. Quote the delimiter (<<'EOF') so the shell does not expand $.
  • Text-entry mode is the usual beginner trap: after a, i or c, every line is data until a lone .. A missing dot swallows the rest of the script.
  • ed versus sed. sed streams, ed buffers the whole file, so ed can move lines backwards and use g with several commands; sed -i is simpler for one-line substitutions.
  • GNU ed is a separate package on most distributions; the ed in busybox supports only a subset.
  • No terminal needed. Scripts that modify configuration files sometimes use ed (or ex) because, unlike vi, it works with plain standard input and output.
  • Safety. ed edits the file you name; make a backup first (cp file file.bak), as ed has no backup option.