22

I've installed the solarized theme package via MELPA. I can select one of the two solarized themes via `customize-theme and it activates it. When I save the theme settings it adds the following to my init.el file:

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(custom-enabled-themes (quote (solarized-dark)))
 '(custom-safe-themes
   (quote
    ("8aebf25556399b58091e533e455dd50a6a9cba958cc4ebb0aab175863c25b9a4" default))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

When I launch Emacs it doesn't load the theme but I don't get an error message either. In fact running eval-buffer in the init.el file loads the theme.

  • 1
    Do you also have a .emacs file in your home directory? If so, init-el is ignored. – Malabarba Oct 29 '14 at 15:34
21

add to your init.el

(load-theme 'solarized-dark t)

you can ignore stuff emacs added there, just delete it.

  • 1
    This might work to work around the problem, but it won't solve it. If his custom configs aren't getting loaded, that's something to get to the bottom of. – Malabarba Oct 29 '14 at 15:37
21

I've added the following to my init.el file (I don't have a .emacsfile in ~).

(setq package-enable-at-startup nil) (package-initialize)

and then at the end

(custom-set-variables
 ;; custom-set-variables was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 '(custom-enabled-themes (quote (solarized-dark)))
 '(custom-safe-themes
   (quote
    ("8aebf25556399b58091e533e455dd50a6a9cba958cc4ebb0aab175863c25b9a4" default))))
(custom-set-faces
 ;; custom-set-faces was added by Custom.
 ;; If you edit it by hand, you could mess it up, so be careful.
 ;; Your init file should contain only one such instance.
 ;; If there is more than one, they won't work right.
 )

That seems to work. I'm not very familiar with emacs so I have no idea whether this is a bad workaround...

  • That is actually the correct solution. – lunaryorn Oct 30 '14 at 13:38
  • @lunaryorn What makes this the correct solution? (Worked for me.) – bright-star Nov 24 '14 at 10:52
  • @TrevorAlexander You need to load packages before you can use them. – lunaryorn Nov 24 '14 at 11:08
15

By default, Emacs only autoload the packages after processing the init file. So when it was trying to set theme, the theme package wasn't loaded.

Rather than making all packages load in your init file (like what you seem to be doing), you can also load the them after package autoloading by adding a hook to after-init-hook, since packages are autoloaded just after the init file and before after-init-hook. Note that it will not work if you try to load-theme directly in the init file because the package hasn't been loaded at that point.

So alternatively, add this to your init file:

(add-hook 'after-init-hook (lambda () (load-theme 'solarized-light)))
  • I think this is the correct solution and works in variety of scenarios (like for me I am using github.com/bbatsov/prelude and it works like a charm there too) – Amol Gawai Nov 9 '16 at 16:38
  • This is the correct solution, and should be the accepted answer – Dodgie Jan 10 '17 at 6:11
7

I think the important part is (package-initialize). I'm not so sure you need the package-enable-at-startup bit. So:

(package-initialize)
(load-theme 'ample t)

You can also tell emacs to trust all your themes by default so you don't get prompted each time:

(setq custom-safe-themes t)

The thing to keep in mind is that the order to everything is important. Depending on where in the file you're trying to load a theme, some of the above suggestions may or may not be necessary.

I initialize packages and the MELPA repo at the very beginning of my .emacs file which allows me to more easily reference MELPA loaded packages later in the file. I also set custom-safe-themes so I don't have to worry about the fact that Custom is adding the trust info at the end of the file. Here's what I have:

(when (>= emacs-major-version 24)
  (require 'package)
  (package-initialize)
  (add-to-list 'package-archives
           '("melpa" . "http://melpa.milkbox.net/packages/") t)
  )
(setq custom-safe-themes t)

..later in the file..

;; Load a nice theme if in GUI
(when (display-graphic-p)
  (load-theme 'ample t)
  )
-1

The first line should be to know where to look:

(add-to-list 'custom-theme-load-path "~/.emacs.d/themes/")

then the others can go :)

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.