Scott D. Anderson
Spelman College
© Spring 1998
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:
What if your keyboard doesn't have a meta key, not even one that is assigned? (This often happens if you are using Emacs via a modem and a terminal emulator.) You can always use the Escape key (marked ESC) as a prefix character to mean meta . Note that ESC is not a modifier key, so you first type ``ESC'' then type ``b'' but don't hold down the ESC key. Meta keys are not in the 7-bit ASCII character set, but the ESC character is (it's number 27).
By the way, sometimes these modifier keys are combined. If you hold down both meta and control and type a ``b'' you get the ``C-M-b'' command, which goes backword by one parenthesized expression. If you don't have a meta key, type ESC followed by C-b.
Note that you can get any command via M-x; it's just that the more common commands are bound to keys. When the documentation talks about ``key bindings,'' they're talking about the connection between keystrokes and commands. You can modify those key bindings if you want to.
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.
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.
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.
| ! | 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 |
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 |
| 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 |
| ! | 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 |
| 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 |
| ! | 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 |
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.
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.
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
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:
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.
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).