The below is a help file for ed. The editor used inside the mud to edit files. This was borrowed and modified from a forgotten source, if anyone knows the original author, or where I stumbled upon this on the web, i would appreciate knowing so that I can give the proper credit. Zifnab@reddragon
(How to read the notation in this document)
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', '3p','100p' etc.
Anything in square brackets [] is optional.
< x|y > reperesents < x > _OR_ < y >, but not both.
: is the standard ed prompt.
There are 2 modes to ed, 'command mode' and 'insert mode'. When you are in command mode ed is waiting for you to enter commands, such as deleteing rows, displaying rows etc. When in command mode, everything you type gets treated as a command.
In insert mode your prompt may or may not be the line number you are on depending on your settings. ( I will explain more on that shortly). When you are in insert mode everything you type gets 'put into' the file you are editting.
'p' or '.p' will print the line you are currently on.
'< number > p' will print out line number < number >.
'< number1 >,< number2 >p' will print out the range of lines from < number1 > to < number2 >.
In the case of printing ranges of lines number1 must be less then or equal to number 2. If number2 is bigger than the end of the file you will see lines number1 to the end of the file.
The command 'Z' will display 40 lines. Same as typing 1,40p. 'z' and 'Z' can also be of the format < number > z. You will then see from < number > down 40 lines. You can also append a < +|- > on the end of z to see up or down. For example:
< number > z- 30z- -- > shows from line 9-30. The default is +
> ed a -> filename a :a -> command 'a' for append This is line 1. -> the next 5 lines are entering the file. This is line @. This is line 3. This is line 5. This is too many lines. . -> return to command mode. :1,3p -> display the lines 1 - 3 This is line 1. This is line @. This is line 3. := ->display current line number 3 ->current line number :5l -> using the 'l' command to show line feed on line 5 This is too many lines.$ :5d -> delete line 5 :1,$p -> print from line 1 to end of the file This is line 1. This is line @. This is line 3. This is line 5. :2c -> change line 2 This is a line 2. . -> back to command mode :1z -> print from line 1 down 20 lines This is line 1. This is line 2. This is line 3. This is line 5. :4i -> insert above line 4 (now in insert mode) This is line 4. . -> back to command mode. :1,$p -> print from 1 to end of file This is line 1. This is line 2. This is line 3. This is line 4. This is line 5. :w file.txt -> save our file :q -> quit
The 's' command is used for substitutions. The general format is this:
:[ < number1 > [,< number2 > ]]s < delim > < pattern > < delim > < sub > [ < delim > 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.
< delim > is simply a character used as a separator. Care should be taken that '< delim >' 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 <--- Note that '/' is used as the delimiter here. we want to replace the first occurence of bm with mb and print the line afterwords 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. and because we used the g parameter all occurences of si were changed. 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'. and we changed a range of lines 3-5 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. If you really want to below is an example of a few ways to do do this.
wiz/magneto -> we want to change this line to domain :s.wiz/magneto.domain -> notice by switching delimiters you can put a / in the pattern. if you use / as the delimiter, this will not work :s/wiz\/magneto/domain -> the other option is to escape the / with a \
Some of these special characters that can be used in <pattern>:
. Match any character. x* Match any numbers of x (0 or more). [abc] Match 'a', 'b' or 'c'. [0-9] Match any digit 0 - 9. [a-z] Match any lowercase letter. [^0-9] Match anything except a digit 0 - 9. The & can be used in the replacement to represent the text being replaced. Example: :. 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. : Example: :. 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. Author's Interjection: (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.
:< range > t|mi < number >
where < range > is the text to be moved, and < number > the line it is to be inserted after. 't' is 'transfer' and leaves a copy of the text to be moved at the original site; it is basically a 'copy' command. 'm' is 'move' and moves the text from its original site to its new site.
:!'Wait, please; I'm in ed. Guest says: But I want to talk.
'/' and '?' with no arguments default to the previous argument. If no argument was given prior, an error occurs.
Both 'g' and 'v' search through < range > for lines that contain < string >. 'g' executes < command > on all lines in < range > that contain < string >, while 'v' executes < command > on all lines that do not 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?=
d
will give you the number of the first previous line containing the string 'bar', and set your current line there.
Examples: :1,$p <----- List from line 1 to the end of the file 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 <----- list all lines that contain the word two This is line two of the test.$ There are more than two lines.$ :1,$v/two/d <----- delete all lines that _dont_ contain the word two :1,$p <----- List from line 1 to the end of the file (You should notice that a few lines are missing.) This is line two of the test. There are more than two lines. :?test <----- Search backwards for test This is line two of the test. :.p <----- Print the current line number This is line two of the test. :/There There are more than two lines. :Q >
If you want line numbers on all the time when you do ~e make sure they are on, then do 'set save' at the : prompt.