use-package’s :config vs. :init

I read this Reddit thread about favorite themes, and got intrigued by the spacemacs theme1.

I added that theme to my init file, and tried making it the default theme. I use use-package, and configured the theme as follows:

(use-package spacemacs-theme
:ensure t
:config
(load-theme 'spacemacs-light t)
)

When re-evaluating my init file, the theme didn’t load. I tried to run only the (load-theme 'spacemacs-light t) line, and the theme loaded. I changed the :config to :init in the package configuration, and it loaded when I re-loaded emacs.

What, then, is the difference between :init and :config in use-package?

The answer to that question, which I found it in this stack-overflow answer, is that in use-package, whatever defined inside the :init keyword, will load whenever emacs is loading. What’s in the :config, though, will be executed only when the package is actually loaded (i.e lazy loading)2.

Here’s how my configuration for that theme looks like now:

(use-package spacemacs-theme
:ensure t
:init
(load-theme 'spacemacs-light t)
)

Footnotes:

1

I tried spacemacs before, and liked its look and feel, but didn’t know I can take it back with me to gnu emacs

2

Needless to say that, going back to the use-package documentation, the difference between :init and :config is clearly described…

Get The Current File’s Path in Emacs

Here’s a small function I borrowed from this question on stack-overflow. It returns the full path of the file I currently edit in the buffer:

(defun show-file-name ()
  "Show the full path file name in the minibuffer."
  (interactive)
  (message (buffer-file-name))
  (kill-new (file-truename buffer-file-name))
)
(global-set-key "\C-cz" 'show-file-name)

You’ll note that this function is bind to C-c z. So when typing it, you should see the path showing in the minibuffer. As a bonus, it stores the path in the kill ring, so C-y (CMD-v works as well on my mac) will paste the value.