EMACS SCREEN EDITOR March 30, 1995 Department of Mathematics University of Utah ASCII Version of Original TeX source 1 INTRODUCTION Emacs is a text editor. It can create letters, computer program sources, typesetting sources, electronic mail messages, scripts for applications like SAS and MAPLE. It is not a word processor: formatting and printing are missing. The editor is screen-based. It starts in insert mode. A carriage return inserts a blank line or splits the line at the cursor. All normal typewriter characters are inserted as you type with no special procedure required. Emacs is loaded in X-windows by a mouse button Menu, or in a terminal window by the Unix shell command emacs & The acronym emacs abbreviates editor macros. The original idea of author Richard Stallman was to identify the fundamental editing objects (a macro) and assign to each a special invocation key. 2 TUTORIAL AND MANUAL Beginners can benefit by running the GNU emacs tutorial. Start emacs by the unix shell command emacs &, then press |MouseLeftButton| on menu item |Help| to select subitem |EmacsTutorial|. Alternatively, enter |Ctrl|-h and press the letter t to start the tutorial. The info tree manual is viewed by unix command xinfo, which uses only the mouse and X- window environment. This is useful for first users of emacs because it is separate from the text editor. Inside emacs itself there exists a similar info system, invoked by |MouseLeftButton| on menu item |Help|, subitem |Info|. The information is the same but the internal version has a sharper learning curve. The Emacs manual can be viewed by the local unix command xdvi /usr/local/doc/emacs.dvi &. The viewer works with the mouse from a sidebar menu and has its own Quit button. The manual is for sale in the university bookstore. There are popular paperbacks for sale; the titles contain the keywords Gnu Emacs. 3 NOTATION Point Emacs synonym for current cursor position. Different from the mouse pointer position! Mark Start position of a region ending at the cursor position. Not highlighted! DEL Press the Delete key. Not the same as backspace! RET Press and release the Return or Enter key. SPACE Press and release the space_bar. TAB Press and release_the |Tab| key. Duplicate: |Ctrl|-I CTRL Hold down |Ctrl| to emit a control code for the next key. SHIFT Hold down |Shift| to emit a CAP or shifted key. ESC Press and release the |Esc| key. Duplicates: |F11|, |Ctrl|-[. COMPOSE Hold down |Compose|, press next key, release. Prefixes |Esc| to the key. |<>| Keys |<>| and |Compose| are the same. LEFT Keys |Left| and |Compose| are the same. |Ctrl |-XS A control sequence. Hold down the |Ctrl| key, then press X and S, then let up on the |Ctrl| key. |Esc |-F A Meta sequence. Press |Esc|, release, then F. Duplicates: |Compose|-F, |<>|-F, |Left|-F |Esc|-X string A manually entered command named string. For example, `string' could be `revert-buffer': |Esc|-X revert-buffer |Esc| then x then revert-buffer then |Return|. 4 EMACS MENU BAR The normal functions of emacs appear on an upper menu bar with pulldown menu. There are four main menus: Buffer, File, Edit, Help. These menu selections replace the key assignments below in certain cases. Useful choices appear under Edit: Undo, Cut, Copy and Paste. 5 LOADING AND SAVING FILES |Ctrl|-XS Save file and continue. Hold down |Ctrl|, press keys XS and release. |Ctrl|-XF Load old file or create new file. |Ctrl|-XW Save as ... |Ctrl|-XC Exit to Unix shell and kill emacs. |Ctrl|-XSXC Save and exit to Unix shell |Ctrl|-X i Read file (inject) into buffer at cursor Hold down |Ctrl|, press X, release, press lower case i. 6 CHARACTER OPERATIONS |Ctrl|-B Move left (Back) |Ctrl|-F Move right (Forward) |Ctrl|-P Move up (Previous) |Ctrl|-N Move down (Next) |Delete| Delete left. Not the same as backspace! |Ctrl|-D Delete right |Ctrl|-T Transpose previous 2 characters |Ctrl|-X u Undo last operation. Unlimited. Valuable! Same as |Undo| on SUN4 (requires setup). Cursor Keys They should work as you expect. 7 LINE OPERATIONS |Ctrl|-A Move to the left edge |Ctrl|-E Move to the right edge |Ctrl|-O Open up a line for typing |Ctrl|-K Cut text from cursor to right edge. Twice removes carriage return as well. |Ctrl|-AKK Delete entire line. Restored by |Ctrl|-Y below. |Ctrl|-Y Paste (Yank) the last cut. See also Region Operations below. 8 SCREEN OPERATIONS |Ctrl|-V Show next screen. Duplicate: |PgDn| |Esc|V Show previous screen. Duplicates: |Compose|-V, |<>|-V, |PgUp|, |Left|-V |Ctrl|-L Redisplay screen and center at cursor |Ctrl|-X Get rid of other split window |Esc|< Top of file. Duplicates: |<>|-<, |Left|-<, |Home| |Esc|> Bottom of file. Duplicates: |<>|->, |Left|->, |End| 9 SEARCH AND REPLACE |Ctrl|-S |Esc| Standard forward search. Press |Ctrl|-S |Esc|, then enter string, end with RET. Usually mapped to |Find|on a SUN4 (requires setup). |Ctrl|-R |Esc| Standard backward search. Press |Ctrl|-R |Esc|, then enter string, end with RET. |Esc| X Replace String Replace one string with another from cursor to end of file. Enter |Esc| X replace-s SPACE then a search string then RET and follow it by the replacement string and RET. |Esc| % Query-Replace one string with another. Enter |Esc| % then the search string, press RET and follow it by the replacement string and a final RET. Answer SPACEBAR to replace, DELETE to skip, Q to quit. During Query-Replace, press ? for a help screen. 10 BUFFER OPERATIONS The easy way to change buffers is to use an emacs X-windows menu. Activate the menu by depressing at once two keys, then press |Mouse Left Button| and release the keys. The two-key sequence is either |Shift|+|Ctrl|, |Shift|+|Left|, |Shift|+|Compose|, or |Shift|+|<>|. |Ctrl|-X b? Change to new buffer. Uses completion. |Ctrl|-X k Kill buffer. Use it to eliminate unused buffers. 11 REGION OPERATIONS The Region is the area of the buffer between point and mark (or mark and point). Some commands set the mark, so check it before using with |Ctrl|-XX (see below). See also Cut and Paste below. |Ctrl|-SPACE Set the mark for the start of a region. The other end of the region is set by the cursor position (called point). Duplicate: |Ctrl|-@ |Ctrl|-XX Interchange point and mark. Moves the cursor to the other end of the region. The definition of the region is unchanged. |Ctrl|-W Kill region. Use |Ctrl|-Y below to unkill. |Esc|-W Copy region. Use |Ctrl|-Y below to transfer copy elsewhere. Duplicates: |<>|-w, |Compose|-w, |Left|-w |Ctrl|-K Apply repeatedly to kill a region of lines |Ctrl|-Y Yanks back last killed region or deleted lines to cursor position (point). 12 CUT AND PASTE This is the most mysterious part of emacs, since regions are not highlighted. To cut is to delete a block of text from the screen. All text removed by emacs is saved in case you want to insert it later. Similarly, a region can be defined and then cut as a block of text. To paste is to insert the saved text at the cursor position. You can cut text by repeated application of |Ctrl|-K, |Esc|-D and that defines a block of saved text. To mark a region: press |Ctrl|-SPACE and move the cursor to the end of the region. That defines the region, but you can't see it! Press |Ctrl|-XX to set the cursor to the other end of the region, repeat to reset the cursor where it was. After a few repetitions you should be able to imagine where the region is even if you can't see it highlighted. Once a region is marked the following common operations are possible: block delete |Ctrl|-W block copy |Ctrl|-WY or |Esc|-W block move |Ctrl|-W move cursor to point of copy |Ctrl|-Y inserts the deleted text. The mouse can be used to mark a region. At the region's start press |Mouse Left Button|, then at the region's end press |Mouse Right Button|. The region is copied twice: once into emacs own private buffer (called the kill ring) and once again into X-windows cut buffer. In any case, |Mouse Middle Button| will insert the associated text into any window on your workstation. For example, you can cut and paste between maple and emacs by the same mouse methods. An emacs problem: the mouse pointer and cursor position are generally different. The solution in emacs: first click |Mouse Left Button| to set the insertion point, then press |Mouse Middle Button| to do the actual insertion. The mouse is easy to misuse. To recover from button errors use emacs undo: |Ctrl|-X u. 13 MACRO STEP MEMORIZER |Ctrl|-X ( Start recording. Key in any series of commands. |Ctrl|-X ) End recording. |Ctrl|-XE Repeat memorized steps (macro). Use |Ctrl|-UX e to repeat 4 times, etc. Same as |Again| key on SUN4 (requires setup). 14 MISCELLANEOUS COMMANDS |Ctrl|-G Abort anything at any time (Valuable!) |Ctrl|-X u Undo last command. Can be repeated. Duplicate: |Undo| on SUN4. |Esc| # Goto line number. Emacs prompts for a line number. Duplicates: |Compose|-#, |<>|-#, |Left|-# |Esc| n Do next command n times |Ctrl|-U Do next command 4 times (|Ctrl|-UU is 16 times). |Ctrl|-Q Inserts next key verbatim. Useful before a function key to see the codes it emits. |Esc| 1 |Esc| Q Justify paragraph. Duplicates: |Compose|-1Q, |Left|-1Q, |<>|-1Q |Ctrl|-X d Directory editing subsystem dired. You can also use dired as a unix shell command. Read the manual page about it with unix command man dired. Dired is used to delete files, edit them, and tree through directories. |Ctrl|-X m Mail program. Read, send and edit electronic mail. Read about how to use it with program xinfo 15 EMACS INITIALIZATION FILE All customization of emacs is handled by a file called .emacs in your root directory. The system operator gives you one with your account. If you don't like the way emacs works, then it is up to you to change the source file. It is realistic to expect that someone else has already customized emacs in the same way as you would like. Ask questions of emacs experts. They can quickly copy in portions of their .emacs file into yours to effect the changes you want. 16 MINI MANUAL The following document may be useful for other features of EMACS. There is considerable duplication with the above list, which is intentional. For any serious use of complicated commands it is required that you read the GNU emacs manual, which can be displayed by the unix shell command xdvi /usr/local/share/solaris/emacs/man/emacs.dvi & Notation: M- means |Esc|, C- means |Ctrl|. The Meta key is |<>| or |Left| on a SUN keyboard and |Compose|on a DEC3100 keyboard. The DEC3100 keyboard uses |F11| or |Ctrl|-[ as the Esckey. Brief EMACS command chart. (CURSOR CONTROL) ================ C-A Beginning of line C-E End of line C-B Backward character C-F Forward character C-N Next line C-P Previous line M-A Backward Sentence M-E Forward Sentence M-B Backward Word M-F Forward Word M-R Move point to mid-line of screen. C-L Redisplay screen with point at center line. C-U k C-L Position point k lines from top of screen. C-U k M-R Move point to kth line of screen. M-M Moves to the line's first nonblank character. M-[ Move back to previous paragraph beginning. M-] Move forward to next paragraph end. C-X = Prints a line in the echo area In this line, the X value is the column the cursor is in (zero at the left),the Y value is the screen line that the cursor is in (zero at the top), the CH value is the octal value of the character after point (101 is "A"), the "point" value is the number of characters in the buffer before point, and the values in parentheses are the percentage of the buffer before point and the total size of the buffer. (WINDOW CONTROL) ================ C-V Next Screen M-V Previous Screen C-L Refresh current window C-U k C-V Scroll the screen k lines M-< Beginning of file M-> End of file C-X 1 One Window mode C-X 2 Two Window mode C-X O Other Window C-X 3 Two windows,stay "in" top one. C-X ^ Grow Window C-M-V Scroll the other window. C-U C-X 2 Two windows,one buffer,for viewing different portions of buffer. (CUT, PASTE AND DELETE CONTROL) =============================== C-D Delete next character M-D Delete next word DEL Delete previous character M-DEL Delete previous word C-K Cut to end of Line C-Y Paste last killed text as a region C-W Cut Region between point/mark M-W Copy region into kill buffer C-M-W Append region onto kill buffer M-" Delete spaces and tabs around point. C-X C-O Delete blank lines around the current line. M-^ Join two lines by deleting the CRLF and any indentation. C-X DEL Cut back to beginning of sentence. M-K Cut to end of sentence. (UN-KILLING) ============ C-X u Undo last operation. C-Y Yank (re-insert) last cut text, move point to end. C-U C-Y Yank last cut text, leave point where it is. M-Y After C-Y fails to replace the right text, press this key in succession *to cycle through all possibilities of cut text that you might want to have inserted. (SEARCH CONTROL) ================ C-S Forward Incremental Search C-R Reverse Incremental Search C-S ESC string RET Forward standard Search C-R ESC string RET Reverse standard Search C-S ESC C-W words RET Forward word Search ignoring punctuation C-R ESC C-W words RET Reverse word Search ignoring punctuation C-M-S regexp Forward search for regular expression C-M-R regexp Reverse search for regular expression (REGION) ======== C-@ Place mark at cursor. Region defined by new location of cursor. C-Space Place mark at cursor. C-X C-X Exchange point and mark. Usually repeated to view defined region. M-@ Set mark after end of next word. C-< Set mark at beginning of buffer and refresh screen. C-> Set mark at end of buffer and refresh screen. M-H Put region around current paragraph. C-X h Put region around entire buffer. C-X C-P Put region around current page. (REPLACEMENT) ============= M-% string RET newstring RET Allows for selective replacement Called "Query-Replace" options (space) Replaces (doesn't show change) (rubout) Don't replace (comma) Replace and show change (uparrow) Go to previous replacement (?) Show options (ESC) Quit (period) Replace it and then quit. M-X replace-string RET string1 RET string2 RET Replaces all subsequent occurences of "string1" with "string2", no query, whole file! (KEYBOARD MACROS) ================= C-X ( Begins definition ----Insert any series of commands here---- C-X ) Ends definition C-X e Execute macro. May be mapped to AGAIN key on SUN4. M-X name-last-kbd-macro RET LispSymbolName RET The LispSymbolName (string) is used as the lisp symbol for the last defined keyboard macro. M-X global-set-key RET functionkey RET LispSymbolName RET Define a function key to execute an emacs command name, usually a keyboard macro named as above. Example: M-X global-set-key RET [press F3 now] RET call-last-kbd-macro RET This makes F3 execute the current keyboard macro instead of C-X e (ABORTING) ========== C-G Stops command in progress C-X u Undoes the effect of the last command. Duplicates: M-X undo, UNDO on SUN4 (requires setup) (EXIT EMACS) ============ C-X C-C Exit, save buffers by query, kill emacs process. (INFORMATION ABOUT EMACS) ========================= M-X info Invoke INFO TREE reader. Q quits. C-h Start the self-documentation feature of EMACS. C-h b Make list of key bindings, ready to print M-X apropos RET keyword RET Get all the EMACS commands that contain the keyword. M-X describe RET function RET Get documentation on an emacs function. (FILE BUFFER CONTROL) ===================== C-X C-F Find File C-X C-V Visit File C-X C-S Save File C-X C-W Write File C-X b Select a buffer C-X k Kill a buffer C-X C-B List Buffers M-X save-buffers-kill-emacs Saves all changed buffers simultaneously. M-X rename-buffer Rename a buffer. M-X kill-some-buffers EMACS will ask which buffers you want killed. M-X view-buffer RET name RET Quick view using space and backspace for paging. Exit with RET. (TRANSPOSE COMMANDS) ==================== C-T Transposes two characters. M-T Transpose two words C-X C-T Transposes two lines. C-X T Transposes two arbitrary regions. (FILLING AND JUSTIFYING) ======================== C-X F Set fill (or justify column). Initally 0. M-Q Fill paragraph. Do C-U 72 C-X F first! M-1 M-Q Justify paragraph. See Set fill above! M-G Fill between point and mark M-1 M-G Justify between point and mark M-X center-line Center current line. Not bound to a key, usually. (WORD ABREVIATIONS) =================== C-X C-A Define a mode abbrev for some text before point. C-X + Define a global abbrev for some text before point. C-X C-H Define word expansion for mode abbrev before point. C-X - Define a word in the buffer as an abbrev M-/ Complete current word before point with first match backwards. C-X ' Expand abbrev before point. M-X list-abbrevs RET Shows definitions of all abbrevs. M-X edit-abbrevs RET Lets you edit the definition list directly. M-X read-abbrevs-File RET filename RET Defines word abbrevs from a definition file. M-X write-abbrevs-File RET filename RET Makes a definition file from current abbrev definitions. M-X define-abbrevs Define abbreviations from buffer M-X insert-abbrevs Insert abbreviations into buffer (UPPER AND LOWER CASE COMMANDS) =============================== M-L Lower case next word M-U Upper case next word M-C Capitalize first letter of next word C-X C-L Convert region to lower case. C-X C-U Convert region to upper case. M-' Fix up omitted shift key on digit. (GETTING A COUNT) ================= M-X how-many RET string RET Gives you a count of the number of occurences of the "string" M-X count-lines-region Count the number of lines between point and mark. M-X make-word-list Convert the buffer contents to a list of words, one per line. M-X what-line Line number of current line. (REMOVING TABS) =============== M-X untabify RET Removes tabs in the file (COMMAND HISTORY AND ECHO AREA) ============= C-X ESC Enter echo area command history with last command presented M-P Move back in list of previous emacs commands M-N Move forward in list of previous emacs commands RET Execute command appearing in echo area and exit C-G Abort echo area command mode (DETAB A FORTRAN FILE ) ========================= M-X detab-fortran RET Replaces tabs with spaces (LIBRARIES) ========== M-X load-library RET libname RET Load a special library. M-X list-library RET libname RET List library functions, documentation (DIRECTORIES) ============ C-X d Invoke DIRED. Duplicate: M-X dired RET The commands possible: d Mark a file to be deleted. u Unmark a file that was marked for deletion. x Execute all marked deletions. f Find file and load into editor. If a directory, then dired it. v View file in editor, return to dired when finished. o Open a window and edit this file immediately. r Rename file c Copy file h Help on dired commands. A mini course! C-X k Kill dired window. C-X b Change to another buffer. C-X C-D List the directory. Press C-X 1 to dispose of window. This document prepared by G. B. Gustafson for unlimited distribution. Copying is unrestricted.