We all know that this is not a satisfactory documentation for
ed, but it is better than nothing.`
Use the additional help from within ed available at the commands 'h'
and 'h<command_you_are_interested_in>'.
When in 'ed', the prompt is ':'.
Ed has two modes, command mode and insert mode. The insert mode has
no prompt. You exit the insert mode by typing a single '.' on a line.
All commands have the following syntax:
X,Ycmd
or
Xcmd
For example:
1,10p
Will print line 1 to 10.
1,5d
Will delete line 1 to 5.
8p
Will print line 8.
A '.' is the "current line". The current line is the last line
referenced. If you want to print last line + 10 more:
.,.+10p
Ed knows the following commands:
/ search forward for pattern
? search backward for a pattern
= show current line number
a append text starting after this line
A like 'a' but with inverse autoindent mode
c change current line, query for replacement text
d delete line(s)
e replace this file with another file
E same as 'e' but works if file has been modified
f show/change current file name
g Search and execute command on any matching line.
h help file (display this list)
i insert text starting before this line
I indent the entire file (from DGD ed v0.1)
j join lines together
k mark this line with a character - later referenced as 'a
l line line(s) with control characters displayed
m move line(s) to specified line
n toggle line numbering
p print line(s) in range
q quit editor
Q quit editor even if file modified and not saved
r read file into editor at end of file or behind the given line
s search and replace
set query, change or save option settings
t move copy of line(s) to specified line
v Search and execute command on any non-matching line.
x save file and quit
w write to current file (or specified file)
W like the 'w' command but appends instead
z display 20 lines, possible args are . + -
Z display 40 lines, possible args are . + -
As line numbers '.' is current line, and '$' is last line of file.
Thus '1,$p' will always print all of the file.
Searching is done with /text/:
/hello/
Finds first line after current line holding 'hello' in it.
Just // will repeat the search.
Substitutions are very advanced.
First a simple example:
s/apa/bepa/
This will substitue the 'apa' in current line to 'bepa'.
If an 'p' is appended, you will also immediately see the result.
1,$s/apa/bepa/
Same, but all lines in file. Only first occurence on every line.
Any character can used instead of '/':
s!apa!bepa!g
The 'g' specifies that all occurences of apa on this line are
changed to bepa.
The pattern that are supposed to be replaced, can be a regular
expression. Regular expressions assign certain matching function to
some special letters:
. Match any character.
^ Match begin of line.
$ Match end of line.
\< Match begin of word.
\> Match end of word.
x|y Match regexp x or regexp y.
() Match enclosed regexp like a 'simple' one.
x* Match any number (0 or more) of regexp x.
[..] Match one of the characters enclosed.
[^ ..] Match none of the characters enclosed.
The .. are to replaced by single characters or character ranges:
[abc] matches a, b or c.
[ab0-9] matches a, b or any digit.
[^a-z] does not match any lowercase character.
\c match character c even if it's one of the special characters.
Example:
s/ab.d/ABCD/
Substitute any string 'abXd' against 'ABCD' where X can be any character.