Introduction to the Emacs Editor

Scott D. Anderson[*]
Spelman College

© Spring 1998

Overview of Emacs

Emacs is an editor available on most UNIX systems; indeed, there are implementations for Windows 95 and the Macintosh, too. Like vi, which is built-in to all UNIX systems, Emacs is a screen editor. Unlike vi, Emacs is ``modeless'' editor, which means that you don't have to switch to ``insert mode'' in order to put characters into the file.[*] By default, any character you type is put into the file at that point. (The ``point'' is usually marked by a rectangular block on the screen.)

Since normal characters like ``a'' or ``$'' are inserted into the file, you have to use abnormal characters to give commands to Emacs. There are essentially four ways to give commands to Emacs:

Finally, when you start XEmacs you'll notice some menus and icons, so you can also issue commands by using the mouse. Power users spend most of their time using keyboard commands, so that's what I'll describe in this document.

One other distinction between emacs and vi is that emacs allows you to edit several files at once. The window for emacs can be divided into several windows each of which contains a view into a buffer. Each buffer typically corresponds to a different file. Many of the commands listed below are for reading files into new buffers and moving between buffers. This distinction results in a very big difference in how the two editors are used. Most vi users enter and exit vi many times, as they edit files and save them. Consequently, vi has been built to start very quickly. Most Emacs users, on the other hand, start up Emacs once when they login and only exit it when they logout. Instead, they just save buffers and switch to another window where they run the compiler or whatever. Consequently, very little effort has gone into make Emacs start quickly.

XEmacs versus Emacs

You may have noticed that sometimes I say Emacs and sometimes XEmacs. There have been many implementations of Emacs over the years (the editor is over twenty years old) and so the word ``Emacs'' often means, generically, any one of those implementations. They are quite similar, although many features have been added over the years.

Currently, there are two major implementations: GNU Emacs, written by the Free Software Foundation (FSF) and XEmacs, written by the XEmacs organization. They are both free, both very good, and very similar. Thousands of organizations all over the world use one or the other, and sometimes both.

Spelman has one of each. Our XEmacs has lots of features to work with the X Windows system, so it can handle mouse clicks, can run in its own window, and stuff like that. There are versions of GNU Emacs that can do that as well, but not Spelman's. Our version of GNU Emacs is not ``X aware,'' so it can't handle mouse clicks, run in its own window, or any of that. If you login to Spelman from home, so you're running over a modem and couldn't use the mouse anyway, you'd find very little difference between them. If you login to one of the SPARCs, you'll probably prefer XEmacs.

In this document, I'll refer to Emacs when I mean any Emacs, and I'll say ``XEmacs'' when I am talking about that specific program.

Using Emacs, Getting Started

To use emacs on a file, type emacs filename or xemacs filename.[*] If the file exists, then the first screen's worth of the file will be displayed; if it doesn't exist, a help message will be displayed.

Alternatively, you can start up Emacs without mentioning a file. It will come up and have a bunch of information about getting help and running the tutorial. The initial buffer will be *scratch*. You can then use commands to read in files as necessary.

To give you a head start, the following lists some basic commands you will need to know to use Emacs to edit a file. An exclamation point to the left of the commands indicate those to learn immediately.

Help Commands

! C-h help-command, prefix character for lots of useful help commands
! C-h t help-with-tutorial, command to run the tutorial
  C-h i info describes most of the emacs commands in man style pages
  C-h k describe-key tells you what a particular keystroke does
! C-h a command-apropos, prompts for a string and then searches for all emacs commands that contain that string
! C-h ? help-for-help, describes how to use the help facilities

File Reading and Writing Commands

! C-x C-f find-file, first prompts for a filename[*] and then loads that file into a editor buffer of the same name
! C-x C-s save-buffer, saves the buffer into the associated filename
  C-x C-w Write named file, prompts for a new filename and writes the buffer into it

Cursor/Screen Movement Commands

The arrow keys should work on most computers, though they probably won't work over a telnet connection, say if you are dialing in from home. In any case, you can use all of the following commands.

! C-a put cursor at beginning-of-line
  C-e put cursor at end-of-line
! C-b go backward one char
! C-f go forward one char
! C-n go to next line
! C-p go to previous-line
  C-v scroll-up by one screenful
  M-v scroll back by one screenful
! M-< go to beginning-of-buffer
! M-> go to end-of-buffer
  M-b go backward one word
  M-f go forward one word

Copy and Delete Commands

  C-d delete-char
  M-d delete from cursor to end of word immediately ahead of the cursor
! C-k kill-line, delete the rest of the current line
! C-@ set-mark-command, the mark is used to indicate the beginning of an area of text to be killed, copied or whatever
! C-w kill-region, delete the area of text between the mark and the current cursor position
  M-w copy-region-as-kill, copy area between mark and cursor into kill-buffer so that it can be yanked into someplace else
