Using Emacs

A Brief Introduction


Executive Summary: This document is designed to help you get started using emacs. It contains a listing of some of emacs' most commonly-used and helpful features, as well as a general overview of what emacs is all about.

What is Emacs?

Emacs is an editor, and perhaps the most portable easy-to-use one for UNIX systems. However, it's also one of the most completely flexible, user-programmable, vast programming environments you can imagine. The engine behind emacs is a Lisp interpreter, which allows users to code their own routines to customize the environment nicely. As such, it can be an intimidating beast, feeling huge and overly complex, and full of options you'll never discover.

The key is to just pretend "what I don't know won't hurt me" and use what you find useful. The more you use it, the more you'll learn. Emacs has a very extensive help system, so it's possible to learn as you go.

For the purposes of this class, you shouldn't have to learn too much about it, and I recommend you don't spend too much time trying to figure out the intricacies of emacs -- if you need to know something and can't figure it out quickly, ask.

For those who want to become emacs gurus, a good place to start is the O'Reilly book "Learning GNU Emacs", which should be available at the bookstore.

Notation

When talking about emacs, we'll use the notation C- to indicate that a key should be pressed while holding down the control key (emacs requires this a lot). For example C-h means hit H while holding down the control key. Similarly, M- means use the Meta key. Reflection-X can be configured to map one or both of the Alt keys to Meta, and I suggest doing that. Ask the lab staff if you're unsure how to configure Reflection-X.

If Alt has not been so configured, the Escape key can be used instead, by punching it before the modified key. For example, instead of typing M-x, one can type Esc, release the key, and then type x.

Lots of emacs functions take two keystrokes to complete. For example, to exit emacs, use C-x C-c (Control-X followed by Control-C).

Running emacs

Emacs can be run in two ways. One will work on any sort of character terminal and makes very few assumptions about what your hardware set-up is. You could run emacs at home on your Vic-20 in this mode, for example. The other assumes that you have an X-terminal, and allows you to do fun stuff like use the arrow keys and the mouse. The latter is recommended whenever it's available to you -- it's much easier to use.

After logging onto an instructional unix server, you run emacs by simply typing the following into an xterm:

Remember that the ampersand means that your xterm won't wait until emacs is done to put up another command prompt.

Emacs Orientation

When you start up emacs, it will come up with an empty buffer. At the top of the window are some pull-down menus which contain some of the common operations. At the right side is a scroll bar. Near the bottom is a status bar. Just below that is a line called the mini-buffer.

Figure 1 A small emacs window

The status bar tells you what you about the buffer you're looking at. The double asterisk (**) indicates that the buffer has been modified since it was created or last saved to disk (in this case because I typed "test" in it). Sometimes, you'll see "%%" there. That means the current buffer is read-only, and that you can't type into it. Next, the bar says "Emacs:" and tells the name of the buffer "*scratch*" -- this is the default buffer when starting up. Next, it tells the mode that this buffer is operating in -- Lisp Interaction mode is the default when starting up. Then it tells what part of the buffer you're looking at. In this case, it contains only the word test, so we are seeing "All" of it.

The minibuffer is a small buffer which emacs uses to report error conditions to you, and to prompt you for information when necessary.

The cursor indicates where your text will appear when you type. You can reposition it using the mouse.

Buffers are the basic unit in emacs. Each file you edit has its own buffer. There can also be buffers for other purposes -- for example the minibuffer is used for emacs-user interactions; you can have a buffer in which you're running a Minischeme or Lisp interpreter; etc. At any given time, your emacs session will have multiple buffers around, although only a few may be visible at a given time.

Each buffer has an associated mode with it which controls the behavior of everything you do in it. For example, if you edit a file with the .ml extension, emacs will realize that this is an ML file, and will put you in SML mode, which knows how ML programs should be indented. Similarly, if you open a .c file, or a .cl file, its buffer will be in C-mode or Common Lisp mode, respectively, and know the syntax of that language. In addition to the indentation rules changing based on the buffer's mode, Control-key sequences may have different actions, additional pull-down menus will typically be added, and different sorts of syntax checks will be done for you (like balancing parenthesis in Lisp).

The *scratch* buffer is just that -- scratch space for you to type anything you want without associating a file with it.

Cancelling and Exiting

The two most important key sequences to know in Lisp are C-g and C-x C-c. C-g is used to cancel an action that you've started, and can be an excellent way to get out of mysterious features that you accidentally got yourself into by typing the wrong thing. Sometimes you may find yourself several levels down by mistake; usually in this case additional C-g's will get you out of it. It's harmless to C-g when you aren't doing anything, so when in doubt just keep typing it until things look normal again.

The other thing you'll want to know is how to exit. Use C-x C-c. Lisp will prompt you if you haven't saved all of your buffers, or if you've left any processes (like a Lisp or ML interpreter) running.

The Pull-down menus

Traditionally, all commands in emacs were accessed by control-key sequences. Modern versions of emacs come with pull-down menus, which obviates the need to remember all the key sequences. Most things that I will explain below can be found in the menus, so you don't need to memorize all the key sequences.

Reading and Writing Files

I think of emacs as being primarily an editor. To load a file from disk or start editing a new file, use C-x C-f (find file). In the minibuffer, you'll be prompted for a filename. If it exists, it will be loaded. Otherwise, it'll create a new file for you. If you're not sure of the filename, you can hit SPACE at any time to get a list of files which match what you've typed up to that point. Or use TAB for file completion.

