Replace require statements by autoload in .emacs file to improve performance

emacsperformance

According to these posts:

one can somehow replace require and load statements in the .emacs file to speed up the emacs start. However I don't know how to do this in detail.

For example I have (among other things) in my .emacs file the following require and load statements:

(load "auctex.el" nil t t)
(require 'alarm)
(require 'linked)
(load "nxhtml/autostart.el")
(require 'autoinsert)
(require 'recentf)
(require 'color-theme)
(load "~/.emacsaddons/emacs-color-theme-solarized/color-theme-solarized")
...

For alarm and linked there are corresponding files in a directory called .emacsaddons, for nxhtml there is a directory, for the others there are no corresponding files in .emacsaddons. I didn't include all require or load statements from my .emacs file in the example above, just a few where I feel that the steps for replacing them with autoload will differ between them (for example because some have el files unter .emacsaddons and some doesn't, or because nxhtml is a subdir of .emacsaddons…).

How are the detailed steps to replace everything by autoload functionality for improving the performance?

Best Answer

As a first step towards autoloading, I would suggest that you convert your explicit load commands with paths appended to the list of things that emacs should load, as in:

(add-to-list 'load-path (expand-file-name "~/.emacs.d/"))

With that at the top of your .emacs, you can call other things that depend on loading other files so that they will be found.

Specifically for postponing loading files, for each (require 'foo) that you have in your .emacs, you should replace that with something similar to:

(autoload 'name-of-foo-mode "code-for-foo.el" "Minor/Major mode for foo" t)

You may have to experiment (perhaps read) the code for the respective .el file to see what is the name that you need to put in place of 'name-of-foo-mode. The most common are 'foo or 'foo-mode, but there is inconsistency.

In my case, I have these declarations close to the bottom of my .emacs:

(autoload 'textmate-mode "textmate" "Minor mode for automatic bracket pairs" t)
(autoload 'post-mode "post" "Mode for editing e-mails" t)
(autoload 'turn-on-reftex "reftex" "Minor mode for references in TeX files" t)
(autoload 'mode-compile "mode-compile" "Compile current buffer" t)
(autoload 'markdown-mode "markdown-mode" "Major mode for Markdown files" t)

but some of the declarations that I had to require before I moved to autoloading were:

(require 'reftex)
(require 'post)
(require 'compile)
(require 'textmate)

So, as I said, you may have to do some experimentation or code reading, but, in the end, it is worth it, as it will save you some time if you invoke emacs frequently.

Related Question