_ _
___ | |__ __ _ _______ | |_
/ _ \| '_ \ / _` |_ / _ \| __|
| (_) | | | | (_| |/ / (_) | |_
\___/|_| |_|\__,_/___\___/ \__|
ed —
text editor: usage
- NOTE: this document is based on OpenBSD ed, there may be some differences
on GNU ed.
back to top
[range]command
| P |
| toggle prompt. |
| q |
| quit. Warns if the buffer has been modified. |
| Q |
| quit regardless of whether any changes have been made to the
buffer. |
| h |
| explain last error. |
| H |
| toggle error explanation. |
| u |
| undoes the last command. |
| = |
| print total lines. |
Most commands require a range on which to be executed. If no range
is specified, it defaults to the current range.
Exceptions: u (undo), z (scroll), h (help
messages).
| n |
: 6 |
| n,n |
: 4,5 |
| + |
: next line. |
| - |
: previous line. |
| . |
: current line. |
| $ |
: last line. |
| +n |
: address at n lines after current line. |
| -n |
: address at n lines before current line. |
| ; |
: from current line to the end. |
| /pattern/ |
: next line matching pattern. // repeats last search. |
| ?pattern? |
: previous line matching pattern. ?? repeats last search. |
| p |
: print addressed lines. |
| n |
: print addressed lines with line numbers. |
| l |
: print addressed lines including control and non-printable
characters. |
| a |
: append: start adding text after the range. (Eg.: 4a) |
| i |
: insert: start adding text before range. (Eg.: 4i) |
| c |
: change: change the addressed line. (Eg.: 4c) |
| d |
: delete: deletes range. (Eg.: 4d) |
| m |
: move: move source range after target range. (Eg.: 4m5) |
| t |
: copy (transfer): copy source range after target range. (Eg.: 4t6) |
| j |
: join: join addressed lines. (Eg.: 4,5j) |
| kl |
: mark an range with a lower-case letter. (Eg.: 4ka) |
| 'l |
: set current range to the range marked as l. (Eg.: 'a > will set
current range to 4) |
| e [filename] |
: edit file, if no file is specified it reloads the current file. |
| E |
: same as e but discards changes if any. |
| w [file] |
: writes file. If file is not specified, it writes the current
file. |
| [range]w [file] |
: writes specified range to file or current file if no file is
specified. |
| [range]W [file] |
: same as w but appends to filename if the file exists. |
| % |
: represents the current file. |
| f |
: sets/shows the current filename. |
| r [file] |
: reads file into buffer at the end. |
| [range]r [file] |
: reads file into buffer after range. |
| g |
: search globally for matches. |
| v |
: search globally for non-matching lines. |
Commands G and V work the same as the lower case version, but
edits each match interactively.
- G/re/
- Returns the first match.
- User edits the match.
- ed displays the next match.
- The user can edit the match or enter & to repeat the last non-empty
command list.
$ ed -p :
:a
line 1
line 2
line 3
.
:G/l/
line 1
s/n/a/
line 2
s/e/o/
line 3
&
:,n
liae 1
lino 2
lino 3
| s |
: normally, substitution is called with /pattern/, but any character can
be used (Eg.: |pattern| or @pattern@).
this is useful when substituting paths or
variables.
|
| z[n] |
: scroll.
- if n is specified, scroll the number of lines specified by n.
- If n is not specified and zn has been run before, remember the number
of lines used before.
- if n is not specified and zn has not been run before, scroll the
number of lines determined by the terminal size.
- n: add n to show line numbers. (Eg.: z5n)
|
The following commands can be used with shell commands:
| e |
: edit output of command |
: e !command |
| r |
: read output of command into buffer |
: [range]r !command |
| w |
: write addressed lines into command. |
: [range]w !command |
$ ed -p :
:e !ls
:,n
dir1
dir2
:r !ls dir1
:,n
dir1
dir2
file1
:w !xargs -n1 echo
dir1
dir2
file1
back to top
$ ed t
a
hello bye
.
w
10
w !awk 'sub("o ","o\n",$0);' > %
e
10
,n
hello
bye
back to top
- NOTE: It's important to always have the . and w on any interactive
commands.
To add a . on a script without ending the input, enter
\. and then substitute the \ :
,s@^\\.$@.@
$ ed script
a
a
this line is appended
\.
w
.
,s@^\\.$@.@
w
q
$ cat script
a
this line is appended
.
w
$ cat test.txt
line 1
$ ed - test.txt < script
$ cat test.txt
line 1
this line is appended