30

From what I understand require is used to load large chunks of code (something like modules) although it can also load individual functions.

Autoload on the other side, only registers functions and defers the loading to execution time.

I've recently read an article which advocates the usage of autoload exclusively.

Is it better to autoload than to require? What are some typical use cases for each of these?

  • 3
    @Gilles: FWIW, I disagree with your having removed tags autoload and require from this question. Adding tag libraries was fine, but now anyone searching, using tags, for questions about autoload or require will not find this one. Those are both important Emacs-Lisp functions that people will search for. Too bad. And think how helpful elisp and libraries (esp. elisp) will really be in practice for an Emacs site: they do not distinguish much in Emacsland, I'm afraid. (But no, I am not against having them and using them.) – Drew Sep 28 '14 at 2:11
  • @Drew I'm going by the applicable meta discussion. If you think that this general principle doesn't apply here, please raise it on meta. – Gilles Sep 28 '14 at 2:13
  • 1
    @Gilles: Done. Yes, I read that and figured that that was perhaps what had motivated you. I don't disagree with lots of general principles. That does not mean that following them in a blanket way is always TRT. – Drew Sep 28 '14 at 3:08
  • 2
    I agree with Drew: since this question is specifically about distinguishing the use cases for autoload and require I think those tags are justified. In fact, labeling this question [only] with libraries is over-generalizing. I for one use require to trigger loading of "my-foobar-cfg", which, as the name suggests, only contains just my configuration, not a library. – paprika Oct 6 '14 at 15:16
28

autoload is not a substitute for require. Typically require is used to make sure that a certain file is loaded. autoload on the other hand gives Emacs a hint in which file to find a given function without loading the file right away. Only when the autoloaded function is called is the corresponding file loaded.

Basically with autoload you can delay the loading of a whole file to the moment when you actually need it. This is why (especially big) packages typically define their entry functions as autoloads.

If you want to do heavy customizations for a package usually you are out of luck with autoload. If you still want to avoid require then you can defer your customizations until after the file has been loaded by using eval-after-load.

The Emacs manual covers this topic in the following sections:

  • 2
    Perhaps worth mentioning also: autoload is (more or less) about providing command definitions, so you can use those commands. require is about making everything in a library (and in libraries that library requires, recursively) available. There is more to Emacs than commands. (But yes, of course once an autoloaded command is invoked, its entire library is loaded.) – Drew Sep 27 '14 at 15:46
4

In terms of Emacs initialization, don't make either choice. GitHub user jwiegley has the excellent use-package declaration macro, that will require or autoload a package as determined by need. Best used to defer the loading of packages that can be loaded on-demand.

3

If you want a certain feature to be loaded and available directly when your Emacs has started, use require, otherwise use autoload. I personally value startup time much (I actually restart Emacs a couple of times per week, sometimes several times per day), so I accept that it will take a little bit of time when Emacs autoloads a certain function when I need it. So, think about what you want to be available directly after Emacs is started and what can be loaded when needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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