User Tools

Site Tools


editor

This is an old revision of the document!


0. A Prelude:

In all examples:

  • Words in angle brackets <like this> are not to be typed literally, but rather to be substituted for. For example, '<number>p' could be '4p' or '27p', etc.
  • [Anything in square brackets] is optional.
  • <x|y> represents <x> OR <y>, but not both.
  • > is assumed to be the standard lpmud prompt.
  • : is assumed to be the ed command mode prompt.
  • * is assumed to be the ed insert mode prompt.

I. A Beginning:

(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>
  • > 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 (':'), 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.

Summary

  • > ed <file | dummy file> takes you into ed.
  • 'a' and 'i' in command mode begin insert mode.
    1. –> Implicit assumption from now on: all commands will be from command mode unless stated otherwise.
  • '.' in insert mode returns you to command mode.
  • 'w <file>' saves your file.
  • 'q' takes you out of ed.

II. Editing existing files:

(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 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:

  • 'p' or '.p'* will print out the line you are presently on.
  • <number>p will print out line number <number>.
  • <number1>,<number2>p will print out the range of lines from <number1> to <number2>. In this case, <number1> must be less than or equal to <number2>. In both cases, all numbered lines must exist, or the command will fail. 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 that file already existed.

Footnote: * As a number, '.' refers to the present line; '$' refers to the last line of the file.

An example encompassing what we've done so far:

  • > 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
Summary:
  • 'p' and 'l' are used to display ranges of lines. 'l' displays some invisible characters.
  • 'z' displays 21 lines in 'p' format.
  • '=' displays the current line number.
  • 'd' deletes a line.
  • 'c' changes a line by deleting it and putting you into insert mode.

III. More Advanced Editing:

(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:

  • <number1> and <number2> are the range of substitution. If ',<number2>' is omitted, the range of effect is merely line <number1>. If '<number1>' is also omitted, the substitution defaults to the present line.
  • <delimiter> is simply a character used as a separator. Care should be taken that '<delimiter>' does not occur in either <pattern> or <sub>. The most common choices for <delimiter> are '/' and '!', although any character can be used.
  • <pattern> is the string that will be changed. <sub> is the string that it will be changed to. If <pattern> begins with a '^', that is taken to mean 'beginning of line.' Similarly, if it ends with '$', it signifies 'end of line.'
  • The final optional [gp] are used, respectively, to make the substitution global throughout the line (instead of just affecting the first occurrence), and to display the newly-changed line immediately afterwards. Note that g must come before p, if both are used.

Some examples:

  • :.

This is line nubmer 3.

  :s/bm/mb/p
  This is line number 3.
* :.
  Thsi si line 3.
  :s!si!is!p
  This si line 3.
* :.
  Thsi si line number 3.
  :s!si!is!gp
  This is line number 3.
* :3,5p
  This is lize 3.
  This is lize 4.
  This is lize 5.
* :3,5sqzqnqp
  This is line 3.
  This is line 4.
  This is line 5.

General notes: 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.

editor.1735836418.txt.gz · Last modified: 2025/01/02 16:46 by titania