! C-y yank, insert at the point whatever was most recently killed or copied

Search Commands

! C-s isearch-forward, incremental search prompts for a string and searches forward in the buffer for it. It starts searching immediately
  C-r isearch-backward, like isearch-forward, but goes backwards
  M-% query-replace, prompts for a search string and a string with which to replace the search string

Window and Buffer Commands

  C-x 0 deletes current window
  C-x 2 splits current window into two parts, so you can edit at two different locations in the same file or so that you can view two different files at the same time
  C-x b switch-to-buffer, display a different buffer on the screen
  C-x o move the cursor to the other window (assuming that you have two windows/buffers open at once)
! C-x C-b list-buffers, shows the buffers currently loaded into emacs

Exiting Emacs, Fixing Mistakes and Other Important Stuff

! C-x C-c save-buffers-kill-emacs, when you are finished editting, it saves editted but unsaved buffers and then returns you to UNIX
! C-g keyboard-quit, if while typing a command you make a mistake and want to stop, this aborts commands in progress
  C-u universal-argument, if you want to do a command several times type this command followed by a number (for the number of times) followed by the command you wish repeated. It's also used to modify the behavior of some commands.
! C-x u undoes the last command typed, in case you made a mistake
! M-x execute-extended-command, prompts for the name of an Emacs command, allows you to execute commands if you know roughly what it is called but cannot remember the key strokes for it

Customizing Emacs

Perhaps the biggest difference between Emacs and vi (and other editors), is that Emacs is designed to be customizable. It has a built-in way to run programs written by users (in a programming language called Emacs-lisp or ``elisp'' for short) and loaded into Emacs. Many, many people have written extensions to Emacs to do all kinds of things. When you're more experienced, perhaps you'll be one of them.

There are simpler ways to customize Emacs than writing code. Often it's as simple as setting the value of a variable. For example, suppose you would like Emacs to save backup versions of your files. You would set some variables to true by doing the following:

(setq make-backup-files t)
(setq version-control t)

Where do you put these variable settings? You put them into a file called /.emacs. When Emacs starts up, it reads in and evaluates all the code in that file. There is a sample .emacs file; it should be in the same place where you got this file. It is called dot-emacs so that it'll be easily visible.

Backspace versus Delete

On PCs, the convention is that the Backspace key deletes the character to the left of the insertion point (the previous character), and the Delete key deletes the character to the right of the insertion point (the next character). On UNIX systems and other older operating systems (Multics, TOPS-20, ...), it's typically the case that the Delete key deletes the previous character and the Backspace key does something else, such as backing up but not deleting the character.[*] Consequently, the Backspace key wasn't used very often. Note, by the way, that Backspace and Delete are both ordinary ASCII characters: Backspace is the same as C-h, which is character 8; Delete is character 127.

Early in Emacs history, the implementors decided that since Backspace wasn't used much, and that C-h would be mnemonic for ``help,'' they assigned C-h to be the prefix character for all of the built-in help facilities. Of course, this meant that the Backspace key also was a prefix character for help, since (at the time) the computer couldn't tell the difference between backspace and C-h. Since most people didn't use the Backspace key, this wasn't a problem.

Nowadays, many people have PCs and are used to using Backspace to delete the previous character. The standard Emacs key bindings are confusing to such people. If you're one, here's what you can do. Put the following into your /.emacs. That will customize Emacs to give you the behavior you want.

(load-library "delbackspace")

An example of this is in the sample .emacs file.

SUN/OpenWindows Annoyance

As you now know, Emacs uses the meta key for many commands, including some that you might come to use pretty often. How often do you use ``cut or ``copy'' with a PC word-processing program? Pretty often, I guess. The Emacs equivalents are ``C-w'' and ``M-w,'' respectively. Another function you might come to use pretty often is ``M-q'' which does various things for different kinds of files, but typically does things like re-indenting code or filling a paragraph (making all the lines about the same length).

The problem is that, on a SUN workstation running OpenWindows (the window manager), the window manager intercepts the M-w and M-q keys instead of giving them to Emacs. If you type M-w (in any window, not just an Emacs window), the window manager iconifies the window (turns it into an icon). If you type M-q in any window, the window manager kills the window.

If you're happy with that, you can just leave it that way, but you'll have to remember that if some documentation advises you to use the M-w or M-q commands, they won't work. You'll have to use the M-x equivalent.

If, like us, you want to use the Emacs M-w and M-q commands, you'll have to tell the window manager not to intercept them. If you put the following into your /.Xdefaults file, the window manager will pass those keystrokes through to Emacs. (Of course, you don't really have to put the comment lines in your .Xdefaults, but it won't hurt.)

