46

I want to enable rainbow-mode everytime I start emacs, rather than having to use M-x rainbow-mode.

I guess there is some command I put in my .emacs file.

I tried all of the following, but none of them worked:

(require 'rainbow-mode)   

(rainbow-mode initialize)

(global-rainbow-mode)

More generally, how do I load any mode/package automatically on startup?

phils
  • 71,335
  • 11
  • 153
  • 198
Dark Knight
  • 461
  • 1
  • 4
  • 3

1 Answers1

51

rainbow-mode isn't a global minor mode, so it needs to be enabled on a per-buffer basis.

I only use it for CSS, so I have:

(add-hook 'css-mode-hook 'my-css-mode-hook)
(defun my-css-mode-hook ()
  (rainbow-mode 1))

If you genuinely want it to be global, everywhere, you can easily define a global minor mode yourself:

(define-globalized-minor-mode my-global-rainbow-mode rainbow-mode
  (lambda () (rainbow-mode 1)))

(my-global-rainbow-mode 1)

You can add any arbitrary logic to that (lambda () (rainbow-mode 1)) function (which will be evaluated in every buffer) in order to decide whether or not to actually call (rainbow-mode 1) for a given buffer, so if you're comfortable with elisp then you can easily extend this approach to cover your specific requirements for the mode in question.


More generally, how do I load any mode/package automatically on startup?

It can vary, but the approaches I've shown would suffice for most minor modes: Either you want them enabled whenever MODE is enabled (being some specific other mode name), in which case you can use the MODE-hook variable (which will always be available) as per the css-mode-hook example; or else you want the mode enabled permanently, in which case a global minor mode is a good approach (because you can toggle it on and off globally). Some minor modes are global by default (or provide global variants), but you can create your own if necessary, as per the my-global-rainbow-mode example.

Also be aware that modes can be derived from other modes, in which case all relevant MODE-hook hooks will be run (for details see https://stackoverflow.com/a/19295380/324105). A common use-case is to use prog-mode-hook to enable functionality wanted for all the programming modes which are derived from it (which is most programming modes).

Remember that many (hopefully most) libraries and packages will provide usage instructions. If you can't find documentation, be sure to try M-x find-library to visit the library file, and then read through the comments at the top. There is often a very informative "Commentary" section, and sometimes this is the primary source of end-user documentation, and explain how to enable its functionality.

Community
  • 1
  • 1
phils
  • 71,335
  • 11
  • 153
  • 198
  • 11
    You can just do `(add-hook 'css-mode-hook 'rainbow-mode)` without going through `my-css-mode-hook`. – Stefan Apr 17 '13 at 01:14
  • 3
    Stefan: only in Emacs 24, otherwise that toggles the mode, and I'm not inclined to assume that everyone has upgraded. (And in my case that function also does a bunch of other things, so I do need it :) – phils Apr 17 '13 at 03:11
  • 2
    Thank-you! I used this to enable goto-address-mode globally. At the end, I put in .emacs: `(define-globalized-minor-mode global-goto-address-mode goto-address-mode (lambda () (goto-address-mode 1)))` and then by accident found the value on customize while checking the configuration for the mode, so in the end I let the global mode activation be handled by customize. – pupitetris Oct 19 '17 at 20:22
  • I used your solution in spacemacs (the global one) and it removed my color theme. I cannot enable it now :( How to undo this definition? How can I undefine the globalized minor mode? – ashrasmun May 05 '19 at 07:52
  • 1
    @ashrasmun, that issue makes no sense to me (unless perhaps you don't have `rainbow-mode` installed at all), but you can toggle off the global mode using `M-x my-global-rainbow-mode` or evaluate `(my-global-rainbow-mode 0)`, after which it's not going to be doing anything. – phils May 05 '19 at 11:02
  • @phils It makes no sense to me either, just as many things in emacs, but that's just one of the problems I have encountered as beginner spacemacs user. Thanks for additional info on the matter - I hope others can make use of it too! – ashrasmun May 05 '19 at 13:26