If I have something like:

(use-package X
  :defer t
  (add-hook 'some-mode 'package-x-function))

Will that work as expected given that I set :defer t and the package doesn't have any "built-in" autoloads and I'm not using any of the autoload-creating use-package functions? In other words, does registering a hook alone take care of loading package X given that I set :defer t?

If not, what is the minimal additional configuration I require to make the hook work? Would I have to do something like :commands package-x-function?

1 Answer 1


I think this should do what you want:

(use-package X
  :commands package-x-function
  (add-hook 'some-mode 'package-x-function))

The :commands directive will generate an autoload for package-x-function, and the :init directive will immediately add it to the hook. Because :commands implies :defer, the package won't be loaded immediately. When the hook is run, the autoload will be looked up and the package will be loaded.

If you left off the :commands part, then you'd get a void-function error when the hook is run, because Emacs won't have a definition for package-x-function and there's no autoload to tell it how to get one.

  • Normally if the package wasn't deferred, you'd put the add-hook in the :config section, so that it wouldn't occur until the package was loaded. In this case thanks to the autoload created by :commands, it makes sense to put it in the :init right, otherwise there would never be a "trigger" run to autoload the package. Commented Jan 20, 2016 at 19:54
  • Forgot to phrase my comment as a question. Would appreciate a confirmation on that. Commented Jan 20, 2016 at 20:02
  • Correct. You need something to load the package, and so using the hook to do so means that it needs to be in :init rather than :config. Commented Jan 20, 2016 at 20:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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