! In Emacs windows, turn off M-w and M-q as OLWM keyboard accelerators, so
! that they are available as Emacs commands.  The following incantation is
! taken directly from the man pages for OLWM.

olwm.Client.Emacs.MenuAccelerators: false

Colors

The .emacs that we have created turns on font-lock as a minor mode (in other words, an added feature) to most of the XEmacs major modes. However, it just uses the default colors. These have been, amazingly enough, intentionally set to bad colors, to encourage people to select their own colors. Here's how to do it:

1.
Go to the "Options" menu and select "Customize," then select "faces." (The face specification says what something looks like, including color, font, and other features.)

2.
This will prompt you in the minibuffer for what face you want to customize. (Remember that the minibuffer is the place at the bottom of the screen where Emacs always does prompting, such as for M-x commands, or for filenames when you use C-x C-f.) If you want to do a lot of customizations, you can use the default ("all") but that'll take a long time. Instead, just type "font-lock-keyword-face". Use tab completion to make the typing easier.

3.
That will bring up a special buffer for doing the customization. Click with the middle mouse button on "foreground" and you will be prompted (again at the minibuffer) for the new color. Try "red." Or try "?" if you want a list of colors (this may take little while).

4.
Click on "save" and "done" and your face foreground color is changed.

For more information, see the XEmacs FAQ, which is available via C-x F. (That's a capital ``F,'' not a lowercase ``f.'')

Flow Control

This section is aimed at people who may use Emacs via a modem and terminal emulator. If you always use Emacs from the computer's console, you can probably skip this section.

Terminals are typically slower than the computers they are communicating with, and so when the computer starts blasting a bunch of characters to the terminal, the terminal needs a way to say ``Wait! I need to catch up.'' When it catches up, the terminal says ``Okay, go ahead,'' and the computer sends some more data. This is called flow control , since the terminal needs to be able to control the rate that information flows in.

For many years, flow control was done by the terminal sending either a ``control S'' (stop) or a ``control Q'' (resume) to the sender. In fact, you can still do that at most Unix prompts. To try it, just sent a large file or listing to the screen (say by cat .login or ls -la). Then type C-s and the output should freeze; type C-q and it resumes.

Notice that I used Emacs notation for C-s and C-q; I did so because those are legitimate Emacs commands that you might want to use (incremental-search and quoted-insert, respectively). If you use one of those commands (or even type one accidentally) and you get odd behavior, particularly if the screen seems to be frozen, that's probably because the C-s or C-q is being interpreted by the computer as flow-control, instead of being passed on to Emacs.

If that happens, the easiest solution is to give the command M-x enable-flow-control RET and you can then type C-s as C-\, and C-q as C-. You can also put (enable-flow-control) into your .emacs file.

Parting Words

Emacs has many, many other useful commands. A partial listing, more compact than this document, is in the bindings.text file that is in the same place where you got this file. As you get more proficient at it, try listing the key bindings (C-h b) to find other commands. There is also an enormous amount of on-line documentation, either via C-h m (help with this mode), C-h F (the FAQ), or C-h i (the info pages, which are hyperlinked manual pages).



Footnotes

...Anderson
My thanks to J. Ross Beveridge of Colorado State University, who sent me an Emacs primer that became the core of this document.

...``control-b''
This is actually a ASCII character. It's number 2 in the ASCII code, while an ``B'' is number 66 and ``b'' is number 98. Do ``man ascii'' to see a table of the whole ASCII character set. All the control codes are in the beginning, so ``control-a'' is number 1, ``control-b'' is number 2 and so forth.

...minibuffer
The minibuffer is a line at the very bottom of the Emacs window, right below the status line, (which is the one that tells you what file you're editing and information like that). The minibuffer is used anytime Emacs needs more information from you in order to execute a command; and it's used whenever Emacs to tell you something short in response to a command.

...command,
Emacs has what is called completion , meaning that as you type in a command, you can type a space or tab at any time and Emacs will either fill out the rest (if there is only one possible completion) or it will list the possible completions. This tremendously reduces the amount of typing and prevents mistakes.

...file.
On the other hand, Emacs has lots of ``modes'' for editing different kinds of files. The word ``mode'' is somewhat overused in computer science.

...filename.
If you want XEmacs to run in its own window, which you typically would, follow the command with an ampersand (&) and it will run ``in the background'' in its own window.

...filename

...
Recall that Emacs has completion , meaning that as you type in a filename, you can type a space or tab at any time and Emacs will either fill out the rest (if there is only one possible completion) or it will list the possible completions. This tremendously reduces the amount of typing and prevents mistakes.

...character.
This makes it possible to actually underline something, by typing it and then backing up and typing underline characters.



Scott D. Anderson
7/22/1998