To save a file, use C-x C-s. If there have been any changes, the file will be written to disk. To save it under a different filename, use C-x C-w (write file). You'll be prompted for a new name, and the buffer will be renamed (although the original file will still exist on disk).

Moving Around

Most of the time, you can move the emacs cursor around using the arrow keys or mouse. Sometimes, however, they won't work, and you'll have to use the keys: C-f, C-b, C-p, C-n to move forwards, back, to the previous line, and to the next line, respectively (note the mnemonic association. Most commands in emacs are mnemonic in some way, although sometimes the mnemonic is a little obscure).

To move forward or back word-by-word, use M-f and M-b. Note that Meta is acting as an intensifier: this is also works with a number of other commands such a backspace or delete. Speaking of delete, the key for it is C-d; on most systems the Delete key does the same thing as backspace.

To jump to the beginning of a screen line use C-a (First letter in the alphabet). To jump to the end of the line, use C-e.

To move more quickly, use C-v to move down a screenful, and M-v to move up a screenful. Use C-< to move to the beginning of a buffer and C-> to move to the end.

Marking a Region

If you can use a mouse with emacs, you can mark a region of text the way you would on any system: clicking and dragging. The highlight tends to disappear after you let go of the button, but the region will still be marked.

Otherwise, you have to mark the region by moving to one end of it and hitting C-SPACE. Emacs will respond with "Mark set". Then move to the other end and do whatever operation you want. This is pretty important: emacs always has a notion of where the last mark was set. The area between the mark and the point (or cursor) is known as a region. Thus, it always has a notion of the current region: namely, everything between "the mark" and your cursor. This can be a problem if you accidentally hit "delete" for example, because half of your file will disappear without you understanding why. Thank goodness for undo. (read on...)

Cutting, Copying, and Pasting

To cut a region, mark it and type C-w. Pasting is done using C-y (yank). To copy a region instead of cutting it, use M-w.

Another way to cut text in emacs is to use C-k (kill). This takes out the text between the cursor and the end of the line. If there is no text, then it takes out the linefeed and moves the next line up. Lines that you kill accumulate and can be yanked. Hence another way to cut and paste a few lines is to C-k a few times to kill the lines you want to cut, move to where you want to put the just-killed lines, and then yank (C-y).

Undoing

Emacs has infinite undo. Invoke it with C-/ (on some systems you have to use C-x u instead). Emacs keeps track of all changes made to a buffer since it was opened, so if you mess up you can almost always undo until things get back to a safe state.

Working with Buffers

When you start emacs up, you'll typically only see one buffer and the minibuffer. You can view multiple buffers at once, however, and this can be useful for viewing multiple files simultaneously. C-x 2 will divide a buffer window in half vertically giving you two buffers, each with their own status bar (note that you don't hold down Control while typing 2 here). Move between the buffers using C-x C-o or the mouse. Similarly, you can cut a buffer horizontally using C-x 3.

To hide the buffer your cursor's in, use C-x 0. Or to make the buffer your cursor's in fill up the whole window again, use C-x 1.

To view all the buffers in an emacs session, use C-x C-b. To switch to a different buffer, use C-x b and emacs will prompt you for its name.

Normally when you are not viewing a buffer, it's still there though it's not visible. To really drop one from your emacs session, use C-x k and supply the name. If it's modified, emacs will check with you before killing it. This can be useful if you need to get rid of a bunch of changes to a file.

You'll notice that emacs uses buffers to report information to you. These are typically surrounded with *'s to show that they don't have files associated with them. An example is the previously mentioned scratch buffer (*scratch*). You can switch between them or kill them just like any other buffer.

Functions/Key Bindings

Most every function in emacs is implemented somewhere using a Lisp routine. The common routines are bound to Control- and Meta- key sequences as we've been describing. Emacs lets you bind any function to any key sequence, which is part of the way in which it's so customizable. For example, in preparation for working with Lisp and ML, we've bound some of the most common functions to keys (detailed elsewhere).

You can also call functions that are unbound by using M-x. This allows you to type the name of the function, which will then be executed. For example, C-v is bound to the "scroll-up" function. You could therefore also scroll up using M-x scroll-up.

Searching/Replacing

To search forward for a string, use C-s. To search backwards (reverse), use C-r.

To replace all occurrences of a string in a buffer, use M-x replace-string. To replace some instances of a string, use M-x query-replace and you'll be prompted at every location of the string.

On-line help

Emacs has a lot of help available to you using C-h. You can then follow it with a single letter indicating what sort of help you want. ? or a second C-h gives you a list of options. a takes a string and lists all the emacs functions related to that string. k lets you type a key sequence, and explains briefly what function that key is bound to. f lets you type the name of a function, and it will describe it in detail. m gives you information about the mode of the current buffer (e.g. Common Lisp mode, SML mode, etc.). t takes you through a tutorial which is somewhat poorly written in comparison with this document. :)

Conclusions

This should be enough to get you started. Talk to me or other students if you have questions. A summary of this information has been placed on the quick reference page.
Shamelessly stolen from cse341 Winter '96
Modified by cary@cs.washington.edu Winter 2001 (Last update: )