ErgoEmacsEmacsLispBlogEmacsLispBuy Tutorial
Web Hosting by 1&1

Organize Your “dot emacs” Init File in 5 Minutes

Xah Lee, ,

This page is a guide on organizing your emacs init file.

Many emacs users, have hundreds of lines in their emacs init file, accumulated over the years. Large emacs init file makes emacs start slow, and is a problem when you upgrade emacs.

When you find some elisp code on the web, you pile it in your “.emacs” and you can immediately go back to work on things you need done. That is the beauty of it. The best way i find in keeping “.emacs” organized, is just to break them into multiple files.

Split Your .emacs into Multiple Files

Go to your “.emacs”. If the file has more than 2 hundred lines, then just go to the middle and split the file into 2 files. Name it .emacs.d/emacs_init_1.el and .emacs.d/emacs_init_2.el. The exact file name doesn't matter. Then, in your “.emacs”, change it to like this:

(load "~/.emacs.d/emacs_init_1")
(load "~/.emacs.d/emacs_init_2")

That's it.

When next time you have more code you want to add, just pick a file and add there. Each time, spend no more than 5 minutes doing it.

Rename Files When Needed

Within that 5 min, you can shuffle the file content a bit. Moving keybindings to 〔init_keybinding.el〕, move loading packages to a separate file 〔init_load_package.el〕, move misc settings (such as dired, highlighting, line numbering, cursor, font, etc) to another file 〔misc_settings.el〕.

The init file categories i have are roughly this: {settings, keybinding, load_packages, elisp_functions, ms_windows, misc}. Usually, new things i add to “misc.el”. Once in a while, i clean up that file, move things to proper places.

This way, you have your customization under manageable condition, without much effort. You may edit your init files once a month. Gradually over the years, you may have multiple emacs init files, all manageable and reasonably organized.

Byte Compile Elisp Packages

If you want, you can byte-compile the elisp files. Byte compiled lisp files will load faster, and also run faster. (by a simple test of a loop, it seems to run about 6 times faster.)

To compile, call byte-compile-file. Or, dired into the buffer, call dired-mark-files-regexp% m】 on “.el” files, then call dired-do-byte-compileB】.

For many small customization code, such as setting font, variable, hooks, personal keybindings, byte compile doesn't make any noticeable difference.

Note that when you use (load "myfile"), emacs will first try to load the byte compiled version〔myfile.elc〕 first. If it doesn't exist, then emacs will try to load 〔myfile.el〕.

Auto byte-compile

On problem with compiling init files is that, if you edit a file, you need to remember to compile it again, else emacs will just load the compiled version that doesn't have your changes.

Here's a useful code to add. It'll automatically byte compile a file when it's saved, but only when a byte-compiled file already exists.

(defun byte-compile-current-buffer ()
  "`byte-compile' current buffer if it's emacs-lisp-mode and compiled file exists."
  (interactive)
  (when (and (eq major-mode 'emacs-lisp-mode)
             (file-exists-p (byte-compile-dest-file buffer-file-name)))
    (byte-compile-file buffer-file-name)))

(add-hook 'after-save-hook 'byte-compile-current-buffer)

thanks to Adolfo Benedetti for this.

Emacs's Custom System?

See: Emacs: Customize System Tutorial.

My init files are here if you like to look: Xah Lee's Emacs Customization File.

blog comments powered by Disqus