emacselispdot-emacs

Add several functions as hooks in Emacs


How could I simplify something like the following code in my init.el file?

(add-hook 'org-mode-hook 'turn-on-auto-revert-mode)
(add-hook 'org-mode-hook 'turn-on-org-cdlatex)
(add-hook 'org-mode-hook 'smartparens-mode)
(add-hook 'org-mode-hook 'abbrev-mode)

I have several other rows like this, including some lambda functions added to org-mode-hook...


Solution

  • Personally, I would strongly advice against adding lambda functions to hooks. The main reason is that if you change the content and reevaluate the add-hook expression, the hook contains both the old and new lambda expression. The second reason is that it looks bad when you inspect a hook -- it's better to see a function name compared to a large lambda expression.

    Instead, I would suggest using:

    (defun my-org-mode-hook ()
      (turn-on-auto-revert-mode)
      (turn-on-org-cdlatex)
      (smartparens-mode 1)
      (abbrev-mode 1)))
    (add-hook 'org-mode-hook 'my-org-mode-hook)
    

    A side note: You can use global-auto-revert-mode to enable auto-revert on all buffers, that way you don't have to enable it for all major modes.