6

I've wrote this, and it works:

(use-package web-mode
  :ensure t
  :mode (("\\.php$" .  web-mode)
         ("\\.html$" .  web-mode))
  :preface
  (defun dg/web-mode-hook())
  :config
  (add-hook 'web-mode-hook 'dg/web-mode-hook))

It enables web-mode when opening .php or .html files. I've noticed that use-package also provides :hook directive, but it's not very clear how I can use it in this case.

Any clue?

Thanks in advance.

1
9

As others said I think https://github.com/jwiegley/use-package#hooks is pretty clear. :hook replaces add-hook and creates autoloads for you. Your example with :hook would be:

(use-package web-mode
  :ensure t
  :mode (("\\.php$" .  web-mode)
         ("\\.html$" .  web-mode))
  :hook (web-mode . dg/web-mode-hook)
  :config
  (defun dg/web-mode-hook()))
4
  • Thank you, I tought that :hook could some way save me from having to define a my-hook-fn(). – Daniele Mar 6 '18 at 8:43
  • @Daniele, you should be able to use a lambda form: (use-package web-mode :ensure t :mode (("\\.php$" . web-mode) ("\\.html$" . web-mode)) :hook (web-mode . (lambda () ...))) – paradoja Feb 15 '19 at 13:48
  • It is not clear to me. For instance, couldn't you simply write :hook dg/web-mode, and if not, why? – kotchwane May 30 '20 at 6:19
  • @kotchwane if you click the readme link in the answer you see that if you pass only one argument (or a list of symbold), use-packages assumes those are the modes you want to activate your current "use-package package" if that makes sense. So in your case it would add web-mode to dg/web-mode-hook (the -hook is added by use-package) – dakra Jun 1 '20 at 11:37

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.