As every tinkerer will know, my Emacs configuration is steadily growing.

To get a some sense of having a structure, I divided it in seperated files, each for every category. But I noticed lately that some people organize their configuration in an different way than I do.

Suppose they have the confiugration foo-configuration.el, they put (provide 'foo-configuration) at the bottom of the file. In their dot-emacs/init.el they call the configuration with (require 'foo-configuration).

But I do it in the other way. I create bar-configuration.el.

In my dot-emacs/init.el, I call the configuration with (load "C:/Dropbox/Emacs/bar-configuration.el") which suits my needs.

But I'm wondering if I'm missing something important (I'm relatively new to Elisp). Is there any advantage to use provide/require above the latter?

  • 2
    I think your question, as it stands can be viewed as opinion based. If you reword the title and question to so that your are looking for the best structure based on some sort of testable metric it won't get closed. – Jordon Biondo Jun 9 '15 at 20:40
  • As in, which structure results in the fasted startup/which one leads to the least memory overhead on startup. – Jordon Biondo Jun 9 '15 at 20:40
  • 2
    Thanks for asking this question and also for the response(s). If rewording is necessary to keep this question, then I would suggest narrowing as follows: What is the preferred structure of Emacs configuration so it is fast and easy yet plays nice with modern ELPA package management tools? – Emacs User Jun 9 '15 at 21:10
  • It's a reasonable question, IMO. My own answer is that there is no single "preferred structure". Use whatever works for you. – Drew Jun 9 '15 at 22:54

require/provide > load but neither solution is ideal.

You should typically never need to require or load anything in your init. Rather you should add the path to your configuration files to the load-path variable and autoload any functions you might bind or add to hook variables.

So if you have my-config.el in /foo/bar/baz/my-config.el.

which looks like this:

;; my-config.el

(defun my-super-command ()
  (interactive) ...)

(defun my-c-mode-hook ()
  (progn ...))

(provide 'my-config)

Your init should look like this:

;; init.el

(add-to-list 'load-path "/foo/bar/baz/")

(add-hook 'c-mode-hook 'my-c-mode-hook)

(global-set-key (kbd "C-c C-j") 'my-super-command)

Your custom top level commands should be autoloaded so there is no need to require or load your config file. It will be loaded automatically when the command is executed.

Also non-interactive functions you may use for hooks or other purposes can be autoloaded as well, so in this case when you your config file will be loaded automatically when c-mode starts up because the hook function is autoloaded.

The goal is to load the absolute least amount of code on startup as possible. Anything that is major mode or package specific should be in a autoloaded major-mode/package hook or wrapped in eval-after-load in your init.

That being said, if you want to decide simply between load and require/provide, I think most would agree require/provide is the more correct solution.

| improve this answer | |
  • Thanks for your well written explaination. But I don't agree on two points: 1. I regular use Emacs client, with the Emacs as server instance. So startup-time is not an important topic here. An when you open a C-file, the C-mode will be triggered, and so your C-configuration will be loaded, which slows down Emacs I think. 2. Your configuration made it diffucult to me to debug. Sometimes there is an error of wrong text, then I comment the loaded files every one for one, until the error don't occur. It enables me to track down the bug. – ReneFroger Jun 9 '15 at 20:54
  • If don't care about a fast startup, just requireing everything in your init is probably the best option. It is important to note that an autoload won't happen twice. So if you a c-mode-hook autoloads your c configuration, it will only happen on the first time you open a c file, it won't reload everytime. Also if you have init errors, the --debug-init flag and debug-on-error command are your friends. – Jordon Biondo Jun 9 '15 at 20:58
  • Thanks for your reply. I think I prefer to load them all in the startup, because I don't like the idea that I need to wait if I open a file in a new mode for the first time. And I didn't knew about the debug-on-error feature, thanks for that. But one thing is still unclear to me. Even when I don't care about a fast startup, why is requireing everything in my init the best option? – ReneFroger Jun 9 '15 at 21:03
  • 1
    It's a matter of taste mostly but I suggest reading these: stackoverflow.com/questions/20952894/… and ergoemacs.org/emacs/elisp_library_system.html – Jordon Biondo Jun 9 '15 at 21:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.