Helen Pain February 1984. AI1 notes Notes on using the editor EM 1. Loading a file/Saving a file. Say you have a file called fred that you want to edit. To load fred into the editor em from emas command level you type: Command:em fred The word Editor will then be printed and the number of lines in the files fred (if the file exists). If you do not actually have a file called fred one will be created. How do you save the file when you have finished editing? If you are editing fred and you type w after the > prompt: >w (followed by a carriage return) then the text in the editor will be copied back into your file. Any changes that you made in the file fred while you were in the editor will now have been saved in the new version of the file fred (you have lost the old copy for ever). 2. Prompts. When you are using em the prompt that indicates that the editor is waiting for a command is > Commands are given to the editor, one command per line (followed by carriage return ). If the command is carried out successfully, the same prompt will reappear. If, however, there is some error in your command then you will get a ? printed: ? [also you may get ?? meaning that the string you were searching for cannot be found - see context searching below]. 3. Syntax, commands and line numbers. Commands take the following form: the lines to which the command is to be applied are given, followed by the command. >27p >10,20p All lines in the file are referred to by line number: the first is line 1 and the rest number upwards. p is the command for printing on the screen, so p27 means print line number 27 on the screen. If we want to say "from line 10 to line 20, inclusive" we simply say 10,20, so the second command above 10,20p prints lines 10,11,12...19,20. If a line number alone is given with no command then it is taken to mean "print the line". So, line 27 could be printed by saying: >27 4. The Current Line. The line you are at is always "the current line". This will usually be the last line that you did anything to: >36p would print line 36 and this would become the current line. If you give a command without a line number it will be taken to mean the current line: >42p >p would both have the same effect. There is a special character to represent the current line. It is a dot. It can be used anywhere that a line number could be used. >10p >.,31p This would print line 10 and would then print lines 10,11,12,13...30,31. If you need to know what the current line is you can find out: >.= or >= the line number will be printed. Whilst p on its own will print the current line, if is typed ie no command is given after the prompt, the next line will be printed, and will become the current line: >p20 prints line 20 > prints line 21 > prints line 22 >= current line is 22 22 5. Other special line characters. There are other special characters that refer to particular lines. The last line of the file is represented by $: >$p or >$ prints the last line of the file and makes it the current line >$ >= will print the last line and its line no. [you can also find out the line number of the last line, without actually printing the line or making it the current line, by >$= ] Lines may also be referred to, relative to the current line: >17 would print line 17 >.-4p prints line 13 (=17-4) >-4 would do the same >-5,+5p would print the 5 lines either side of the current line 6. Context searching. What if you want to see the line in which a particular piece of text occurs, but you don't know its line number? Or you want to check whether some piece of text occurs at all in the file? The way to do it is to enclose the characters that you are searching for between a pair of "slashes" - /the characters/. These //'s are referred to as delimiting characters. >/interesting/ would print the next line encountered that contains the string of characters "interesting". >/red/ would print the next line containing the string "red" - even if it occurred in some other word eg "bored". You would need to put a space either side of the string if you wish it to be a separate word: >/ red / If you then want to find the following occurrence of the same string you do not have to retype the string, but just type: >// 7. Adding text. How do you add text to a file? The commands a (stands for add or append) and i (insert) allow you to do so: >a this is some text that you might add . > The command a is given, followed by . The text is then added. When you have finished adding it, type a single full stop on a line on its own. The prompt will then be returned (it does not appear when you are adding text). The text is added after whatever was the current line when the command a was given. The last line added becomes the new current line. The command i is used in the same way, except that the text is inserted before the current line (the current line becomes the last line added). 8. Deleting text. Text is deleted or removed from the file using the command d. Lines which are to be deleted are referred to in the same way as lines to be printed: >1,6d deletes lines 1,2,3..6 >.d or >d deletes the current line >1,$d deletes the whole file! not recommended >6,.d deletes all lines from 6 to the current line The line following the last line deleted becomes the current line. 9. Substituting text. What if you want to change a line in the file, or correct a mistake? You could do this by deleting the line and adding it again (the best solution when a lot of it needs changing). More often you will only want to change a few characters in it - to substitute one string of characters with a replacement string. The command s (stands for substitute) allows you to do this. >a this is a lien of text . >s/lien/line/ >p this is a line of text > The strings of characters are enclosed by delimiters - slashes as in context searching above. The command s is given, then the string to be removed, followed by the string to be put in its place. Remember though that if you have more than one occurence of the same string in a line, the first one will be matched and replaced: >p I saw no cars, only noe bike >s/no/on/p I saw on cars, only noe bike >s/on/no/p I saw no cars, only noe bike >s/noe/one/p I saw no cars, only one bike If you wish to change all occurrences of the string in the line you do this by typing g at the end of the s command line (as above, p may optionally be added if you wish to print the altered line): >p I like tea and I like coffee >s/like/do not like/gp I do not like tea and I do not like coffee > Some characters have special meaning in the substitution and replacement strings eg $ means the end of the line. If you actually wanted to use the character $ in the text you must indicate that you do not wish it to be treated as a special character by putting a backslash \ immediately before it: >p It cost $10 to buy >s/$/more than/p It cost $10 to buymore than >p It cost $10 to buy >s/\$/more than /p It cost more than 10 to buy Note that to delete a string simply replace it by nothing: >p I am very hungry >s/very //p I am hungry 10. Moving text. Chunks of text can be moved about in a file. The command m allows this to be done. The text to be moved is specified by line numbers, followed by the command m, and then the line number after which the text is to placed is given. The last of the moved lines becomes the current line: >2,6m20 would delete lines 2 to 6 and add them again after line 20. >1,20m$ would move the first 20 lines from the beginning to the end of the file. 11. Global commands. What if you have misspelt some word every time you have used it? You could find all lines on which it occurs (using context search) and then substitute every occurrence on that line. This is tedious. You can also, more easily do it by indicating that you wish to change it on every line in occurs: >g/intresting/s/intresting/interesting/ would look for every line containing "intresting" and alter the first occurrence of it to "interesting". >g/intresting/s/intresting/interesting/g would replace all occurrences. It is a bit tedious to type the string to be replaced "intresting" twice - it is also not needed: >g/intresting/s//interesting/g would assume that you meant the string to be found in the context search was the one to be replaced. As well as using the global command for substitution, it can be used with any other command, and can refer to any set of lines: >1,100g/fred/d would look at lines 1 to 100 and delete any lines that contained the string "fred". If no range of lines is given with the global command then the whole file (1,$) is assumed. 12. More on writing to files. As stated above, the command w will copy the edits back to the file you loaded (eg fred if you said em fred). You can also write out parts of the file to other files: >1,100w tempfile >w fredcopy The first command would copy the first 100 lines of fred to a file which would be called tempfile. The second command copies the whole file to a file called fredcopy. All the edits can still be copied back to fred by giving the command w on its own - there are not lost when they are copied. A few other things that may be useful: >& prints the page of text before the current line (same as -17,.p) >% prints the 8 lines before and after the current line >" prints the next 18 lines >r extrainfo would copy (r for read) in the text in the file called "extrainfo" (or any other file) immediately after the current line. In most cases where I've said that a command applies to the current the command can be made to apply to any line if the line number is given before the command eg 23r infofile would copy the file in after the line numbered 23, not after the current line. For more inforamtion see the leaflet/handout: "Using the 'EM' editor on EMAS" available from Margaret Pithie, or from me. [footnote - if you find any errors in these notes please use the editor em to correct them!]