Many programs have start-up settings, which they read from a configuration file or from some database. Emacs is no exception: when it starts, it reads a file called ".emacs" from your home directory. However, the big difference is that .emacs does not consists of simple "key=value"-pairs. Instead, your .emacs is an Emacs-Lisp (elisp) program itself. At startup, Emacs interprets ('evaluates') your .emacs, and, hopefully, give you the desired results. One of the goals of this blog is to show you how.

Through .emacs, you have an almost unlimited power to make emacs do what you want. You can change the colors or the keybindings, but you can also make much deeper changes. And you can make it depend on the phase of the moon, if you want.

The flip side of all this power is that editing your .emacs is a bit harder than clicking through some 'Preferences'-dialog; this is a price that the typical emacs user gladly pays, but it does make learning emacs a bit harder. I should mention that actually, there is a way to configure emacs by clicking through some dialogs; go to Options/Customize Emacs. I find the system a bit clunky, and I don't use it; but you can give it a try. Any customizations you do that way will be written to the end of your .emacs-file.

Let's look at writing a simple .emacs by hand. It could look something like this:

;; a simple .emacs

;; don't show startup messages
(setq inhibit-startup-message t)          
(setq inhibit-startup-echo-area-message t)

(column-number-mode t)  ; show column numbers
This sets two variables (with 'setq') to true (t), and activates 'column-number-mode' (also, see modes), which gives you the column number in the mode-line (the information bar in the bottom of your emacs window). Everything on a line after a ';'-character is considered a comment.

NOTE: you are not required to have a .emacs-file at all; if you don't have one, emacs will start with its defaults. In fact, it's a good idea to start with an empty .emacs, and add things as you go -- if you don't like some default, or want to things in a different way.

If you made some error in your .emacs, emacs might complain. To find the exact problem, it can be useful to start emacs as:

  $ emacs --debug-init
Also, it might be useful to start without evaluating your .emacs:
  $ emacs -q
You could also try an alternative .emacs to test things out:
  $ emacs -q -l dot-emacs-2
(in this last example you would have an alternative .emacs in a file called dot-emacs-2).

Many of the tricks and tips discussed here in emacs-fu are about adding lisp-expressions to your .emacs-file, which will then influence the behaviour of emacs in some way - maybe some external package is loaded, some keybinding is set up, some color is changed, and so on. You can find many .emacs-files used by other people on-line (mine is here). However, as I've written elsewhere, I do not recommend you to copy large parts of people's .emacs without understanding what it does. It easily leads to a strange-behaving emacs, with no idea how to fix it.


NorthboundTrain said...

One tip when adding things to your .emacs:

Edit the .emacs in your current Emacs session, and to test it start up a second Emacs session. If there are errors, you can fix them in your first Emacs session, which is important as sometimes the errors are bad enough to render the new Emacs session unusable.....

doug said...

If you didn't follow Northbound Train's advice on testing your .emacs in a second session but rendered your Emacs session unusable, you can open Emacs with the command emacs -q, which tells Emacs not to load the init file when starting.