– or – Your Introduction To The Wonderful World Of Programming With Ed!
(How to read the author's peculiar notation)
In all examples:
– The author/editor
(The 'ed', 'a', 'i', 'w', and 'q' commands)
First, the command to enter ed from lpmud can take one of two basic forms:
> ed <<filename>>
or
> ed <<dummy filename>>.
The first is used to edit an existing file. The second is used to create a new file. The dummy filename can be any string that isn’t already the name of a file. Short strings like ‘a’ work well for this.
Once you are in ed, your prompt will become :. From this prompt, you enter ed commands. If you are beginning a new file, you can use one of two commands to enter insert mode: a or i. The difference between these two will become significant later on. In insert mode, your prompt will become *. From here, you type in the body of your file. You can leave insert mode at any time by typing a single . on a line by itself.
Once you are finished entering your text and have returned to command mode (the : prompt), you can save your file with the w command, specifying a filename:
:w myfile.c
Then you can use q to exit ed and return to your normal prompt.
(The 'ed', 'a', 'c', 'd', 'i', 'l', 'p', 'w', 'z', and '=' commands.)
To edit an existing file (a reminder from section I), use:
> ed <<filename>>
This again puts you in command mode, with the difference from starting a new file being that there are already lines in the buffer. Use the command '.p' to see the line you are presently on.
The command 'p' will print out a line or range of lines. Its syntax is this:
The command 'l' is similar to 'p' in most respects, except that it also makes visible some 'invisible' characters, like newline (which shows up as $). Tabs become completely invisible. 'z' will display 21 lines in 'p' fashion (i.e., ctrl characters remain invisible). 'z' can be prepended by a line number to start at.
The command «number» will take you to the line of the same number.
The commands 'i' and 'a' are the commands introduced in section I. The difference between these two commands now takes on a significance; 'i' starts inserting before the line you are presently on, while 'a' starts inserting after the line you are on. (Use '.' or '.p' to display the line you are on.) Also, 'i' and 'a' can both be prepended by a «number», ('«number»i' and '«number»a'), which sets your present line at «number» before beginning insert mode.
The command '=' can be used to discover the number of the line you are on.
The command 'd' deletes the current line you are on. 'd' can, like 'p' and 'l', be used with a single line number, or a range of them for arguments.
The command 'c' is used to change lines. It is essentially similar to 'd' in its usage, except that instead of leaving you in command mode, you are put into insert mode, inserting text to 'replace' what was removed.
Finally, 'w' without being followed by «file» will save the file under the name you began editing it as, provided that file already existed.
* As a number, '.' refers to the present line; '$' refers to the last line of the file.
> ed a :a *This is line 1. *This is line @. *This is line 3. *This is line 5. *This is too many lines. *. :1,3p This is line 1. This is line @. This is line 3. := 3 :5l This is too many lines.$ :5d :1,$p This is line 1. This is line @. This is line 3. This is line 5. :2c *This is line 2. *. :1z This is line 1. This is line 2. This is line 3. This is line 5. :4i This is line 4. :1,$p This is line 1. This is line 2. This is line 3. This is line 4. This is line 5. :w file.txt :q > ls Total 1 1 file.txt >
(The 's' command)
The 's' command is used for substitutions. The general format is this:
:[<number1>[,<number2>]]s<delimiter><pattern><delimiter><sub>[<delimiter>gp]
The command may look intimidating at first, but it turns out to be one of the most powerful commands in ed.
An explanation of all the angle-brackets:
Some examples:
:. This is line nubmer 3. :s/bm/mb/p <--- Note that '/' is used as the delimiter here. This is line number 3. :
:. Thsi si line 3. :s!si!is!p <--- The delimiter here is '!' This si line 3. <--- Note that only the 1st occurrence of 'si' was changed. :
:. Thsi si line number 3. :s!si!is!gp <--- Here, the delimiter is '!', again. This is line number 3. :
:3,5p This is lize 3. This is lize 4. This is lize 5. :3,5sqzqnqp <--- Here, to confuse matters, the delimiter is 'q'. This is line 3. This is line 4. This is line 5. :
For a global substitution, use:
:1,$s/<pattern>/<sub>/g
Beware of special characters in «pattern». '.', '(', ')', '&', '*', '|', '[', '^', and ']' should all be prepended by backslashes ('\') if they are used. '\' is, although possible to use in «sub» and «pattern», very tricky to use. The author recommends beginners use the 'c' command to do substitutions for this character instead.
Some of these special characters that can be used in «pattern»:
The '&' can be used in the replacement to represent the text being replaced.
:. This is a lazy line, lying abed. It is also silly; abcd. :s/ab.d/ABCD/gp This is a lazy line, lying ABCD. It is also silly; ABCD. :
:. This is a long line that is being used to demonstrate a silly example. :s/l.n./&foo/ This is a longfoo linefoo that is being used to demonstrate a silly example. :
(The '<range>' notation)
What was formerly referred to as «number1»,«number2»«command» will, from here on out, be referred to as «range»«commmand», for the author's typing ease. Thank you.
(The 'e', 'E', 'f', 'j', 'k', 'Q', 'm', 'r', 't', 'x', and '!' commands)
The 'e', 'E', and 'r' commands can all be used to read external files into the buffer.
'Q' and 'x' can, like 'q', be used to exit ed.
'<range>j' will join all of the lines in <range> together into one line.
'<number>k<marker>' will assign <marker> to line <number>. <marker> can be any lowercase letter. A line marked is referred to as “'<marker>”. For example, the commands:
:5kd :1,'dp
will mark line 5 as “d” and then print out lines 1-5.
'f <file>' sets the default name of the file (used for the 'w' and 'x' commands) to <file>.
't' and 'm' are both used to move sections of text. The format is:
:<range>t|m<number>
where <range> is the text to be moved, and <number> the line it is to be inserted after.
'!' is used to execute lpmud commands from within ed, and can be used in either the command or the insert mode. For example:
:!'Wait, please; I'm in ed. Guest says: But I want to talk. :i *!'I said wait, please. *
(The 'g', 'v', '?', and '/' commands)
'/' and '?' are used to find expressions in the buffer.
'g' and 'v' use similar formats for operation:
:<range>g|v/<string>/<command>
Both 'g' and 'v' search through <range> for lines that contain <string>.
<command> may be one of the following commands: 's', 'l', 'p', 'd', 'm', or 'r'. Arguments for these commands work normally.
'/' and '?' may be used similarly to operate on the first occurrence of <string> found. For example:
:/foo/s/x/ox/gp
will replace all instances of 'x' on the next line containing 'foo' with 'ox' and set your current line there. Similarly,
:?bar?=
will give you the number of the first previous line containing the string 'bar', and set your current line there.
:1,$p This is a test. This is line two of the test. There are more than two lines. This is the last line.
:1,$g/two/l This is line two of the test.$ There are more than two lines.$
:1,$v/two/d
:1,$p This is line two of the test. There are more than two lines.
:?test This is line two of the test.
:.p This is line two of the test.
:/There There are more than two lines.
:Q >
–Mishael, author/editor.
David “Mishael” Green