SSCC Pub.# 7-10

Using Last revised: 4-19-00

The GNU Emacs editor is the most commonly used at the SSCC on UNIX. It is an extensible, customizable, self-documenting full-screen editor. Emacs is extremely powerful and has too many specialized commands and features to cover completely here. This handout serves only as an introduction, helping you get started using Emacs. A GNU Emacs Reference Card is attached to the end of this handout which lists many of the most frequently used Emacs commands along with a short description of what each command does. If you find you need to learn more after reading this handout, Emacs has a comprehensive online help facility where you can get more information. This help facility is described in this handout. Two books, Learning GNU Emacs, by Debra Cameron and Bill Rosenblatt and GNU Emacs Manual, by provide a more comprehensive coverage and are available from Consultant for short term loan.

Emacs is a modeless editor. This means that you insert text into your document without having to first enter a special insert mode. Likewise, you do not enter a special command mode when you need to issue a command. When you type an ordinary printable character such as the letter "k", it is inserted into your document; when you type a special character such as "Control-v" or "Meta-v", it is taken as a command (or the beginning of a command) to perform some operation such as deleting text or moving to the next line.

Typing Emacs Commands

Each command in Emacs has a formal name which is very long. Instead of typing out command names (which you could do), Emacs ties each command name to a short sequence of keystrokes starting with the or Meta key, followed by one or more characters. This tying of commands to keystrokes is called binding. Commands shown in this handout use the following conventions:

C-v Hold down the Control key and press v.

M-v Hold down the Meta key and press v. On the NCD X-terminals, the Meta key is the Alt .

If your keyboard does not have a Meta key, you can use the Escape key instead. Rather than holding down the Escape key and pressing a character as you do with the Meta key, you need to press the Escape key, release it, and then press a character. To complete a command you usually need to press a :

