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:
Note that both Emacs and VI, although very different in design, started out as pure text-mode terminal editors and have brought forth GUI versions.
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."
(interactive)
(save-excursion
(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:
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".)
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.
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.
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
emacs-22.1-bin-i386.zip, unzip it, and execute the
installer.
