The Mother of All Editors

About Emacs

Most everyone has heard of Richard Stallman (aka RMS), the activist and driving force behind GNU and the Free Software Foundation. As a programmer, though, he's mainly famous for writing Emacs and the GCC compiler. Emacs has remained the editor for many people — it is currently in version 22.

Emacs is open-source software under the GPL, of course, so there are many "forks" — versions where someone has taken RMS's code and modified it to create a new package. XEMACS is the best known (it's available via the Ubuntu repository, for instance), but Mg, MicroEmacs, and Jove are also used at some sites. What I'm describing here, though, is genuine GNU Emacs, which Stallman still maintains.


Emacs is noted for the following features:

Emacs and Lisp

Emacs is actually a very small set of primitive editing routines written in C, supplemented by millions of lines of Lisp code. Emacs is essentially a Lisp interpreter whose programs are designed to act on the file being edited. Emacs customization is done by writing (or borrowing) snippets of Lisp code for a particular purpose, and then having Emacs execute that code. This looks quite intimidating at first glance, but Lisp is probably the simplest programming language there has ever been.

Every Lisp item is either an atom (a number, string, variable name) or a list, which is a set of atoms and/or lists enclosed in parentheses. A function is a list of the form (fname arg arg...) where fname is applied to the arguments (which can be atoms or other functions) and the result is used in place of the whole expression inside the parentheses. As a simple example, (+ 2 3) returns "5", while (sqrt (+ (* 2 3) (+ 5 5))) returns "4". You have now learned the entirety of Lisp syntax — that's it!

Here's a short example of a code snippet to perform a function RMS didn't feel like providing: counting the number of words in the file being edited. I'll walk through it line by line.

(defun count-words ()
  "Count the number of words in the current buffer;
print a message on the bottom line with the result."
    (let ((count 0))
      (goto-char (point-min))
      (while ( < (point) (point-max))
        (forward-word 1)
        (setq count (+ count 1))
      (message "buffer contains %d words." count)

All bold words in the above are built-in Lisp functions, while those in italics are atoms. Here what they do:

(defun funcname (args) code)
Define and name a function which expects args as input and executes code.
Allow users to execute the new function. (Some functions are designed to run only behind the scenes.)
(save-excursion code)
Save the location of the cursor (aka "point") and restore it after code has executed.
(let ((tempvar1 value)(tempvar2 value)..) code)
Define and initialize temporary variables for use while code is running.
(while condition code)
As long as condition is true, keep executing code.
point, pointmin, pointmax
Functions which return a number — the location of the cursor, the start of the file being edited (usually 1), and the end, respectively.
(setq var somevalue)
Set the variable var equal to somevalue.

If someone is anal-retentive and wants to check how many words are in their document every minute or so, it makes sense to assign count-words to a key so it's always ready to run. Assigning an action to a key is done by the Lisp function (global-set-key key action). Thus

(global-set-key [F12] 'count-words)
would do the trick. (That apostrophe means "what follows is a NAME, not something to be actually executed".)

Saving your Customizations

Emacs looks for a magic file name — .emacs — in your home directory. (On Windows, it goes in C:\.) .emacs contains Lisp code which will be executed before your editing session begins. If you need count-words often, then you should save the definition (and the key assignment) in .emacs, and it will be available at all times. Over a period of time, the file can get pretty large with all the goodies you have written or stolen. (There are thousands and thousands of Lisp functions for Emacs on the net, ready to be cut and pasted.)

Note that since all incarnations of Emacs simply process the Lisp code they are given, Emacs works exactly the same on all platforms! The .emacs file on my Linux machines is identical to the one on my Windows XP machine.

The downside of this customization is that your Emacs might not work anything like my Emacs. It is perfectly possible that you have (global-set-key J 'delete-all-files). This means I have to do one of two things: either learn how to live with the "vanilla" editor, or carry my .emacs file on my keychain for emergencies.

Example — HTML Helper

I lifted the "HTML Helper" set of Lisp code from the Internet; it isn't part of the standard Emacs distribution. Since it is straightforward Lisp, I promptly customized it and added a few more features that were suitable for my particular needs.


Installing Emacs

If you're running Linux, simply install the emacs22 package with the package manager of your choice.

If you're running [walking?] Windows, get the install wizard from the GNU Windows repository. Download the file, unzip it, and execute the installer.

Last modified: Thu Feb 14 22:28:21 CST 2008