1

I would like to turn on rainbow-mode in elisp and/or lisp modes when I am editing init.el using use-package. I've tried the following:

(use-package rainbow-mode
  :demand t
  :hook 
  ((text-mode  . rainbow-mode)
   (elisp-mode . rainbow-mode)
   (lisp-mode  . rainbow-mode)))

I've also tried attacking it from the other side and attaching it to the lisp/elisp mode definition

(use-package lisp-mode
  :ensure nil
  :hook 
  (lisp-mode  . rainbow-mode))

And I also tried using a setup method:

(use-package elisp-mode
  :ensure nil
  :hook 
   (elisp-mode  . lisp-mode-setup))

But it doesn't seem to work. I can turn it on manually in the buffer by calling M-x rainbow-mode but I can't get use-package to play ball. I'm basically trying to find a way to add code to a mode hook.

What am I missing? Should I do this a different way? Am I hooking into the correct mode?

1 Answer 1

1
(use-package rainbow-mode
  :hook (emacs-lisp-mode text-mode lisp-mode))


This is from the use-package info manual

File: use-package.info, Node: hook

:hook

The :hook keyword allows adding functions onto hooks, here only the basename of the hook is required. Thus, all of the following are equivalent:

     (use-package ace-jump-mode
       :hook prog-mode)
     (use-package ace-jump-mode
       :hook (prog-mode . ace-jump-mode))
     (use-package ace-jump-mode
       :commands ace-jump-mode
       :init
       (add-hook 'prog-mode-hook #'ace-jump-mode))

And likewise, when multiple hooks should be applied, the following are also equivalent:

     (use-package ace-jump-mode
       :hook (prog-mode text-mode))
     (use-package ace-jump-mode
       :hook ((prog-mode text-mode) . ace-jump-mode))
     (use-package ace-jump-mode
       :hook ((prog-mode . ace-jump-mode)
         (text-mode . ace-jump-mode)))
     (use-package ace-jump-mode
       :commands ace-jump-mode
       :init
       (add-hook 'prog-mode-hook #'ace-jump-mode)
       (add-hook 'text-mode-hook #'ace-jump-mode))

The use of :hook, as with :bind, :mode, :interpreter, etc., causes the functions being hooked to implicitly be read as :commands (meaning they will establish interactive autoload definitions for that module, if not already defined as functions), and so :defer t is also implied by :hook.

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.