The generic command structure is: [address[,addresss]] command [parameters or destination address]
One concept to grasp when using ed is that of the current line or current address. It's the default address in the buffer on which commands work if no address is supplied. It changes constantly. The current address is initially set to the last line of a file read into the buffer. And generally, it's then set to the address of the last line (or the last of a range of lines) that's been changed by a command. Typing ".=" will give you the line number of the current address. You can also type "n" by itself to see both the number AND the text of the current address.
The address of 0 is always the first line of the buffer. The command 0a appends *before* first line, making whatever text is input the new first line.
(n,n) sets a range of numbers upon which to act. (n;n) sets the range from the current line to line n. In both cases, if only the first n is given, the second address is set equal to n. If only the second n is given, the range (1,n) is assumed; for (;n), the range of (current line, n) is assumed.
Regular expressions can also be used to set ranges. For example, "8,/foo/p" will print every line between line 8 and the first line containing "foo." Regular expressions can be used for *both* ends of a range. "/begin/,/end/d" will delete all lines beginning with the first match of "begin" and stop at the first match of "end."
Line address symbols:
- . = current address
- $ = last line in buffer
- n = line number
- +n = the nth next line
- -n = the nth previous line
- + = the next line
- - = the previous line
- , (by itself) = the entire buffer
- % (by itself) = the entire buffer
- ; (by itself) = from the current line to the end of the buffer
- '[a-z] = the line previously marked
- k[a-z] sets a mark for ed to go to using the above " ' " command
- Lines that are marked using the "k" command remain marked no matter what happens to the lines above and below them. In essence, marking a line gives it a permanent "line number" that doesn't change, even if the other line numbers in the document do.
- (n)d = delete line(s). Deleted text is stored in the cut buffer.
- (n,n)j = joins the two mentioned lines into one line, without inserting a space in the middle of the join. The original lines are copied to the cut buffer.
- (n)l = list lines to include showing non-printing characters and spaces at the line ends; line ends are marked with a "$"
- (n)m(n) = move line(s) to after destination address
- (n)n = print lines, showing line numbers
- (n)p = print the line(s) to the screen
- (n)t(n) = copies the marked line(s) to the line after the destination address
- u = undo previous change. Only the most recent change can be undone; there is no "multi-undo."
- (n)x = copies contents of cut buffer into document after addressed line
- (n)y = yanks (copies) of line(s) into cut buffer
- Printing commands can be combined: "nl" will both number a line and show the end-of-line.
- Just about every command can be followed by a p (print), n (number) or l (list) to show the changes just made.
- /[term] = search forward for [term].
- // = repeat last search
- ?[term] = search backward for [term]
- ?? = repeat last search
- .= (That is, period, equal sign) = the number of the current line.
File- and program-handling commands:
- e [file] = edit [file] and/or set default filename to [file] for writing. While you can't edit multiple files simultaneously, you *can* switch between files without having to leave the ed program, by typing e [new filename]. Be sure to save any changes before switching between files.
- E [file] = edit file WITHOUT warning about unwritten changes. Typing E while inside an edited file will undo *all* unsaved changes and write the file to disk.
- f [file] = sets the working filename to [file]. Typing "f" by itself will list the current filename. (Good if you've edited multiple files and want to know which one you're currently in.)
- You can't invoke ed [filename] if [filename] doesn't yet exist, because it's an empty buffer. However, once inside ed you can set a filename for saving either by typing e [filename] or by typing f [filename] while in command mode.
- h = print information on the last error
- H = toggle on/off the printing of error messages
- P = toggle command-mode prompt on/off
- q = quits ed. The program will warn if there are unsaved changes in the buffer.
- Q = quit without saving changes
- r [file] or [!command] = read the contents of [file] or the results of [!command] into the current buffer
- w [file] or [!command] = write the buffer to [file] or the standard input of [!command]
- (n,n)W [file] = append line(s) to end of [file]
- wq [file] = write to [file] then quit
- (n)zN = scroll N lines in file from line (n).
- !command = execute a shell command
- If you've done a lot of editing, typing "!clear" clears the screen via the shell, giving you a fresh "page" to work upon.
- <ctrl+c> will abort the current command