HMC Homepage      CS Home

How to Write and Edit Documents Using Emacs

There are a number of text editors available on Knuth, one of which is GNU-Emacs. It is a very powerful and flexible editor.
  1. If you don't have it already, you will probably wish to obtain a copy of the startup file .emacs . This can be done using the command

    % cp /mnt/shared/skel/generic/.emacs .emacs

    This is a file that Emacs will read when it is run. The default .emacs does very little, but it can be customized by the user to run a wide variety of commands.

  2. To use Emacs, type

    % emacs filename

    If the specified filename exists, Emacs will read it into the buffer. If the file does not exist, Emacs will open an empty buffer with the specified name.

    The second to last line on the screen, displayed in inverse text, is called the "mode line." The mode line contains several pieces of useful information, including the buffer name, the current mode (see below), and position within the buffer.

  3. Now you may edit the file. On many terminals, the arrow keys will operate as you would expect, but this is not guaranteed. For a listing of the canonical cursor movement keys in Emacs, please see the keybindings list. The Delete key will delete the character before the cursor.

  4. In order for the changes you've made to become permanent, you will need to save your buffer to a file. To do so, simply type C-x s. That is, simultaneously press the Control and X keys, and then press the S key.

    Emacs will display a message like

    Wrote filename

    indicating that filename has been saved. The two asterisks at the left end of the mode line will also disappear, indicating that the buffer has not been modified since the last save.

    There are two ways of entering commands into emacs. The most commonly used commands are bound to keyboard sequences using the Esc or Ctrl key. For example, ^V (typed by holding control and pressing V) moves forward one page and Esc-v (typed by pressing and releasing escape, and then pressing V) moves back one page.

    emacs has several ways of designating combinations of keys. The combination of holding Control and typing "v" can be written Ctrl-v, ^V, or C-v, and the combination of typing the Meta key (either pressing and releasing Escape or holding the Alt key) can be written M-x, Esc-x, or Alt-x.

    The second method of command entry is called the command line. Typing M-x will cause a M-x prompt to appear at the bottom of the screen. You then type in a command and emacs will execute it. The commands here include all of the commands bound to keys as well as many others. For example, with the default set up M-x scroll-up is the same function as ^V but M-x apropos can only be invoked at the command line.

  5. Emacs includes a number of useful help functions, each of which is accessed by typing Esc-h key where key is any of a number of options. NOTE: Emacs documentation will tell you to access help with C-h. This will not work on Turing. Use Esc-h instead. Note that while Ctrl is pressed at the same time as it's associated key, Esc must pressed and released before the associated key is pressed. Some of the more commonly used help functions are:

    • Esc-h t

      Invoke the Emacs interactive tutorial. This will teach you the basics of editing with Emacs in a fairly short time. Highly recommended.

    • Esc-h a

      Apropos search. Emacs will list all commands containing a key word.

    • Esc-h c

      Asks for a command(e.g. C-v) and gives a very brief description of said command.

    • Esc-h k

      Similar to Esc-h c, but gives more information.

    • Esc-h f

      Asks for a function name (e.g. auto-fill-mode) and describes the function.

  6. Emacs has a number of modes in which it can operate. These are generally optimized for editing specific document types. The mode you will likely use most often is Text mode. Other modes include C mode and Fundamental mode. You can change the mode if you wish. For example, if you were to find yourself in C-mode, you could change to Text mode as follows

    Esc-x text-mode

    All of the above modes are known as Major Modes. You must choose one at any one time. There are also Minor Modes, which may be toggled on and off at will. The most commonly used minor mode is auto-fill-mode which toggles automatic insertion of line breaks at the right hand margin. For a full listing of modes, try

    Esc-h a mode.

  7. To exit emacs, first save your file if you wish to keep the changes you have made, then type C-x C-c. That is, Ctrl-X and then Ctrl-C. If you have not saved since your last editing change, Emacs will ask you if you wish to save. If you do, hit Y, and if not, hit N. If you choose N, Emacs will ask you if you really wish to exit without saving the buffer, giving you one more chance to change your mind. If you choose Y or if you saved at any point in there, Emacs will exit.

Notes:

There are several sources for further information on using Emacs. A good place to begin is the tutorial mentioned above (type Esc-h t while inside Emacs). The man page for Emacs (man Emacs) describes command line and Xwindows options. If you really wish to get to know Emacs intimately, the info page (info emacs) describes Emacs in excrutiating detail.

Emacs has many, many features not detailed in this Qref, ranging from multiple buffers and windows to a built in News reader and a psychoanalyst. To learn about these and other Emacs capabilities, make use of the documentation listed above.

Emacs will use C-Q and C-S for certain commands. Unfortunately, C-S will freeze certain terminals, and C-Q is used to unfreeze them. If you find yourself using such a terminal, type

Esc-X enable-flow-control

This will allow you to use C-\ in the place of C-S, and C-^ (Ctrl, Shift, and the ^ (above 6) keys simultaneously) for C-Q.


Copyright (c) HMC Computer Science Department. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.1 or any later version published by the Free Software Foundation; with the no Invariant Sections, with no Front-Cover Texts, and with no Back-Cover Texts. A copy of the license is included in the section entitled ``GNU Free Documentation License.''

HMC Computer Science Department
Olin Science Center
301 E. Twelfth Street Claremont, CA 91711-5980 USA
PH : (909) 621-8225 FX : (909) 621-8465
Info Email: CS Staff or Admission Office
Last Modified Tuesday, 22-May-2001 14:34:20 PDT