Handout: 108 June 1994 Save this as the file "tutor.emacs" on the disk of your Unix Machine and open it with the command "emacs tutor.emacs". Using EMACS If you are a new user of EMACS, we recommend you read this page, then try EMACS (either on your own, or using the UCS- provided tutorial in Appendix A, or using the standard EMACS tutorial). Save the reference material for later. UNIX is case-sensitive; all commands must be entered as shown. Otherwise, the command will not work or, worse, you may end up executing an entirely different command than you wanted! Start the editor by entering the EMACS command and a filename: emacs filename You can enter most EMACS commands by pressing a sequence of keys; these sequences are denoted as C- or M- C- preceding another character means to hold down the CTRL key while pressing the other key. For example: C-v for move to next screen. Sometimes C-x introduces another sequence. For example, C-x C-c exits from EMACS. M- (or Meta) preceding another character means press the ESC key and release it; then press the letter. For example: M-v moves backward one screen. Exceptions for Meta: with ProComm (PCPlus) for DOS only, use Alt-4 with a VT320, use F11 on the function keys across the top of the terminal. If your keyboard does not have an ESC key, use CTRL-[ (left bracket) the screen Initially EMACS divides the screen into two areas. The top, large area is the text window; the last line of the text window is the mode line. The second area - the bottom line on the screen - is the command line, where EMACS take commands and reports on what it is doing. basic editing Use the arrow keys for moving the cursor character by character or line by line. M-f move forward one word M-b move backward one word C-a move to beginning of line C-e move to end of line M-a move to beginning of sentence M-e move to end of a sentence M-< move to beginning of a file M-> move to end of a file C-v move to next screen M-v move to previous screen C-l put current line near middle of screen; redraw screen (lower-case L) C-x C-C quit Emacs C-h a search for a command by keyword more information about EMACS OIT is running the 5th edition EMACS version 18. For complete documentation refer to the GNU EMACS Manual, which is available in Academic Computing reference areas: General Consulting Office in LGRES room A109 I/O area LGRES room A110 Main Library reserve room Physical Sciences Library reserve desk _______________________________________________________________________________ CONTENTS key bindings and commands terminal environment key bindings using the command line M-x beginning an editing session -- the emacs command ending an editing session keeping the changes you have made not keeping the changes you have made buffers backup files error recovery canceling a command undo gibberish on command getting help within EMACS apropos from command name to binding list of bindings help on a specific keybinding EMACS with ProComm (PCPlus) EMACS tutorial APPENDIX A - UCS tutorial - example of creating and editing a data file with EMACS special problems working with rectangles (for columns of data) viewing long lines (over 80 characters long) unwrapped APPENDIX B - REFERENCE section useful EMACS commands alphabetized list of EMACS commands with keybindings (if any) and descriptions discussion ARG: the "universal argument" -- C-u # numeric arguments -- M # more on the command line -- M-x marking a region -- C-@ or C-2 marking a rectangle buffers and windows kill vs. delete and the yank buffer modes and filename extensions string mathcing and regular expressions special characters in regular expressions _______________________________________________________________________________ key bindings and commands Many EMACS commands can be entered with a key sequence (beginning with either the control or escape keys). This is called a key binding. VT320 terminals If you are using a VT320 with the Default SETUP [which is set with the SETUP key on the terminal], there are additional keybindings available. Also, on VT320 terinal, the Default SETUP sets the key marked C-] Alt-2 press C-\ Alt-1 press press so you see VT100 on ProComm status line; press C-l (lower-case L) C- - Alt-3 none necessary EMACS tutorial Before you actually use EMACS for the first time, we suggest you run the tutorial. To run the EMACS tutorial, start EMACS without entering a filename, and then press C-h t Note that microcomputer keyboards do not have a delete key labelled (the column is removed from the text) or press C-c k (the keybinding for kill-rectangle) 5. move the cursor to the place where the upper left corner of the rectangle is to be inserted 13 0 70 _ 6. press M-x and type: yank-rectangle and press (the third column now appears as last column of text) 13 0 70 1 making changes within a rectangle Changing all 0s to 2s for only the new last column of data: 1. move the cursor to the upper left corner of the rectangle 13 0 70 1 2. press C-@ (marks beginning of rectangle) 3. move cursor to one space after the lower right corner of the rectangle to be changed 153 10 99 1_ 4. press C-x r a the rectangle is copied to a register called "a" (a register name must be a single character) 5. press M-x and type: view-register and press the command line responds: view register: 6. type: a the a register, containing the rectangle, now appears on the bottom half of your screen 7. press C-x o (move cursor to register window). You can now edit the rectangle as much as necessary. 8. press M-x and type: replace-string and press the command line responds: replace string: 9. type: 0 and press the command line responds: replace string 0 with: 10. type: 2 all 0s are now changed to 2s in register a 11. move cursor to one space after the lower right corner of your rectangle in register a 1_ 12. press C-x r a to save the changes in register a 13. press C x o (move cursor to top window). It should now be at the lower right corner of the rectangle. 14. press M-x and type: delete-rectangle and press the original rectangle is removed 15. place cursor on position where you wish to place the upper left corner of the modified rectangle 13 0 70 _ 16. press C-x g a (inserts the contents of register a) 13 0 70 1 30 60 87 2 etc. viewing long lines (over 80 characters long) unwrapped By default, all lines over 80 characters long will occupy two or more lines on the screen (with a \ at the extreme right margin to indicate continuation). If you wish, you can "truncate" (or unwrap) the lines instead (so that you do not become confused as to which line is which). You can then scroll horizontally to see the part of the line that does not show on the screen. 1. press M-x then type: set-variable the command line responds: set variable 2. type: truncate-line the command line responds: set truncate-lines to value: 3. type: 80 (typing any number will cause EMACS to show only 80 characters) To scroll, press C-x and either < or > depending on which direction you want to scroll in: C-x < (scrolls the text left 80 characters, which means you see the 80 characters to the right of where you were looking) C-x > (scrolls the text right 80 characters, which means you see the 80 characters to the left of where you were looking) To scroll fewer characters (part of the screen), use a numeric (or universal) argument: C-u 20 C-x < (scrolls text left 20 characters) When you scroll the screen right (or left), the cursor is left in its previous position. If this position is no longer visible on the scrolled screen, the $ at the extreme edge of the screen will be highlighted. If you just want to view the screen, you do not need to move the cursor. However, if you need to edit the portion of text on the screen, you need to move the cursor to the visible area. You can move the cursor with any of the cursor movement commands. If you want to move character by character, or word by word, you probably will also want to use either the universal argument or the numeric argument. You can use the end-of-line command (C-e) if the visible portion of text does in fact show the end of the line. Example: Suppose your lines are 150 characters; or that your data appears in "words" that are 6 to 8 characters long with one (or more) blank space(s) between each one. You have used truncate-lines to unwrap the lines on the screen. You are now looking at the left-most screen (columns 1-79) and the cursor is in column 10. First, scroll the screen: C-x < scrolls the screen left. You now see columns 79-150 on the screen, but the cursor is still in column 10. If you think of your data as columns (a column is 1 characater -- a digit, letter, etc.), it is easiest to move the cursor with one of the commands that moves by column: C-u 90 C-f the universal argument (C-u) or numeric argument (M) or is used to quickly move the cursor 90 columns forward (C-f) M 90 C-f putting it in column 100. (If you only need to move the cursor a few columns, you can use your arrow keys as usual. If you think in terms of "words" [actual words followed by a blank space, or a string of digits followed by one (or more) blank space(s)], then it is easiest to move the cursor the appropriate number of words: C-u 8 M-f again, the universal argument (C-u) or numeric argument (M) may be used to indicate the number of words to move; the command to move forward one word if M-f. This command will move the cursor to the eighth blank to the right of the current position on a line of words separated by blank(s). Use a negative argument to move the cursor left (whether by columns or words). To return to wrapped lines, scroll so column 1 is in its normal position, then enter the following commands to set the truncation to nil: M-x set variable truncate-line nil To move your cursor back to the first character/column, enter C-a the command for beginning-of-line _______________________________________________________________________________ APPENDIX B REFERENCE SECTION useful EMACS commands Below are two lists of useful EMACS commands. Neither list is complete. For complete documentation refer to the GNU EMACS Manual, which is available in Acadmeic Computing reference areas. The first list is grouped according to functions. The next list is alphabetized by the names of the commands. EMACS key ProComm for DOS key EMACS key ProComm for DOS key M Alt-4 C-\ Alt-1 EMACS commands -- by functions getting help C-h help-command C-h a help-apropos C-h t help-with-tutorial C-h b describe-bindings C-h ? help-for-help C-h C-h help-for-help leaving EMACS C-x C-c save-buffers-kill emacs C-z suspend-emacs error recovery C-g cancel-command C-x u undo-last-command C-l redraw-garbage-screen cursor motion C-a beginning-of-line C-b backward-character C-e end-of-line C-f forward-character C-n next-line C-p previous-line C-o open-line M-f forward-word M-b backward-word scrolling C-v scroll-up C-l center-current-line-in window C-x < scroll-left C-x > scroll-right M- < beginning-of-buffer or-file M- > end-of-buffer-or-file M-v scroll-down marking commands C-@ set-mark-command or C-2 C-x C-x exchange-point-and-mark killing and deleting end-of-buffer Move cursor to the end of the buffer; Leave mark at previous position. C-e end of line Move cursor to end of current line. C-x^ enlarge-window-vertically Make current window ARG lines bigger. C-x} enlarge-window-horizontally Make current window ARG columns wider. C-x C-x exchange-point-and-mark Put the mark where cursor is now, and cursor where mark is now. M-x execute-extended-command Prefix for typing any command from the command line. C-x C-f find-file Edit file FILENAME. C-f forward-character Move cursor right ARG charaters (left if ARG is negative). M-g goto-line Go to line ARG, counting from line 1 at beginning of buffer; prompts for line number. C-h help-command C-h? help-for-help You have typed C-h, the help character. Type a Help option, or c-h for a description of the options. C-h t help-with-tutorial Select the EMACS learn by doing tutorial. C-x i insert-file Insert contents of a file into buffer after cursor. C-x g insert-register Insert contents of a register. Prompts for a name of register (one character). C-\ isearch-forward Do incremental search forward. OIT version of EMACS only; cannot be used with ProComm. C-r isearch-backward Do incremental search backward. keep-lines Delete all lines except those containing a given string (REGEXP) C-g keyboard-quit Restart a sequence of command keys. C-x k kill-buffer Get rid of the specified buffer. Prompts for name of buffer. C-k kill-line Kill the rest of the current line. If ARG is given, kills next ARG lines. C-w kill-region Kill between cursor and mark. C-c k kill-rectangle Remove rectangle with corners at cursor and mark; (save in yank buffer). M-O R kill-whole-line Kill entire current line and newline character at end. C-x C-b list-buffers Display a list of names of existing buffers. C-x C-d list-directory Display a list of files in a directory, as with 'ls'. list-matching-lines or occur Show all lines following cursor containing a match for a given string (REGEXP). C-n next-line Move cursor verticaly down ARG lines. occur Show all lines that follow current line that contain a match for a given string (REGEXP). occur-mode-goto-occurrence Goes to line in buffer where occurrence was found. Must be entered from the window that shows the occurrences. C-c o open-rectangle Blank out rectangle with corners at cursor mark, shifting text right. C-x o other-window Select the ARG'th different window. C-c v overwrite-mode Toggle overwrite mode. C-p previous-line Move cursor vertically up ARG lines. M- % query-replace Replace some occurrences of FROM-STRING with TO-STRING. possible responses for Query- replace are: (SPC =yes; Del =no; ! =replace all; M means quit) C-^ quoted-insert Read next input character and insert it. C- l redraw-display Clear the screen and display again what is supposed to appear on it. replace-string Replace all occurrences of FROM-STRING with TO-STRING. M- s save-buffer Save current buffer in file (if modified). C-x C-c save-buffers-kill-emacs Offer to save each buffer, then quit EMACS. M- v scroll-down Scroll text of current window downward (backwards). ARG lines or full screen if no ARG is given. C-x < scroll-left Scroll display ARG columns left, or full screen if no ARG is given. C-x > scroll-right Scroll display ARG columns right, or full screen if no ARG is given. C-v scroll-up Scroll text of current window upward (forward) ARG lines; or full screen if no ARG. C-@ or C-2 set-mark-command Set mark at current cursor location. set-variable Set the value of a VARIABLE; prompts for variable name and value. shell Start a "shell" for running unix commands without leaving EMACS. M- ! shell-command Execute a unix command from inside EMACS shrink-window Make current window ARG lines smaller. C-x 5 split-window-horizontally Split current window into two windows side by side. C-x 2 split-window-vertically Split current window into two windows; one above the other. C-z suspend-emacs Stop EMACS and return to superior process. You can resume EMACS at the same place using %emacs. C-x b switch-to-buffer Select a different buffer in the current window. Prompts for buffer name. C-x C-u upcase-region Convert the region to upper case. In programs, wants two arguments. C-x u undo Undo some previous changes. C-u universal-argument Enter a numeric argument for the following command. view-register Display what is contained in a register. Prompts for 1 character register name. C-x = what-cursor-position Display information on Cursor Position (on screen and within Buffer). C-c w what-line Display the line number (within the buffer) of the cursor. C-x C-w write-file Write current buffer to a file. write-region Write current region to a file. C-y yank Reinsert the last stretch of killed text. M- y yank-pop Replace just-yanked stretch of killed-text with previously killed text. yank-rectangle Insert the last killed rectangle with upper left corner at cursor. discussion the "universal argument" - C-u The "universal argument" is used to enter a numeric argument or repeat factor for an EMACS command. In the descriptions of EMACS commands, the word ARG is used to refer to the universal argument. The universal argument may also be negative (to cause the command to move or act backwards through the buffer). The universal argument must precede the command to which it is to be applied. Examples: a) C-d deletes the character the cursor is on. If you need to delete 10 characters, starting at the cursor, you could press C-d 10 times, or you could use the universal argument, and type C-u 10 C-d. b) Without numeric argument, C-v scrolls up (forward) a screen. Using universal argument, you can scroll forward a specific number of lines: C-u 50 C-v scrolls forward 50 lines of text. c) to insert 33 asterisks into your file at the current cursor location, type: C-u 33 * If you do not precede one of these commands by the universal argument, a default number will be used: a screenful for the scrolling commands; 1 for most other commands. numeric arguments The numeric argument is similar to the "universal argument": any EMACS command may be given a numeric argument that indicates how many times the command should execute. Numeric arguments may also be negative (to cause the command to move or act backwards through the buffer). Examples: a) M5 C-d deletes the character the cursor is on plus the next 4 characters. b) M10 C-v scrolls up (forward) 10 lines. M-10 C-v scrolls down (backward) 10 lines. more on the command line -- M-x Some commands do not have a key binding. These commands must be entered on the command line (also referred to as the mini- buffer). For commands that you don't use very often, you may find it easier to remember the name of the command than its binding. Press M-x, then you can type any EMACS command. Examples: The command "replace-string" does not have a key binding. To use it, press M-x then type replace-string. The "copy-region-as-kill" command has the key binding M-w. If you want to use this command, but don't remember the binding press M-x and type copy-region. When you are typing commands on the command line, you only need to type enough of the command to make each word unique; EMACS will fill in the rest. For example to enter "replace string", you can just type repl and press the space bar. EMACS completes the first word, replace-, and waits for you to type the second word. For the second word, it's enough to type st and press return. If you do not type enough to make a word unique, EMACS will show you a list of all possibilities. You must then enter additional letters until your choice is clear. In the previous example, if you had only typed rep [for replace] and pressed space, EMACS would show you a list of commands beginning with the words repeat and replace. When you add the "l", it completes the first word. marking a region -- C-@ or C-2 Many EMACS commands operate on only a selected part of your file. For example you might want to copy, move, or delete a section of text. To select the part you want to do something with, you need to put a "mark" at the beginning of the chosen section, and the cursor at the end (or the cursor at the beginning of the section, and a "mark" at the end.) The "mark" is invisible. You set it by typing C-@ or C-2 at the place you want marked. Once the mark is set, it remains in the same place until you set it somewhere else. In addition to the C-2 command, the beginning-of-buffer (M-<) and end-of-buffer (M->) commands also reset the mark. Having marked one end of the section you want, move the cursor to the other end. Everything starting at the mark, up to (but not including) the cursor is called a "region." Any EMACS command with the word region in it will apply to just that portion of your file. Examples: copy-region will copy the selected text to the yank buffer kill-region will delete the selected text write-region will write the selected text to a file marking a rectangle Rectangles are marked in exactly the same way as regions: put the mark in one corner of the rectangle and the cursor in the diagonally opposite corner. Any command that operates on rectangles will affect only the rectangular area demarcated by the mark and cursor. Examples: open-rectangle will shift everything right, to leave a blank rectangle kill-rectangle will delete the marked rectangle When a mark is set, the area between the mark and cursor could be used either as a region or a rectangle. buffers and windows A "buffer" is a temporary holding area. When you edit a file using EMACS, the file is loaded into an EMACS "buffer." The buffer has the same name as the file that is loaded into it. You edit the "buffer" copy of the file but the original is not changed until you save the buffer. EMACS lets you keep many files open for editing at the same time. Each open file is loaded into a different buffer. Each time you open a file with find-file (C-x C-f), you are opening a new buffer. In addition, you could also have a "shell" buffer, which allows you to execute unix commands without leaving EMACS. However, you don't usually see all your buffers on the screen. The active or current buffer fills the whole screen; the others (if there are any) are "behind" the current buffer, so you can't see them. The name of the current buffer is displayed on the formation line near the bottom of the screen. When you want to work on a different buffer, you use the switch-to-buffer (C-x b) command. If you can't remember the names of the buffers you have opened, use the list-buffers (C-x C-b) command. "Windows" are partitions of the screen. You can split your screen into 2 or more "windows," either horizontally or vertically, using the split-window commands. Then you can look at a different buffer in each window. The name of the buffer is listed at the bottom of each window. kill vs. delete and the yank buffer The delete commands of EMACS remove parts of text and throw them away. The only way to retrieve something you have deleted is with undo (C-x u), which will put it back in the same place. The kill commands also remove parts of text, but they are saved in the "kill" buffer. You can take text from the kill buffer and insert it anywhere in your file. Furthermore, when you "insert" something from the kill buffer, it is still available in the kill buffer, so you can yank it again. You can use the kill buffer to duplicate a section of text in many different places in your file. When you use the "yank" (C-y) command, you always get back the most recently killed text. If you need to insert something that was killed earlier, use yank-pop (M-y) following the yank. This will remove the text that was just yanked, and replace it with the next previously killed text. You can use yank-pop several times until you get back to the text you want to insert. See the OIT tutorial section of this handout for examples of kill and yank. modes and filename extensions EMACS has special commands for features that are based on the syntax of particular programming languages and certain applications. The indicator to EMACS is the filename extension, if any. The available EMACS modes and their associated extensions include Fortran, Lisp, C, LaTex, etc. For a list of the modes and their extensions, do a help on variable auto-mode-alist: C-h v auto-mode-alist string matching and regular expressions The descriptions of many EMACS commands use the words "regular expression," or REGEXP for short. A regular expression is just a string of characters; however, some characters have special meaning which can be used to make searches more general. For example, the list-matching-line command lists all lines in the current EMACS buffer which contain matches for a regular expression. Suppose you asked EMACS to list- matching-lines for the regular expression "out." It would list all lines in which any of the following words occurred: out, shout, outside, and many others. special characters in regular expressions The period (.) can be used as a "wild card" to replace a single character. In the above example, if you asked to list-matching-lines for the regular expression "o.t" EMACS would list any lines containing words such as loiter and boats, in addition to out, shout, etc. It would not match doubt, however; the period can only replace one character. Now suppose you are working with a file that contains decimal numbers, and you need to look at those numbers that end with ".5". If you use ".5" as the regular expression in the search, the decimal point will be interpreted as the wildcard, and EMACS will match it with numbers such as 15.0, and 35.2, as well as 10.5. You can use the backslash (\) before any special character to make it lose its meaning as a special character. So the regular expression \.5 will get you what you want- just numbers like 1.5, 33.5, etc. There are other special characters and regular expressions can get quite complex. Here we will merely mention what the other special characters are so you will not accidentally use them in a search string. The other special characters are: $, ^, *, +, ?, [, and ]. If you need to use a search string that includes one of these characters, be sure to precede that character with a backslash (\) so it will lose its special meaning. .