DotEmacsStructuring

Here are some techniques to help you organize your InitFile.

System-specific configuration

See CustomizingBoth for tips on organizing platform-specific configuration (ex: Linux / Windows / Mac OS X).

Splitting your configuration up into separate files

Steve Yegge's init file is a good example of an intricate init file that still has some order to it. Different parts of initialization are separated out into different files, and everything is loaded in the init file.

For example, you could have style.el, keys.el, and modes.el all in ~/.emacs.d/elisp making the appropriate customizations. Then you could tie everything together like this in your init file:

    (add-to-list 'load-path "~/.emacs.d/elisp/")
    (load-library "style")
    (load-library "keys")
    (load-library "modes")

See DotEmacsModular for more about splitting up your configuration into multiple files.

Using variables to turn sections on and off

This technique is based around keeping a list of variables which corresponds to particular features, additional modes or customisations, with a corresponding when clause to wrap the code related to that specific feature.

For example, something like the following at the beginning of the file.

(defvar *slime-enabled* t)      ;; superior lisp interaction mode
(defvar *globrep-enabled* nil)  ;; global find and replace
(defvar *dict-enabled* nil)     ;; mizspellnigs & dictionaries
(defvar *vc-darcs-enabled* nil) ;; darcs using vc interface  
(defvar *paredit-enabled* nil)  ;; paredit-mode

then somewhere further down…

(when *paredit-enabled*
  (autoload 'enable-paredit-mode "paredit" 
            "Turns on pseudo-structural editing of Lisp code." t)
  (add-hook 'lisp-mode-hook 'enable-paredit-mode)
  (add-hook 'scheme-mode-hook 'enable-paredit-mode))

so setting *paredit-enabled* to either t or nil would enable or disable the feature completely. This can be especially useful when testing several modes for interoperability, or moving to a new setup.

Organizing package-related configuration with use-package

You can install UsePackage and use it to group together commands and keybindings related to a package. If you would like to optimize your Emacs startup, you can use UsePackage to time how long packages take to load and define your own autoloads so that Emacs doesn’t load packages until you call those commands.

Here’s a sample configuration snippet:

    (use-package color-moccur
      :commands (isearch-moccur isearch-all)
      :bind ("M-s O" . moccur)
      :init
      (bind-key "M-o" 'isearch-moccur isearch-mode-map)
      (bind-key "M-O" 'isearch-moccur-all isearch-mode-map)
      :config
      (use-package moccur-edit))
  

Org Mode and literate configuration

You can use OrgMode to organize your InitFile and optionally publish it to HTML or other formats. This allows you to use headings, add links or screenshots, share a nicely-formatted version of your config, or even include TODOs and =org-drill= reminders.

See OrgDotemacs for tips on how to keep your InitFile organized in an OrgMode file, choose which bits to load, and handle errors and dependencies gracefully.

Here are some examples:

If you would like to write your config in one Org file but have it generate multiple Emacs Lisp files, see the tutorial at https://github.com/pierre-lecocq/literal-emacs .