1

I want to use use-package for nearly everything in my init.el. I have set up recentf. And to make the recent-file buffer pop up when Emacs starts I also included the init-open-recentf package.

But I cannot make it work with use-package. The recentf-list does not pop up on start.

This question is not only about how to make it work but more about how to make it work the use-package way.

This is the problematic init.el

    (use-package recentf
      :bind
      ("C-x C-r" . recentf-open-files)
      :config
      (setq recentf-max-menu-items 15
            recentf-max-saved-items 100
       )
      (recentf-mode 1))
    
    (use-package init-open-recentf
      :after recentf
      :config (init-open-recentf))

Of course I can call init-oopen-recentf outside the use-package thing. But I assume there is a way to do this with use-package.

    (use-package init-open-recentf
      :after recentf)
    ;; works
    (init-open-recentf)

1 Answer 1

2

Your snippet isn't working because recentf won't load until you press the C-x C-r key combination, which in turn implies init-open-recentf will not be loaded. You can read more about lazy loading with use-package.

(use-package recentf
      :bind ("C-x C-r" . recentf-open-files)
      :config
      (setq recentf-max-menu-items 15
            recentf-max-saved-items 100
       )
      :hook (after-init . recentf-mode))
    
(use-package init-open-recentf
      :after recentf
      :config (init-open-recentf))

This works for me. The hook ensures that recentf-mode is loaded and configured after Emacs starts. It is possible to come up with alternate forms using :init sections.

You will need to add :demand t to the second use-package macro if you have set use-package-always-defer to a non-nil value.

4
  • My snippet does work. Maybe my direct (init-open-recentf) does invoke loading of init-open-recentf package which itself loads recentf because of after: recentf directive. But thanks. Your hook is a more use-package like way what I also asked for.
    – buhtz
    Commented Nov 1, 2021 at 8:41
  • Why not using :defer nil instead of that hook? IMHO this means "load imidiatly" or doesn't it?
    – buhtz
    Commented Nov 1, 2021 at 8:51
  • 1
    Yes, not deferring and using (recentf-mode 1) in the :config section should also work. I like hooks because they allow you to explicitly control when you want to do things. Commented Nov 1, 2021 at 9:06
  • 1
    Note that init-open-recentf loads recentf because of the (require 'recentf) line in the init-open-recentf.el code, and not because of the :after recentf. Commented Nov 1, 2021 at 9:09

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.