Note: If your keyboard has no Escape key, press C-[ (hold down the Control key and press [) instead. Release the key before pressing the next character in your command.

The most commonly used commands are bound to the Control key. Slightly less commonly used commands are bound to the Meta key. Other commonly used commands are bound to C-x something, where something is some character you type in after pressing C-x. You can also get at commands by typing M-x long-command-name, where long-command-name is a command's formal name. Most Emacs users prefer to use the short sequence of keystrokes over the long command names because they are

1 SSCC Pub.# 7-10

easier to learn and take less time to type in.

Emacs has a very helpful feature known as completion. For example, if you are typing the name of a file in the minibuffer (more about the minibuffer later) after issuing a command such as find-file (C-f), you need only type the first few letters of the name, enough to make a unique filename. Then press the and Emacs will complete the filename for you.

Getting Into and Out of Emacs

To enter Emacs, simply type emacs followed by the name of the file you want to edit. If you use a filename that does not exist, Emacs creates a new file and you will see a window similar to the one below:

You simply enter text to begin your editing session.

If the file you specify already exists, Emacs reads the file and displays it on the screen:

You can also omit the filename when you invoke Emacs. In this case, your screen will look similar to the one below:

2 SSCC Pub.# 7-10

The message printed in the Emacs window disappears as soon as you type the first character. Emacs then puts you in an empty buffer (a buffer is a temporary workspace that may be saved later as a file) called *scratch*. Note: *scratch* buffers are not saved when you exit Emacs. Type C-x C–w to write this temporary buffer to a permanent file.

The Emacs window contains three major areas: the largest area which takes up most of the window is where you enter your text. A cursor marks your position in the file. The cursor is also called "point" or "dot".

The area at the bottom of the window which is in reverse video is called the mode line and provides information about the editing session: the name of the buffer being edited, the date, the mode you are in (Emacs has many different modes, each of which customizes Emacs for editing text of a particular sort), and where you are in the file. If you are at the beginning of the file, Emacs prints the word Top; if you're at the end, it prints Bot; if you are somewhere in the middle, it shows you a percentage; and if the entire file is visible on the screen, Emacs prints the word All. At the left edge of this line, you may see two asterisks (**). If the asterisks are there, this means that whatever you are editing has been modified since the last time you saved it.

At the bottom of the window, below the mode line is the minibuffer. This is the area where Emacs echoes the commands you enter and where you specify filenames for Emacs to find, values for search and replace, and so on.

To exit Emacs at any time, type C-x C-c. If you have made any changes that have not been saved, Emacs will prompt you, asking you if you want to save your changes.

Getting Help

Emacs has an extensive help facility, which you can activate by typing C-h. C-h t activates the Emacs tutorial which provides an excellent guided tour of most of the important Emacs features and concepts. However, do not use it for retrieving information on how to do something in particular. There are better ways of doing this.

Pressing C-h k asks Emacs to give you documentation on whatever keystroke you type next. For example, if you type C-h k C-x i, Emacs gives you a description of the insert-

3 SSCC Pub.# 7-10

file command. Pressing C-h f asks Emacs to describe a function (really just a command's full name such as find-file). Essentially, C-h k and C-h f give you the same information; the difference is that with C-h k you press a key and ask what it does, whereas with C-h f you type a command name and ask what it does.

To display the help options, type C-h three times. Emacs displays a window listing the options and providing a brief explanation of each one. Some options replace the window by a window of help information; some others split the screen into two windows. The screen always contains at least one window; the window containing the cursor is called the selected window.

The following commands provide the window operations you will need in order to use the help system and exit from it when you are done:

! To switch windows when the screen contains more than one window, type C-x o.

! To scroll the selected window forward, type C-v.

! To scroll the selected window backward, type M-v.

! To scroll the other window forward, type M-C-v.

! To make all windows disappear except for the selected window, type C-x 1.

! To remove help information from the selected window, type C-x k.

Probably the most useful form of help when you are first starting out is "apropos" help, which you can use to find out all the commands relating to a particular topic. You can get "apropos" help by typing C-h a. Emacs then asks you for some text that might appear in the names of the commands you are interested in. For instance, if you respond with delete, Emacs lists all the commands that have delete in their names along with their explanations.

Editing Files

Emacs achieves some of its famed versatility by having various editing modes in which it behaves slightly differently. Mode in Emacs simply means being sensitive to the task at hand. When you are writing, you often want features like word wrap so you do not have to press Return at the end of every line. When you are programming, the code must be formatted correctly depending on the language. For writing, Emacs has a text mode; for programming, Emacs has modes for different languages. Whenever you edit a file, Emacs attempts to put you in the correct mode for what you are going to edit.

Fill Mode and Text Mode

Before you start typing, look at the mode line at the bottom of the Emacs window. If the word Fill appears, you are in fill mode. In fill mode, when you type past the end of a line,

4 SSCC Pub.# 7-10

Emacs automatically starts a new line for you. If Emacs is not in fill mode, you have to press Return at the end of every line. You can turn on fill mode for a session by typing M-x auto-fill-mode followed by a Return. This command is a toggle command so if you want to turn fill mode off, just type the command again.

Moving the Cursor

To move the cursor forward one space, type C-f ("f" for forward). C-b moves the cursor backwards one space ("b" for backwards). To move up, type C-p ("p" for previous) and to move down, type C-n ("n" for next). Often the on your terminal are mapped to these commands.

In Emacs you can also move to the beginning or end of a line. C-a moves you to the beginning of the line (just like a is the beginning of the alphabet). C-e (e for end) moves you to the end of the line. Using Emacs commands not discussed here, you can also move the cursor by words, sentences, and paragraphs.

Use C-v to scroll through your document one page at a time. Emacs leaves a couple of lines from the previous screen at the top to give you continuity. Likewise, M-v shows you the previous screen.

You often want to move all the way to the beginning or end of a file. To go to the end, type M->. To go to the beginning, type M-<.

Sometimes you may want to go to the n-th line or to the n-th character in the file. M-x goto-line n moves the cursor to the beginning of line n of the file. Likewise, M-x goto- char n goes to the nth character of the file.

The command C-l redraws the entire Emacs display and puts the line that has the cursor in the center of the screen.

Deleting Text

Emacs provides many ways to delete text. The simplest way to delete text is to press the or ; this deletes the character immediately to the left of the cursor. Unfortunately, since keyboards are not standard, your Backspace key may be labeled differently. Another way to delete a single character is with the C-d command. C-d deletes the character under the cursor.

If you want to delete an entire line, or part of a line, use the command C-k (for kill-line). This deletes everything from the cursor to the end of the line. Typing C-k on a blank line deletes the line itself. As a result, it usually takes two C-ks to delete a line; one to delete the text, and one to delete the resulting the resulting blank line.

When you use C-k, the material you delete does not just disappear. Emacs hides it away in a special place called the "kill ring". You can get back what you have killed with C-k by typing C-y (y for yank). Conveniently, if you kill several lines in succession, Emacs collects them all in a single item and places the whole unit into the kill ring; a single C-y

5 SSCC Pub.# 7-10 will bring back everything.

Marking Regions to Delete, Move, or Copy

You can mark areas of text called regions which you can then delete, move, or copy. To define a region, you use the primary pointer (your cursor) along with a secondary pointer called a mark. Unfortunately, the mark is invisible so you have to remember where it is. To mark a region, you set the mark at one end of the region by pressing C-@, then move the cursor (the point) to the other end of the region. Deleting text requires the following four steps:

1. Move the cursor to the beginning of the area you want to delete.

2. Press C-. Emacs displays the message Mark Set.

3. Move the cursor to the end of the region you want to delete.

4. Press C-w to delete the region.

Remember, you can retrieve whatever you have deleted with C-y. C-y also gives you an easy way to move text from one part of the file to another. Follow these steps to move text:

1. Delete the text you want to move following the four steps above.

2. Move the cursor to the point where you want to insert the text.

3. Press C-y and Emacs will insert the text.

To copy text, follow these steps:

1. Move the cursor to the beginning of the area you want to copy.

2. Press C-. Emacs displays the message Mark Set.

3. Move the cursor to the end of the region you want to copy.

4. Press M-w to delete the region.

5. Press C-y and Emacs will insert the deleted text back in its original position.

5. Move the cursor to the place you want to insert the copied text.

6. Press C-y and Emacs will insert the copied text.

6 Motion GNU Emacs Reference Card Cursor motion: (for version 18) entity to move over backward forward character C-b C-f Starting Emacs word M-b M-f Multiple Windows To enter Emacs, just type its name: emacs line C-p C-n delete all other windows C-x 1 To read in a file to edit, see Files, below. go to line beginning (or end) C-a C-e delete this window C-x 0 sentence M-a M-e split window in 2 vertically C-x 2 Leaving Emacs paragraph M-[ M-] split window in 2 horizontally C-x 5 suspend Emacs (the usual way of leaving it) C-z page C-x [ C-x ] scroll other window C-M-v exit Emacs permanently C-x C-c sexp C-M-b C-M-f switch cursor to another window C-x o function C-M-a C-M-e shrink window shorter M-x shrink-window Files go to buffer beginning (or end) M-< grow window taller C-x ^ read a file into Emacs C-x C-f M-> shrink window narrower C-x { save a file back to disk C-x C-s Screen motion: grow window wider C-x } insert contents of another file into this buffer scroll to next screen C-v select a buffer in other window C-x 4 b C-x i scroll to previous screen M-v find file in other window C-x 4 f replace this file with the file you really want C-x C-v scroll left C-x < compose mail in other window C-x 4 m write buffer to a specified file C-x C-w scroll right C-x > run in other window C-x 4 d run Dired, the directory editor C-x d find tag in other window C-x 4 . Killing and Deleting Getting Help Formatting The Help system is simple. Type C-h and follow the entity to kill backward forward indent current line (mode-dependent) TAB directions. If you are a first-time user, type C-h t for a character (delete, not kill) DEL C-d indent region (mode-dependent) C-M-\ tutorial. (This card assumes you know the tutorial.) word M-DEL M-d indent sexp (mode-dependent) C-M-q line (to end of) M-0 C-k C-k indent region rigidly arg columns C-x TAB get rid of Help window C-x 1 sentence C-x DEL M-k insert newline after point C-o scroll Help window ESC C-v sexp M--C-M-k C-M-k move rest of line vertically down C-M-o apropos: show commands matching a string C-h a kill region C-w delete blank lines around point C-x C-o show the function a key runs C-h c kill to next occurrence of char M-z char delete all whitespace around point M-\ describe a function C-h f yank back last thing killed C-y put exactly one space at point M-SPC get mode-specific information C-h m replace last yank with previous kill M-y fill paragraph M-q fill region M-g Error Recovery Marking set fill column C-x f abort partially typed or executing command C-g set mark here C-@ or C-SPC set prefix each line starts with C-x . recover a file lost by a system crash M-x recover-file exchange point and mark C-x C-x undo an unwanted change C-x u or C-_ set mark arg words away M-@ Case Change restore a buffer to its original contents M-x revert-buffer mark paragraph M-h uppercase word M-u redraw garbaged screen C-l mark page C-x C-p lowercase word M-l mark sexp C-M-@ capitalize word M-c Incremental Search mark function C-M-h uppercase region C-x C-u search forward C-s mark entire buffer C-x h lowercase region C-x C-l search backward C-r capitalize region M-x capitalize-region regular expression search C-M-s Query Replace Use C-s or C-r again to repeat the search in either direction. interactively replace a text string M-% The Minibuffer using regular expressions M-x query-replace-regexp The following keys are defined in the minibuffer. exit incremental search ESC complete as much as possible TAB undo effect of last character DEL Valid responses in query-replace mode are: complete up to one word SPC abort current search C-g replace this one, go on to next SPC complete and execute RET replace this one, don't move , show possible completions ? If Emacs is still searching, C-g will cancel the part of the skip to next without replacing DEL abort command C-g search not done, otherwise it aborts the entire search. replace all remaining matches ! back up to the previous match ^ Type C-x ESC to edit and repeat the last command that used exit query-replace ESC the minibuffer. The following keys are then defined. enter recursive edit (C-M-c to exit) C-r previous minibuffer command M-p Regular Expressions next minibuffer command M-n The following have special meaning inside a regular expression. any single character . (dot) one or more repeats + zero or one repeat ? any character in set [ ... ] Buffers any character not in set [^ ... ] select another buffer C-x b beginning of line ^ list all buffers C-x C-b end of line $ kill a buffer C-x k quote a special character c \c alternative (``or'') \| Transposing grouping \( ... \) transpose characters C-t nth group \n transpose words M-t beginning of buffer \` transpose lines C-x C-t end of buffer \' transpose sexps C-M-t word break \b not beginning or end of word \B Spelling Check beginning of word \< check spelling of current word M-$ end of word \> check spelling of all words in region M-x spell-region any word-syntax character \w check spelling of entire buffer M-x spell-buffer any non-word-syntax character \W character with syntax c \sc Tags character with syntax not c \Sc find tag M-. find next occurrence of tag C-u M-. Registers specify a new tags file M-x visit-tags-table copy region to register C-x x regexp search on all files in tags table M-x tags-search insert register contents C-x g query replace on all the files M-x tags-query-replace save point in register C-x / continue last tags search or query-replace M-, move point to saved location C-x j

Shells Info xecute a shell command M-! enter the Info documentation reader C-h i run a shell command on the region M-| Moving within a node: filter region through a shell command C-u M-| scroll forward SPC start a shell in window *shell* M-x shell scroll reverse DEL beginning of node . (dot) Rmail Moving between nodes scroll forward SPC next node n scroll reverse DEL previous node p beginning of message . (dot) move up u next non-deleted message n select menu item by name m previous non-deleted message p select nth menu item by number (1--5) next message M-n n previous message M-p follow cross reference (return with l) delete message d f delete message and back up C-d return to last node you saw l undelete message u return to directory node d reply to message r go to any node by name g forward message to someone f Other: send mail m run Info tutorial h get newly arrived mail g list Info commands ? quit Rmail q quit Info q output message to another Rmail file o search nodes for regexp s output message in Unix-mail style C-o show summary of headers h SSCC Pub.# 7-10

Keyboard Macros Copyright (c) 1987 Free Software Foundation, Inc. start defining a keyboard macro C-x ( end keyboard macro definition C-x ) Permission is granted to make and distribute copies of this execute last-defined keyboard macro C-x e card provided the copyright notice and this permission notice append to last keyboard macro C-u C-x ( are preserved on all copies. name last keyboard macro M-x name-last-kbd-macro insert lisp definition in buffer M-x insert-kbd-macro

Commands Dealing with eval sexp before point C-x C-e eval current defun C-M-x eval region M-x eval-region eval entire buffer M-x eval-current-buffer read and eval minibuffer M-ESC re-execute last minibuffer command C-x ESC read and eval Emacs Lisp file M-x load-file load from standard system directory M-x load-library

Simple Customization The intended audience here is the person who wants to make simple customizations and knows Lisp syntax. Here are some examples of binding global keys in Emacs Lisp. Note that you cannot say "\M-#"; you must say "\e#".

(global-set-key "\C-cg" 'goto-line) (global-set-key "\e\C-r" 'isearch-backward-regexp) (global-set-key "\e#" 'query-replace-regexp)

An example of setting a variable in Emacs Lisp: (setq backup-by-copying-when-linked t)

Writing Commands (defun () "" (interactive "