August 9, 2006

Emacs tips: Customize your Emacs experience

Author: Nathan Willis

One of the principal advantages of Emacs over competing editors is how flexible and customizable it is. In fact, in several other "Emacs tips" columns, you may find references to customizing your setup. It's a big topic, so this is a quick start guide to the fundamentals: the .emacs file and basic customization techniques.

Most of Emacs itself is written in Emacs Lisp, a programming language that as its name suggests is a variant of LISP. This means it is easy to read, has a simple syntax, and can customize almost every feature of Emacs.

But don't worry; you don't have to learn a new language to start tuning your environment. A few simple statements are all its takes to perform the most common customizations: setting preferences, defining useful macros, and mapping functions or macros to keystroke combinations.

Setting preferences

The easiest way to customize Emacs for your personal use is to edit your .emacs file. This file lives in your home directory and is read and evaluated each time you start Emacs. Your Linux distro may create a .emacs file for you; I have noticed that it is popular to pre-fill the file on GUI systems with some snazzy modernizations such as scroll wheel support. But if it doesn't exist, you can create it in Emacs yourself; just start with a blank file.

To save simple preferences in .emacs, you add a setq expression to the file. This is a basic variable setting command of the form (setq variablevalue).

For instance, if you are routinely short on time and can't spare the fraction of a second between when Emacs starts up and you open the first file, you can skip over the default Emacs startup message with (setq inhibit-startup-message t).

This set the Boolean variable inhibit-startup-message to true. Other variables take numerical values, of course, and still others take Emacs functions. For instance, to set the default major mode to text mode, you would use (setq default-major mode 'text-mode). The single quote tells Emacs that text-mode is a string constant (i.e., the name of a mode, not a variable named text-mode).

In both of the above cases, the variables set are predefined -- we did not make up inhibit-startup-message on the spot. A comprehensive list of Emacs' variables is found in the Variable Index section of the GNU Emacs manual.

I'll let you in on a little secret: whenever I decide to set some Emacs preference for the first time, I usually just search on the terms in question and the word Emacs; what turns up near the top is frequently a useful tutorial written by some experienced Emacs user tackling the same issue, and I learn what I need to know quicker than I would reading through the encyclopedic official manual.

Some old-school Emacs gurus might bristle at that suggestion, so let me temper it by saying that the Web search method of Emacs education is not much of a time-saver for basic variable questions, but it can be real life-saver when it comes to writing your first Emacs Lisp functions.

For instance, soon after I began writing with OSTG, I knew I would need a word count function, which Emacs does not have built-in. Searching around the Web led me to an excellent tutorial on defining a short word count function and binding it to a keystroke combination for easy access.

Functions and key bindings

The tutorial is from Robert J. Chassell's amazingly helpful Programming in Emacs Lisp, which is available online. I'm not going to touch the function writing topic itself, but I will show you what the word count function looks like, so we can talk about loading it into your .emacs file.

The function Chassell presents is called count-words-buffer, and it looks like this:

;;; Final version: while
(defun count-words-region (beginning end) "Print number of words in the region." (interactive "r") (message "Counting words in region ... ") ;;; 1. Set up appropriate conditions. (save-excursion (let ((count 0)) (goto-char beginning) ;;; 2. Run the while loop. (while (and (< (point) end) (re-search-forward "\\w+\\W*" end t)) (setq count (1+ count))) ;;; 3. Send a message to the user. (cond ((zerop count) (message "The region does NOT have any words.")) ((= 1 count) (message "The region has 1 word.")) (t (message "The region has %d words." count)))))) ;; Count the words in the entire document (defun count-words-buffer () "Count all the words in the buffer" (interactive) (count-words-region (point-min) (point-max) ) )

All it is is a pair of functions defined in Lisp code with defun. You can copy and paste the code directly into your .emacs file, and when you launch Emacs, the function definitions will be read in and made accessible just like all of Emacs' built-in functions.

But as long as we are at it, we might as well bind this new function to a key combination, so we don't have to type out the function name every time. The syntax for binding a keystroke to a function is (global-set-key "keystroke" 'function).

Chassell's recommendation for count-words-buffer is Crtl-c Ctrl-c, which looks like (global-set-key "\C-c\C-c" 'count-words-buffer). Note that there is an escape-sequence syntax for the control keys: \C means "hold down Ctrl" -- similarly, \M means "hold down Meta."

You can add key bindings for new and existing Emacs functions. Remember in the "Checking spelling and syntax" article I mentioned how ispell-complete-word was bound to Meta-Tab, which is inconvenient because Meta-Tab usually equates to Alt-Tab, a key combination intercepted by most window managers? Well, now you know the solution: remap the function to a new key combination with global-set-key: (global-set-key "\C-c\C-v" 'ispell-complete-word). What a relief!

External packages

The last bit of beginner's .emacs customization to look at is using external packages. The Emacs gurus who might have bristled at my suggestion to eschew the manual a few paragraphs ago are responsible for some heavy-duty Emacs add-ons. Many are available from your Linux distro and can be installed with your system's package management tool. Others you may discover on the Internet and have to install yourself. That's usually just a matter of copying the .el files to a directory where Emacs can find them, somewhere under /usr/share/emacs.

There are Emacs packages for additional major modes, database connectivity, full interface makeovers -- you name it. I use the eldav package heavily; it enables Emacs to directly access files on WebDAV servers as if they were local. This helps me keep things synchronized, especially when I am traveling. The eldav package is part of Ubuntu's standard repositories, so I did not have to install the files by hand, but I still need to tell Emacs to load it up.

To do this every time, all I have to do is add the expression (require 'eldav) to my .emacs file. Then, whenever Emacs launches, it finds the eldav package and activates it. Here again, the single quote tells Emacs that eldav is the literal name of the package, not some other expression or variable.

Customizing Emacs opens up a whole new world of editor flexibility. If you are interested in learning more in-depth, I recommend picking up Chassell's Programming in Emacs Lisp -- the tutorials are well-written, and the examples are useful, down-to-earth stuff as typified by the count-words-buffer example. But if you are happy enough to get by with just a few simple customizations, this article should leave you prepared for most of what you will see when you find a good add-on on the Web and want to incorporate it into your Emacs experience.

Click Here!