Beginners Guide to Emacs

by
Donu Arapura

Pros.

  1. Under the X-interface, you can select text or basic commands with the mouse (unlike vi).
  2. Until you save the file you're editing, all actions are reversible. And it saves a back up (ending with ~) just in case you really screw up.
  3. It's very powerful. Not only can it be used to edit files, but also to read mail, read news, and run other programs (e.g. TeX, shells...) and parse the output or error messages in some useful way.
  4. It's customizable in the extreme. More about that later.


Cons.

  • It can be slow, especially on an old machine. However, emacs can be run remotely on a faster machine. Also it's a good idea to have only one copy of emacs running for as long as you think you need it.
  • It has a reputation of being hard to use. Part of this stems from the fact that emacs traditionally required users to memorize a long list of complicated key combinations. Under the X-interface however, this is not so critical.


    Basics and Some Terminology.

    Usually, emacs is invoked by typing "emacs", or "emacs &" (if you use solaris, you may need to fiddle with your path first). To edit a file select "open file" from the menu, to save it select "save buffer", type Control-g to cancel a previous command, and select "exit emacs" to quit.

    Buffers: A buffer is a copy of the file that you are editing. Until you actually save the buffer, you can undo commands many times. Emacs can handle several buffers simultaneously - which is why you won't need to run multiple emacs's.

    Minibuffer: The strip along the bottom of emacs. For certain commands such as searches, you may be required to type information into it.

    Modes: Emacs is modal in the sense that it "knows" about many different kinds of files (TeX files, shell scripts, C programs, html files...), and will change behaviour accordingly.


    Invoking Commands from Keyboard.

    Some commands cannot be accessed through a menu, but ALL commands can be invoked from the keyboard. Unfortunately, this is where emacs gets its bad reputation. Emacs often uses multiple key combinations where some keys have to be "modified" by also pressing the control or esc keys first. A complete list can be found under "List Keybindings" of the help menu ( "C-" and "M-" means control and esc). A brief list: quit (C-x C-c), save buffer (C-x C-s), open file (C-x C-f), search forward (C-s), search backward (C-r), cancel last command (C-g).

    On Sun keyboards, keys labeled "page up", "undo", "cut"... should have the expected meanings.

    It's possible to define your own key bindings in your .emacs file. See below.


    More Help.

    There's considerable help from within emacs itself (obviously under the help menu). Beyond that,there are news groups, a number of resources on the web, and even a book or two. If all else fails and you need to curse, try M-x doctor


    TeX Modes.

    Emacs comes prebuilt with a basic tex mode, but there is a more refined one called AUC-TeX that I've installed in my directory (but can used by anyone, as I'll explain below). In these modes, you get visual clues about the correctness of your file: balancing of $'s, braces..., and (optional) colour coding. TeX or LaTeX can be run directly under these modes. In the case of AUC-TeX, emacs can parse the error messages and go directly to the offending lines of the appropriate source files.


    Customization.

    1. X resources.

    The external appearance of anything running under X can be set in an initialization file such as .Xdefaults. To increase the size of the fonts that emacs uses, you can append THESE LINES to your .Xdefaults file, or another way to copy these lines to the end of your .Xdefaults file is to type:

    cd
    cat .Xdefaults ~dvb/pub/sample.Xdefaults > tempXdefaults
    mv -f tempXdefaults .Xdefaults

    2. .emacs

    When emacs starts up, it reads a file in your directory called .emacs (if it exists). This file can make emacs do pretty much anything. It is in fact a lisp program, so it requires a little know-how to create from scratch. I've written a sample .emacs file that you can download here or you can copy it from my account via UNIX by typing

    cp ~dvb/pub/sample.emacs .emacs

    then modify it to suite your taste. It's set up to invoke AUC-TEX and colour coding. If you want to make changes, the main thing to keep in mind is that a sentence in lisp looks like (...), and anything to the right of a semicolon is treated as a comment.

    ADDENDUM, 9/22/99

    We have now installed AUC-tex system-wide for the new emacs (which is at /pkgs/gnu/bin/emacs for both older SunOS and newer Solaris machines). So if you use that and remove

    (setq load-path (cons "~dvb/elisp" load-path))

    and just leave

    (require 'tex-site)

    in your .emacs, you should be OK.


    ADDENDUM, 1/29/02

    Check out this New version of xdvi that connects to the emacs editor for interactive TeX viewing and editting.