Ed
Ah Ed, the standard editor.
ed a simple editor, which usefulness can be found when vi doesn't cut it anymore:
vineeds/usrto be mounted,eddoesn'tviis dynamically linked,edisn't- ed can be assumed to almost always exist,
vican be often assumed to exist vineeds$TERMto be set andtermcapfile exist- bsd.rd ships with
edbut notvi:
# vi ksh: vi: not found # ed BEHOLD THE POWER OF MIGHTY ED ?
- ed is cooler
- ?
Most of these are too niche for a day-to-day life, but there are times, ed worth it's manual page in gold. when crisis happens, ed can be your only friend.
ed, like vi is a modal editor, which means it has different modes, input mode and command mode.
input is for writing text and command is for commands, such as q, w, s and so on.
Input mode
input mode is where you type text into your document, to exit this mode, type:
.
in a empty line. you will be returned to command mode. to enter command mode, any one of these commands get you into input mode:
| a | append after current line |
| i | insert before current line |
| c | replace current line |
Command mode
basic ed commands
Note that some ed commands can be combined, as they did in vi, for example wq.
| e | opens a file to edit (includes !command notation too). cleans the buffer |
| E | same as e, but without warning on unsaved file |
| f | sets default filename |
| H | verbose errors (instead of "?") |
| h | explain last error ("?") |
| n | print current line and it's line number |
| p | print current line |
| P | toggles prompt, only if you had -p in arguments of ed |
| q | quits ed |
| Q | q, but without warning for unsaved file |
| u | undo, there is only one level of undo (which includes undo too) |
| r | read from a file (or a !command) |
| w | save the buffer |
| = | shows how many lines current file has (wc -l) |
| any number | sets current line |
advanced ed commands
| m,nd | deletes mth line until nth line in buffer |
| m,nG/re/ | finds and moves on lines which include regular expression re from mth line, until nth line |
| m,nj | joins all lines from mth line to nth line |
| m.ng/re/command | finds all lines containing re regular expression and executes (ed) command on them. |
| s/re/replacements | replaces all all "re"'s with replacements |
| m.nV/re/ | like G, but reverse (all lines that does not contain re) |
ed symbols
| , or % | all lines from first line until current line |
| ?? | repeats last search done by ?re? |
| ?re? | previous line containing regular expression re |
| -n or ^n | nth previous line |
| - or ^ | previous line |
| n | nthn line in buffer |
| . | current line |
| $ | last line in buffer |
| +n | n line upper |
| /re/ | next line containing regular expression re |
| // | repeats last search done by /re/ |
| + | next line |
| % | file name |
| 'lc | shows (book?)marked line in buffer (see k command) |
Examples
Opening a file
$ ed file
Using ed quietly, handy for scripts
$ ed -s file
