Hooks are an important mechanism for customizing Emacs. A hook is a Lisp variable which holds a list of functions, to be called on some well-defined occasion.
Questions tagged [hooks]
387 questions
35
votes
3 answers
Proper way to enable minor mode
I have foo-mode and would enable bar-minor-mode for it. So which way is more common and preferable?
A
(add-hook 'foo-mode-hook 'bar-minor-mode)
B
(add-hook 'foo-mode-hook (lambda ()
"Turn on `bar-minor-mode' mode."
…

Netsu
- 555
- 1
- 5
- 9
34
votes
1 answer
What is the difference between ' and #' in front of a symbol?
I'm a little new to Emacs.
When looking at some of the configurations, I found there are two types command in "add-hook".
(add-hook 'LaTeX-mode-hook #'LaTeX-math-mode)
and
(add-hook 'LaTeX-mode-hook 'LaTeX-math-mode)
This has confused me for a…

X.Arthur
- 443
- 4
- 9
20
votes
2 answers
Run command when opening a file of a specific filetype
I'm trying to get a lisp script to run some commands for me when I open a file of a specific filetype. I know that I'm working in the correct init file because if I remove the theme from it, emacs has no theme when I start it up.
This is the script…

SiXoS
- 303
- 1
- 2
- 5
19
votes
2 answers
Is there fully-automatic fill-paragraph-mode for code comments?
I'm looking for a minor mode to keep paragraphs filled at all times while typing (similar to what aggressive-indent-mode does for indentation). It also needs to be smart enough to only fill comments (and maybe strings depending on the…

dshepherd
- 1,281
- 6
- 18
19
votes
1 answer
Remove hooks for specific modes
I want to delete trailing whitespace on save for every mode except org-mode.
In my .emacs, I have the following line:
(add-hook 'before-save-hook 'delete-trailing-whitespace)
I use the use-package macro, and I tried adding (remove-hook…

Matthew Piziak
- 5,958
- 3
- 29
- 77
18
votes
3 answers
before-save-hook for cc-mode
I want to customize cc-mode to execute a function before saving. One solution would be to modify cc-mode keymap to bind C-x C-s to a function which performs the work I need and then saves it.
My question is whether there is a hook I could customize…

Pradhan
- 2,330
- 14
- 28
16
votes
4 answers
Run a Function Only Once, Though a Hook Many Times
The context
I'm using the after-make-frame-functions hook to load properly the themes in an emacs client/server configuration. Specifically this is the code snippet that I use to make that (based in this SO answer):
(if (daemonp)
(add-hook…

joe di castro
- 370
- 3
- 12
16
votes
2 answers
How to find out what a key sequence really does
From time to time I observe unexpected behavior when editing text. My first recourse is usually to use C-h k to find out what functions are being called by a given key sequence. However, sometimes the documentation is at odds with the observed…

nispio
- 8,175
- 2
- 35
- 73
16
votes
1 answer
What's the difference between after-init-hook and emacs-startup-hook
Seems there are several hooks about actions on Emacs initialization. However I'm not sure what their differences are, and a brief Googling yields little information. Is it like emacs-startup-hook is executed instantly after Emacs is fired up, but it…

xji
- 2,545
- 17
- 36
15
votes
1 answer
Execute external script upon save when in a certain mode?
When I'm in org-mode, I want Emacs to execute a bash script I wrote whenever I hit C-x s to save. The script automatically syncs the file I am saving to my Raspberry Pi. It expects the file name as argument.
How do I tell Emacs to run the external…

qacwnfq q
- 253
- 3
- 6
14
votes
1 answer
How to avail of `:hook` using use-package?
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…

Daniele
- 637
- 1
- 5
- 14
13
votes
2 answers
Where should I include a lazy initialization?
I want to add a specific key binding to latex-mode:
(define-key latex-mode-map (kbd "") 'my-latex-defun)
At the same time I want to set it only when the specific mode is loaded. So I would like to know if there is a preference (best practice)…

jrbalderrama
- 133
- 7
11
votes
2 answers
Disable hl-line-mode only for eshell and ansi-term
I have global-hl-line-mode setup in my init file:
(use-package hl-line
:init (global-hl-line-mode 1))
Is it possible to disable it when I am in eshell and ansi-term?
I tried adding a hook like this:
(add-hook 'eshell-mode-hook (hl-line-mode…

Manuel Uberti
- 3,150
- 18
- 36
11
votes
5 answers
How to enter view-only mode when browsing Emacs source code from help?
When I browse Emacs help for functions via C-h f, I often want to peek into the Elisp/C implementation. I want to enter view-mode automatically when I access source code this way to avoid unnecessary modification. Is there a hook or function I can…

rationalrevolt
- 213
- 1
- 4
10
votes
1 answer
What is a hook?
Just to clarify... For example, in this doc:
A hook is a Lisp variable which holds a list of functions, to be
called on some well-defined occasion. (This is called running the
hook.) The individual functions in the list are called the hook
…

147pm
- 2,907
- 1
- 18
- 39