Use your Emacs startup file
Every time Emacs starts, it looks for a hidden file in your home directory, .emacs, and executes any Emacs Lisp the file contains. This is your Emacs startup file or init file; use it to set variables or make other changes that happen automatically whenever Emacs starts.
Emacs Lisp calls are enclosed in parentheses; comments begin with a semicolon character (;). A number of conventions have evolved with Emacs Lisp; for example, comments describing a function usually begin with two semicolons and a space character. These conventions are described in the GNU Emacs Lisp Reference Manual (see Resources).
Among the many customizations you can make in your .emacs file, you can set variables, define key bindings, and set various modes, as described next.
(As described in the fourth tutorial in this series (see Resources), you can start Emacs
without loading this init file first by giving the
option, and you can have Emacs load another user's startup file by giving the
username as an argument to the
Maybe the most common way a .emacs init file is used is to
set certain variables so that these values are set
automatically every time Emacs is started. To set a variable in a .emacs file,
setq function, as follows:
(setq VARIABLE VALUE)
You saw an example in Listing 7, where you created a simple .emacs file and saved the buffer in your home directory. Now you can demonstrate it:
C-x C-cto exit Emacs; it's okay to lose the changes in any of the unsaved buffers you worked on in this tutorial.
Get today's sunrise and sunset: Type
You didn't have to set the Emacs variables for latitude, longitude, or location name because Emacs read them in from your .emacs file when it started. But at any point in an Emacs session, you can redefine any variable that's set in your .emacs file.
When you define a new key binding using the methods described in the Make a custom key section, it only lasts for the current Emacs session—once you exit, the changes are lost. But you can put custom bindings in your .emacs file to make them work every time you start Emacs.
If you have a problem with the Del key, for instance, you can include this in your .emacs file:
;; Make Del delete the character at point. (global-set-key [delete] 'delete-char)
And you can include any one of the pairs described in Listing 8 to automatically redefine your Home and End keys for every Emacs session.
Listing 8. Redefining the Home and End keys in a .emacs file
;; Make Home and End move to the beginning and ending of the buffer (global-set-key [home] 'beginning-of-buffer) (global-set-key [end] 'end-of-buffer) ;; Make Home and End move to the beginning and ending of the line (global-set-key [home] 'beginning-of-line) (global-set-key [end] 'end-of-line) ;; Make Home and End move to the beginning and ending of the sentence (global-set-key [home] 'backward-sentence) (global-set-key [end] 'forward-sentence)
You can also turn modes on or off in your .emacs file.
Because most modes are called as a toggle, following them with a positive argument turns them on and a negative argument turns them off. You do so using this format:
For instance, you can turn off the menu bar automatically with the following:
;; turn off the Emacs menu bar (menu-bar-mode -1)
Usually, modes are turned on when given a value of a positive integer or
t (for "true"); they're turned off when given a
negative, zero, or
All of the following lines do the same thing: They set
global-font-lock-mode, which turns on Font Lock mode
automatically for all buffers:
(global-font-lock-mode) (global-font-lock-mode 1) (global-font-lock-mode t)
Sometimes, when Emacs needs an answer from you, you have to type out the word
no, and follow it
with a tap of the Enter key.
When you know what you're doing, this can be as annoying as how some window applications make you click OK in a popup window to verify the command you just specified.
To simplify the process so that pressing the Yor N key suffices, add the following to your .emacs file:
;; eliminate long "yes" or "no" prompts (fset 'yes-or-no-p 'y-or-n-p)
This technique uses the
fset function to redefine a
given function. To learn more about this and similar Emacs Lisp functions,
consult the GNU Emacs Lisp Manual (see
Listing 9 contains a sample .emacs init file and many of the customizations described in this section. It also contains additional customizations using functions and variables described in Table 1 and Table 2. You can edit your new .emacs file to contain these entries, and then restart Emacs to see how the changes work.
Listing 9. A sample .emacs init file
;; set the current location (setq calendar-latitude 26.37) (setq calendar-longitude -80.09) (setq calendar-location-name "Boca Raton, FL (USA)") ;; eliminate long "yes" or "no" prompts (fset 'yes-or-no-p 'y-or-n-p) ;; Custom mode configurations ;; ;; turn off the menu bar (menu-bar-mode -1) ;; ;; turn off the tool bar (tool-bar-mode -1) ;; ;; turn on Font Lock mode globally (global-font-lock-mode t) ;; Custom key bindings ;; ;; Make Del delete the character at point. (global-set-key [delete] 'delete-char) ;; ;; Make Home and End move to the beginning and ending of the sentence (global-set-key [home] 'backward-sentence) (global-set-key [end] 'forward-sentence)