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]
409 questions
20
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
- 6,038
- 3
- 31
- 79
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,635
- 18
- 38
9
votes
1 answer
List hooks that will run after command
Is there a way to lookup which hooks will run when a given command is called?
For example, for major modes it is pretty easy to guess that activating python-mode will run python-mode-hook. But sometimes it is not obvious (to me at least) which…

elethan
- 4,825
- 3
- 30
- 56
8
votes
3 answers
how to unbind a mode hook without restarting
I had set up a custom hook for a mode, but I now realize that I want to use the mode without my changes and without my hook. So, I want the mode to behave as if I never set the hook in the first place, and I want to do this without restarting emacs.…

modulitos
- 2,482
- 1
- 22
- 37
8
votes
3 answers
What's a good way to have a hook that runs for every file in a given directory?
What is a good way to assign a function to run when I open any file in a specified directory? Just like a hook but based on the location of a file rather than a major mode.
It would be nice if I could enable this by having a special file in the…

Tikhon Jelvis
- 6,252
- 2
- 28
- 41
7
votes
2 answers
Programmatically controlling the order of functions within a hook
I am looking for an example, please, to programmatically control the order of functions within a particular hook:
First in time, which is easy -- i.e., don't append using add-hook.
Last in time, which is easy -- i.e., append using add-hook.
Second…

lawlist
- 19,106
- 5
- 38
- 120
6
votes
1 answer
(la)tex-mode-hook doesn't work?
I was playing around with my .emacs, and I cannot figure out why the following does not work with .tex files:
(add-hook 'tex-mode-hook
(lambda ()
(show-paren-mode t)))
On the other hand, the following does work on elisp files.
(add-hook…

bernie
- 609
- 4
- 11
5
votes
1 answer
Why (lambda () (run-hooks 'foo)) instead of just 'foo?
I am studying the following code snippet taken from prelude-c.el:
;; taken from prelude-c.el
(defun prelude-makefile-mode-defaults ()
(whitespace-toggle-options '(tabs))
(setq indent-tabs-mode t))
(setq prelude-makefile-mode-hook…

nalzok
- 675
- 6
- 18
5
votes
1 answer
Is there a way to detect change of monitor in Emacs / Elisp?
tl;dr I'm looking for way to detect when an Emacs frame has been moved from one monitor to another in a multi-monitor setup.
I have code in my ~/.emacs which sets the default font size for all displayed buffers in a frame based on the physical size…

nrvale0
- 63
- 3
5
votes
4 answers
Is there a standard function that applies a list of predicates to a value until success?
I look for compacting expression like:
(let ( (val (complex-expr ...)) )
(or (pred1 val) (pred2 val) ...(predN val)))
Instead I like something:
(apply-some (complex-expr ...) '(pred1 pred2 ... predN))

gavenkoa
- 3,452
- 20
- 37
4
votes
1 answer
Running a function every time a character is inserted
I want to check its properties as soon as a Greek word has been
inserted into the buffer. As far as I can see, this requires me to
check each time the buffer changes, whether, if a Greek word was being
typed, the last character was not a word…

Toothrot
- 3,264
- 1
- 13
- 32
3
votes
1 answer
post-command hook for org-cut-subtree
I want to run newline command after whenever I run org-cut-subtree. I tried
(add-hook 'after-org-cut-subtree-hook 'newline)
(add-hook 'org-cut-subtree-hook 'newline)
(add-hook 'org-after-cut-subtree-hook 'newline)
but none of them worked. How can I…

stacko
- 1,597
- 1
- 11
- 19
3
votes
2 answers
Emacs Equivalent of Vims's Auto Commands?
I'm currently trying to move from vim to emacs so be gentle with me ;)
In Vim, one of my favorite features is auto commands, I'll have one for each project for example:
au BufEnter c:/projects/foo/* :call FooSetup()
au BufEnter c:/projects/bar/*…

flukus
- 193
- 5
3
votes
3 answers
Debug why a function is removed from post-command-hook
I am trying to implement the auto-indentation snippet posted by @Malabarba in one of the emacs SE questions.
From that post, below is the function that I am trying to add to the post-command-hook:
(require 'cl-lib)
(defun endless/indent-defun ()
…

Kaushal Modi
- 25,651
- 4
- 80
- 183
2
votes
1 answer
Automatically add a newline when dragging text from Firefox into Emacs
One of my workflows is drag lines from Firefox to Emacs (X).
What I'm trying to accomplish is putting a new line after dragging every line.
I've tried zillions of combination but I can't find the correct syntax.
Here is one of my more promising…

Javier Ortega
- 23
- 4