Using the EMACS Editor 1. What is EMACS? Emacs is a full-screen editor which allows you to create character files (also called ASCII or text files). You would use Emacs to : a. Create and modify a source code program (i.e. the program as written in some programming language such as assembly language, Pascal, C, etc). b. Create and modify data files (i.e. the files which contain the input data for an executing program). Emacs allows the user to add characters, remove characters, and modify characters in the specified file. When Emacs is invoked, the contents of the specified character file stored in secondary storage are copied to a special area of memory called the "file buffer". Any additions, deletions, or modifications made to the file during an Emacs editing session are made to this "buffer" version of the file. The original stored version of the file remains in secondary storage (on a disk drive unit) and is not directly altered or modified by the editing operations performed during an editing session. When the contents of the buffer version of the file are "saved", two things happen: a. The original stored file is copied to a "back-up" file in secondary storage. The name of this back-up file is the same as the name of the original file, but with the tilde character (~) appended at the end. b. The buffer version of the file (with all revisions made during the current editing session included) is written over (and thus destroys) the original stored version of the file. If the buffer version of the file is not formally "saved", then all changes and modifications made during the current editing session are lost and the original stored form of the file remains unchanged. So, be sure to periodically save the file being edited. 2. Explanation of the Control and Escape Key Commands In the remainder of this document, the following special key combinations will have the meaning as described. a. CTRL-key Where "key" is some valid key on the keyboard. This term will signify that the "control key" should be pressed and held down while at the same time "key" should be pressed once and released. Example: CTRL-c means to hold the control key (marked CTRL) down while at the same time press and release the "c" key. b. ESC/key Where "key" is some valid key on the keyboard. This term will signify that the "escape key" should be pressed and released, and then "key" should be pressed and released. Example: ESC/x means to press and release the escape (ESC) key, and then press and release the "x" key. 3. Obtaining Help in Using EMACS a. Tutorial There is a tutorial on the use of Emacs and its editing facilities. To view the tutorial, invoke Emacs and press CTRL-h and then press the "t" key. To get out of the tutorial, press CTRL-q. b. Individual instructions Invoke Emacs and press and hold the control key while at the same time press and release the "h" key. Do this three consecutive times and you will view information on how to access descriptions of Emacs commands. c. Personal assistance The system consultant in SAL-125 is a primary source of assistance on the proper use of Emacs. You can also call (213) 740-5555 (and get the UNIX consultant) for phone assistance in using Emacs. d. You can also get help in using emacs by attending the special 2-hour free classes offered by UCS (University Computing Services). To see the time, date, and location of the emacs classes, just enter: help from the UNIX command line, and then enter the integer digit preceding the word "classes", and then enter the integer digit preceding the word "emacs". 4. Invoking EMACS From the UNIX command line prompt, type: emacs filename and then press the RETURN key. If the file named "filename" does not exist, then it will be created. If it does exist, then it will be loaded for editing. 5. Moving the Cursor within a File that is Being Edited The cursor can be moved to any character position on the screen, or within the file. a. The CTRL-b (backward one character) and CTRL-f (forward one character) keys can be used to move the cursor to any desired character on a line. b. The CTRL-p (up to Previous line) and CTRL-n (down to Next line) keys can be used to move the cursor up a line or down a line within the file. In fact, you can scroll up or down the entire file by pressing and holding down the CTRL-p and CTRL-n key respectively. c. ESC/< will move the cursor to the first character in the file (i.e. it moves the cursor to the top of the file). d. ESC/> will move the cursor to the last character in the file (i.e. it moves the cursor to the bottom of the file). e. CTRL-a will move the cursor to the first (left-most) character position of the line in which the cursor currently resides. f. CTRL-e will move the cursor to the last (right-most) character position of the line in which the cursor currently resides. g. CTRL-v will display the next screen full of the file contents (i.e. it scrolls one screen toward the end of the file). h. ESC/v will display the previous screen full of the file contents (i.e. it scrolls one screen toward the beginning of the file). i. To move the cursor to the "nth" line of the file, do: ESC/x and then type: goto-line and then press the RETURN key. A prompt for the line number to which you wish to move the curser will appear. Type the desired line number and press the RETURN key and the cursor will be moved to the specified line in the file and that line will be displayed at the middle of the screen. You can also go to a specific line of the file by invoking EMACS as in: emacs filename +n where "n" is the number of the line you wish to have the cursor placed on. When emacs starts, line n will be placed at the middle of the screen. 6. Deleting Items from the File You must place the cursor on the item to be deleted and then perform the deletion operation. a. Delete a character: 1. Delete the character immediately to the left of the cursor. Press the "backspace" key. 2. Delete the character the cursor is on. Press CTRL-d b. Delete a line: Place the cursor on the first (left-most) character of the line to be deleted and then: Press CTRL-k twice, and the line is deleted from the file. c. Delete a block of the file text (use this when you want to delete multiple lines in the file). 1. Place the cursor on the first character of the block of text to be deleted. 2. Press CTRL-@ to set the begin-of-block mark. 3. Move the cursor to the last character of the block of text to be deleted. 4. Press CTRL-w and the entire block of text is deleted from the file. If you wish to delete just a few lines of text, it may be easier to repeatedly use CTRL-k. For example, if you want to delete 5 contiguous lines of text, put the cursor on the far left side of the first (upper-most) of the five lines, and press CTRL-k 10 times. 7. Insert Items in a File a. Insert a character immediately to the left of the character on which the cursor currently resides. To do this, just press the key that corresponds to the character to be inserted. b. Insert a new blank line above the line on which the cursor currently resides. 1. Move the cursor to the first (left-most) character in the line above which the new line is to be inserted. 2. Press the RETURN key. This works even if you want to insert a new line at the very top of the file. c. Insert a new blank line below the line on which the cursor currently resides. 1. Move the cursor to the character position that is immediately to the right of the right-most character in the line below which the new line is to be inserted. 2. Press the RETURN key 8. Redisplaying the Contents of the Screen If the contents of the screen ever get overwritten with a message or other unwanted text appears over your file text, press: CTRL-l (the last character is lowercase el) and the contents of the current file screen will be re-displayed (thus removing the unwanted text). 9. Saving a File Save a file, but do not exit the Emacs editor: a. To save the file being edited under the same name as that used when Emacs was entered, press. CTRL-x and then CTRL-s A back-up file of the original stored file is made, and then the contents of the buffer version of the file (containing all revisions made during the current editing session) are written out as the new stored version under the same file name as the original stored version of the file. b. Save the file being edited under a different file name than the name used when Emacs was entered. CTRL-x and then CTRL-w then respond to the prompt with the new file name. No back-up to the original stored file is made. You are prompted to enter the name you want to have the contents of the buffer version of the file saved to. A new file is created in the current (sub)directory having the name you specified at the prompt. The contents of the buffer version of the file (with all revisions made during the current editing session included) are then written to this new file. 10. Exit the EMACS Editor CTRL-x and then CTRL-c and you are returned to the UNIX operating system command line. If you made a change in the file you are closing, the editor will ask you if you want to save the changes or exit emacs without saving the changes. 11. Search a File for a Specified String Press CTRL-s and the prompt "Search for?" will appear at the bottom of the screen. Type the string (series of characters) you would like to search for in the file, and then press the RETURN key. The file will be searched from the current cursor position to the end of the file to locate the string you specified. If the string is found, the search ends and the cursor is left on the last character of the first instance of the string you are searching for. If the string cannot be found in the file, you are so informed. Press CTRL-r and the same thing as happened for CTRL-s occurs except the search is made from the current cursor position to the beginning of the file (i.e. a reverse search is done). To have Emacs find the next (or previous) instance of the same string, press the: CTRL-s or CTRL-r keys again. 12. Search a File for and Replace a Specified String Press ESC/% and the prompt "Search for?" will appear at the bottom of the screen. Type the string you want to have replaced and then press the RETURN key. The prompt "Replace with?" will appear at the bottom of the screen. Type the string you want to replace the "Search for?" string with, and then press the RETURN key. Emacs will then find the first instance of the string you entered at the "Search for?" prompt and moves the cursor to its location in the file. If you now press the "y" (for yes) key, this string will be replaced by the string you entered at the "Replace with?" prompt. If you press the "n" (for no) key instead of the "y" key, no string replacement is done and the cursor is moved to the next instance of the string you entered at the "Search for?" prompt. You can then press the "y" key or "n" key and repeat the search-and- replace operation again and again. If you press the ESC (escape) key instead of the "y" or "n" key, the search-and-replace operation is terminated and you are placed back into edit mode. To have all instances of the "Search for?" string replaced by the "Replace with?" string without having to repeatedly press the "y" key, you can press the "!" key when the first instance of the "Search for?" string is found. Be careful using this option because you may end up replacing instances of the string you do not want to have replaced. 13. Cut and Paste (Copy and Move) Blocks of File Text a. To COPY a block of text to another location in the file. Copy means that the block of text you mark to be copied remains in its original position in the file unchanged. 1. Place the cursor on the first character in the block of text that is to be copied. 2. Press CTRL-@ to mark the beginning of the block. 3. Move the cursor to the last character in the block of text to be copied. 4. Press ESC/w and the block of text is copied to a temporary (scratch) buffer in main memory. 5. Move the cursor to the file location where you want to insert the block of text that was just copied. 6. Press CTRL-y and the most recent block of text that was copied into the scratch buffer is inserted at the current position of the cursor. b. To MOVE a block of text to another location in the file. Move means that the block of text marked to be moved is deleted from its original file position and after the move operation is completed will no longer be found in its original file position. 1. Place the cursor on the first character in the block of text that is to be moved. 2. Press CTRL-@ to mark the beginning of the block. 3. Move the cursor to the last character in the block of text to be moved. 4. Press CTRL-w and the block of text is deleted from the file and is copied to a temporary (scratch) buffer in memory. 5. Move the cursor to the file location where you want to insert the block of text that was just deleted. 6. Press CTRL-y and the block of text that was deleted is inserted at the current position of the cursor. If you wish to MOVE just a few lines of text, it may be easier to repeatedly use CTRL-k. For example, if you want to move "n" contiguous lines of text to another place in the file, put the cursor on the far left side of the first (upper-most) of the "n" lines, press CTRL-k (n*2) times, move the cursor to the spot in the text where you want to move these "n" lines, and press CTRL-y once. If you wish to COPY just a few lines of text, it may be easier to repeatedly use CTRL-k. For example, if you want to copy "n" contiguous lines of text to another place in the file, put the cursor on the far left side of the first (upper-most) of the "n" lines, press CTRL-k (2*n) times, press CTRL-y once, move the cursor to the spot in the text where you want to copy these five lines, and press CTRL-y again. 14. Selecting EMACS as Your Default Editor If you are using a piece of software (e.g. CODECENTER), and you request the editor be loaded and the VI editor is invoked instead of the EMACS editor, you can force the system to use EMACS as your default editor by executing the following UNIX command. From the UNIX command line prompt, enter: setenv EDITOR /usr/usc/bin/emacs where EDITOR must be in uppercase characters. 15. Some Advanced EMACS Features A. REPEAT an emacs command "n" times. You can have almost any emacs command repeated two or more times by using the ESC/# repeat command. The "#" is any positive whole number greater than one. For example, if you want to move the cursor down 12 lines from its current position, you can enter: ESC/10 { that is, press the ESC key, let go, and } { then press the 1 and then the 0 (zero) } { keys } and then press CTRL-n. Another example. Say you would like to enter a line of 50 "*" characters as in: ************************************************** Instead of pressing the "*" key 50 times you could do: - Place the cursor where the 50 asterisks are to be placed. - Press ESC/50 - Press the "*" key once Thus, the repeat command can be used to insert multiple blank lines, insert a line of text multiple times, delete multiple lines, delete the next "k" characters, etc. If you press the ESC key, but then decide you do not want the effect produced by pressing the ESC key, press CTRL-g and the ESC key effect will be discarded. B. UNDO Commands Already Carried Out If you have made a change to the text, but you decide that you would like to UNDO that change (i.e. put the file back into the form it was in before you made the change), you are able to do this by using the emacs UNDO command CTRL-x u. For example, say you just moved a block of text from one spot in the file to another. Now you decide you made a mistake and want to put that block back where it was. Instead of doing another MOVE command, you can just press: CTRL-x, let go, and then press the "u" key. You can combine the UNDO command with the REPEAT command to undo the last "k" changes. For example, say you just finished (a) deleting one line of text, (by pressing CTRL-k twice), (b) copying a block of text, and (c) inserting a new line, and you want to undo all four of these changes (four and not three because pressing CTRL-k twice is considered as two commands). To do this, all you have to do is: 1. Press EXC/4 2. Press CTRL-x u C. Editing More than One File at a Time You can have emacs edit more than one file at a time. Each time you load a new file, that file is put into its own BUFFER separate form any other file being edited. Thus, if you are concurrently editing "k" files with emacs, there are "k" buffers, each one storing a separate file. Say you are currently editing "file1" and now you want to concurrently edit "file2" without pushing "file1" out of emacs. Just press CTRL-x and then CTRL-f and respond to the prompt by entering "file2" (or whatever name the second file has). If "file2" is not in the current subdirectory, then you must enter its full pathname. "file2" will now be loaded into emacs (in a different buffer than the one storing "file1") and now you can concurrently edit both files. You must take an action to have emacs move from one file to the other. Assume emacs is currently editing "file1" ("file2" has been loaded as described above), and you want to edit "file2". Just press CTRL-x and then CTRL-f and enter the file name "file2" and now you will see "file2" on the screen. To go back and edit "file1", just press CTRL-x and then CTRL-f and enter "file1" and now you are back in "file1". You can go back and forth between the files as many times as you wish. You can use the CTRL-x, CTRL-f command to concurrently edit many files with emacs. To see all the files (buffers) currently active in emacs, press CTRL-x and then CTRL-b. (the list of active buffers will be displayed in a "window". To get rid of this window when you have finished reading the list, put the cursor in the area of the screen (outside the window) where the current file is being edited, and press CTRL-x and then press the "one" key. The window will disappear. To save the changes made in the separate buffers, press CTRL-x and then press the "s" key (note I did NOT say CTRL-x, CTRL-s, just CTRL-x and then the "s" key). You will be prompted to save or not save changes for every file in the separate buffers. NOTE: a BUFFER is just an area of main memory which can temporarily store some block of characters (such as a text file). Emacs and the operating system keep up with where these areas (blocks) of main memory are. You do not need to worry about doing this.