ed

Created: 19 Sep 2025
Updated: 08 Jan 2026
wiki english
gnu manual

Why?

  • being part of POSIX, is everywhere
  • to work on embedded device with few cli binaries
  • to work with small screens

cli

rlwrap ed -p '> '
ed  [OPTIONS] [FILE]
red [OPTIONS] [FILE]
-E –extended-regexp -  
-s –quiet –silent - turn OFF byte counts
-p –prompt STRING  
-v –verbose - prints error msgs
-r –restricted - disable edit outside CWD
      disable (!) command

commands

[ADDRESS[,ADDRESS]]COMMAND[PARAMETERS]
  .   exit insert mode
.,. p - prints
.,. n - prints w/number
.,. l - prints verbose
  u   undo last edit
.+1 \n - new line

address

= prints nr of buffer lines
.= prints nr current line
. prints current line
N jumps to N line number
$ jumps to last line
% entire file (aka 1,$)
+1 moves cursor down
-1 moves cursor up

address range

, whole file
% whole file
1,$ whole file
foo,/bar/ lines between next foo and bar
?foo?,/bar/ lines between previous foo and bar

substitution (.,.)

s repeats lasts substitution
s/re substitutes 1st re(gex) with nothing
s/re/bar substitutes 1st re(gex) on current line
s/re/bar/I substitutes 1st re(gex) on current line, case insensitive
s/re/bar/2 substitutes 2nd re(gex) on current line
s/re/bar/g substitutes ALL re(gex) on current line
s/re/%/ substitutes 1st re(gex) with prev subst
s/re/*&*/ substitutes 1st re(gex) with re

edit

. a - append mode
. i - insert mode
.,. c - change
  j   joins lines
.,. d - delete
.,. t . transfers/copies and paste lines
.,. m . moves/cuts and paste lines
.,. y - yank/copies to cut buffer
. x - pastes the cut buffer

file / command

  f   shows current file name
  f FILE   sets output FILE name
  e FILE   edit file, reads it and sets curr file
1,$ w FILE - write to a file
1,$ w !cmd - write to command
1,$ W FILE - appends to file
$ r FILE   read from file
$ r !cmd   read from command
  ! CMD   executes command, "%" is curr filename
  !!   executes last command

misc

Q quits unconditionally
q quits
P enables prompt
H enables error reporting
h prints last error msg

snippets

s repeat previous substitution
g/Needle/p shows lines matching
3,10c replaces range with given text
r !xsel paste from clipboard
;p prints lines from curr to EOF
,p prints all lines
,n prints all lines with numbers
foo;/bar/-1p prints between "foo" and "bar"
w !diff -u - % diff buffer with file
2w !wc count words, and chars on line 2
w !wc count buffer lines, chars
!wc % "

ex