Customizing Emacs

Since Emacs is such a popular editor, it's worthwhile to know how to customize it to your own tastes. Once you learn how, you can feel quite confident using Emacs to do everything from read mail and news to doing ftp, to editing and debugging programs, and even troubleshooting your sanity with M-x doctor!

Your .Xdefaults File

One of the primary ways to modify your Emacs appearance is through modifying your ~/.Xdefaults file. On many distributions this is the file that tells X11 applications to act or look a certain way. Your distribution may have a different way of handling this configuration; please adjust these instructions to your needs. Naturally, if you're running Emacs in a tty terminal rather than X, your .Xdefaults file does not apply.

Some Emacs packages come complete with a file called sample.Xdefaults. This file, which can be cut or copied and pasted into your existing ~/.Xdefaults file, is handy because you can learn by example how to customize many Emacs features. The disadvantage, if any, is that you'll probably have to operate on faith in the beginning since much of this syntax might make no sense to you. Also, some of the original creators of these files had dubious artistic abilities, so it's possible that their color and font choices may not suit your taste.

Colors and fonts are the major customizations you'll want to make in this file. Some colors you might want to try that I find pleasing are ``ivory'' for the text area background, ``midnightblue'' for the text color, and ``bisque'' for the modline and menubar. (The colors for your terminal are normally defined in /usr/X11R6/lib/X11/rgb.txt. Consult this file for more colors you can play with.) I would keep your font selections simple to begin with, because many Emacs major modes require simple fonts to work properly. (A ``major mode'' is a specific Emacs application, such as mail reading or C program editing. A ''minor mode'' is a feature that may apply to one or many Emacs major modes, such as text wrapping or font locking.) You'll be able to do quite a lot with courier and helvetica; when you get more sophisticated, then you can deal with lucidasanstypewriter, pretty as that font is.

Try the following additions to your .Xdefaults file:

Emacs.background: ivory
Emacs.foreground: MidnightBlue

Emacs.modeline*attributeForeground:     MidnightBlue
Emacs.modeline*attributeBackground:     Bisque

Emacs.text-cursor*attributeBackground:  Red3
Emacs.pointer*attributeForeground:      Red3
Emacs.pointer*attributeBackground:      Red3
Emacs*menubar*Foreground:               MidnightBlue
Emacs*menubar*Background:               Bisque1
Emacs*popup*Foreground:                 MidnightBlue
Emacs*popup*Background:                 Bisque1

Emacs.default.attributeFont:  -adobe-courier-medium-r-*-*-12-*-*-*-*-*-iso8859-*

Be careful, these definitions work with my copy of XEmacs; if you use another version of Emacs, your milage may vary.

Your .emacs File

Common options are specified in the .emacs file in your home directory. This file simply contains Emacs Lisp code that is executed when you start the emacs program. If you use several major mode programs like LaTeX or VM-mode or CC-mode, then you'll want to add your customizations to an .emacs file.

Most .emacs files contain general startup information for Emacs as well as specific instructions for those modes you use frequently. Many distributions of Linux already contain a sample.emacs file somewhere in the Emacs documentation. That might be a good starting point.

Since Lisp can be long winded and confusing to follow, I would recommend using a lot of documentation in your .emacs file. Comments in Lisp are preceded with a single ``;'' character. Anything that follows a semi-colon is a comment. Here's an example:

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;          Basic Customization                                    ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(put 'narrow-to-region 'disabled nil)
(put 'eval-expression 'disabled nil)
(setq user-mail-address "dsj@dsj.net")
(global-set-key "\C-xw" 'what-line)

;; === This replaces the garbage collecting icon, this is snazzier.
;;  This comes from the sample.emacs file in my XEmacs rpm.

(if (featurep 'xpm)
      (let ((file (expand-file-name "recycle.xpm" data-directory)))
        (if (condition-case error
                ;; check to make sure we can use the pointer.
                (make-image-instance file nil
                                     '(pointer))
              (error nil))      ; returns nil if an error occurred.
            (set-glyph-image gc-pointer-glyph file))))



;; ===== This adds version checking prior to loading .xemacs-options
;;  9-1-97 from <ray@fred.net>

(cond
 ((and (string-match "XEmacs" emacs-version)
       (boundp 'emacs-major-version)
       (or (and
            (= emacs-major-version 19)
            (>= emacs-minor-version 14))
           (= emacs-major-version 20))
       (fboundp 'load-options-file))
  (load-options-file "/home/dsj/.xemacs-options")))



;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;                      TeX Mode Customization                     ;;
;;                        8-1-97  <dsj@dsj.net>                    ;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(add-hook 'tex-mode-hook
  (function (lambda ()
  (auto-fill-mode)
  (font-lock-mode)
  (setq tex-dvi-view-command "xdvi")
  (setq tex-print-command "dvips")
  (setq tex-dvi-print-command "dvips")
)))

You could probably create this file and save it in your home directory, and it would work with your copy of Emacs. Basically, all the above file does is turn on some features that are disabled by default, it sets my mail address, replaces a dull icon with a snazzier one, and checks the version of Emacs so it is compatable with code loaded from another configuration file called .xemacs-options. Then I begin the configuration of specific packages, such as LaTeX.

Oh, you'll notice I also keep track of when I made certain changes and who I got the code from. I lifted some code from a friend named Ray, so if it bombs out I know who to ask for advice. If my own code doesn't work, I'm just flat out of luck!

In my .emacs file, I go on to specify many options with my mail reader, called VM. I have added several pieces of software including PGP from Phil Zimmerman and Mailcrypt from the GNU organization. I also use the SuperCite and XFace packages for Emacs and VM, along with a couple of MIME packages. All of these packages are configured to my taste in my .emacs file.

I also specify my preferences for other major modes for Emacs, such as telnet, ftp, gnuscape (a web browser), command shell mode, and CC and other programming modes. Further, many Emacs major modes allow you to use a separate configuration file just for that mode.

Along with my .emacs file, I use a .vm file and a .gnus file which govern my mail reader and my news reader respectively. This is yet another way of modifying Emacs defaults for specific modes. These files are simply more collections of Lisp code that gets initialized when Emacs starts a major mode. I've seen other people use similar configuration files for other Emacs major modes.

Conclusion

Books have been written just on using and configuring Emacs, so I can certainly not cover the entire subject in a single article. (I can recommend Learning Gnu Emacs by Cameron et al, from O'Reilly, as a good reference.) However, this article might get you started with customizing the Emacs ``Mega-Editor.'' This editor is considered by many to be THE Unix killer app, and once you start customizing it, you can begin to get an idea why they think so.



David S. Jackson
Sun Jan 18 22:19:48 EST 1998