32
votes
Accepted
What is the difference between ' and #' in front of a symbol?
In Emacs Lisp, if foo is a symbol, then 'foo and #'foo are completely equivalent. The latter form (with #') is preferred when foo is a function, as it documents the fact that it is intended to be ...
25
votes
Accepted
Remove hooks for specific modes
I don't have any experience with the use-package macro, but in principle, you could use the third argument to remove-hook which tells it to remove the function only from the local hook:
(remove-hook '...

Dan♦
- 32.3k
21
votes
Accepted
Run command when opening a file of a specific filetype
In Emacs terminology, these are two different steps:
Associate files with the .ts extension with the major mode typescript-mode.
Run the function tss-setup-current-buffer when Typescript mode starts.
...
21
votes
Accepted
What's the difference between after-init-hook and emacs-startup-hook
See the Startup Summary section of the Emacs Lisp manual for a detailed description of the things that happen when you start Emacs.
The emacs-startup-hook runs later than the after-init-hook.
One ...
14
votes
Accepted
add-to-list vs add-hook?
The question you asked
The biggest difference is the last argument:
local for add-hook and
compare-fn for add-to-list.
This means that you have no control over how add-hook decides whether it ...
14
votes
How to avail of `:hook` using use-package?
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
:...
12
votes
Accepted
Run code right after compilation
Try using compilation-finish-functions:
(defun my-compilation-finish-function (buffer desc)
(message "Buffer %s: %s" buffer desc))
(add-hook 'compilation-finish-functions 'my-compilation-finish-...
12
votes
Accepted
how to unbind a mode hook without restarting
Using function symbols certainly makes this easier (and this is one of the reasons why I would recommend always using that approach). You can still use remove-hook with the lambda form, though -- it ...
11
votes
Run command when opening a file of a specific filetype
What you are looking for is find-file-hook:
(add-hook 'find-file-hook 'my-project-hook)
(defun my-project-hook ()
(when (string= (file-name-extension buffer-file-name) "ts")
(typescript-mode)
...
11
votes
Accepted
Quitting Emacs and re-enabeling touchpad
You can use kill-emacs-hook which is run when emacs is quit 'normally'.
Hook run when kill-emacs is called. Since kill-emacs may be
invoked when the terminal is disconnected (or in other similar
...
11
votes
Accepted
Prevent folding org files opened by ediff
It sounds like your goal is to have org-mode files always show everything when in ediff. The simplest solution is probably to avoid the local variables approach and just put show-all in the relevant ...

Dan♦
- 32.3k
10
votes
Accepted
How to automatically remove a hook provided by a minor mode after disabling that mode?
The minor mode's hook is called both when activating and deactivating the minor mode, so how 'bout
(add-hook 'my-minor-mode-name-hook
(lambda ()
(if my-minor-mode-name
...
9
votes
Accepted
Run function after specific snippet
You can make snippet-specific bindings with # expand-env:
# -*- mode: snippet -*-
# name: ipdb
# key: ipdb
# expand-env: ((yas-after-exit-snippet-hook #'bm-toggle))
# --
import ipdb; ipdb.set_trace()
...
8
votes
Accepted
(la)tex-mode-hook doesn't work?
If you have AUCTeX installed, then you're presumably using AUCTeX's modes, so the hooks you need to use have names of the form TeX-mode-hook and LaTeX-mode-hook (note the capitalization).
8
votes
Buffer-local after-save-hook?
This is standard functionality built into add-hook:
add-hook is a compiled Lisp function in ‘subr.el’.
(add-hook HOOK FUNCTION &optional APPEND LOCAL)
Add to the value of HOOK the ...
8
votes
Accepted
How to replace focus-out-hook with after-focus-change-function in emacs 27?
You could try using something like:
(add-function :after after-focus-change-function #'your-function-here)
So, in your case, something like this should do what you are after:
(add-function :after ...
8
votes
Accepted
Add hooks for a file type based on org-mode
(define-derived-mode hy-mode org-mode "Hy Mode"
"Major mode for editing *.hy files."
do-whatever-you-want-here)
(add-to-list 'auto-mode-alist '("\\.hy\\'" . hy-mode))...
7
votes
Accepted
What's a good way to have a hook that runs for every file in a given directory?
find-file-hook, and in your hook function, check whether the candidate file is in the given directory.
7
votes
Accepted
Use `.dir-locals.el' to append to `before-save-hook' as a buffer local variable
(add-hook 'before-save-hook #'clang-format-buffer nil t)
This is the right way to add a function locally to a hook.
This will set before-save-hook as a buffer local variable ignoring everything ...
7
votes
Accepted
How to run an `after-save-hook` only when the buffer has been saved manually
Firstly you need to define what "saved manually" actually means. It could cover multiple commands, to my mind. M-x apropos-command RET save\|write RET probably includes all the typical cases.
Once ...
7
votes
Accepted
What is a hook?
Your understanding of Emacs Lisp notion of hook is absolutely correct. Indeed,
“normal” hooks are just lists of functions (every function doesn't take
arguments, otherwise it's not usually called “...
7
votes
Is there a standard function that applies a list of predicates to a value until success?
You can use cl-some to apply a list of predicates to a value:
(cl-some (lambda (p) (funcall p "some value"))
'(vectorp listp stringp))
I suppose you could wrap this in a function. Although ...
7
votes
Accepted
Why is this function not working properly, when added to a hook?
Dunno what Evil might change in the picture (try without Evil, to see if there is a difference).
But if you are interested in the buffer name of a buffer visiting a file then what you should use is ...
7
votes
Accepted
Is there a hook I can use to run my function when point moves?
I think post-command-hook is what you wish to use.
It is run every time a command has been run. And a command is basically any interactive action, such as clicking a mouse button, typing a letter (...
7
votes
Accepted
How to track user changes in the buffer?
To react to buffer changes, you generally want to use after-change-functions (or sometimes before-change-functions).
These hooks are run everytime some part of the buffer is modified, either by ...
7
votes
Accepted
Looking for something like a `first-keypress-in-a-while-hook`
You could try:
(defun my-run-fkpiawh ()
(remove-hook 'pre-command-hook #'my-run-fkpiawh)
(run-hooks 'first-keypress-in-a-while-hook))
(run-with-idle-timer 1200 t (lambda ()
...
7
votes
Accepted
Hook with quoted lambda as an argument
(add-hook 'coffee-mode-hook '(lambda () (coffee-custom)))
Definitely not recommended.
Quoting lambdas like that is not good practice.
As you say, (add-hook 'coffee-mode-hook #'coffee-custom) would ...
6
votes
What's a good way to have a hook that runs for every file in a given directory?
This sounds like what directory local variables are for. Just create a .dir-locals.el file in the directory with the settings you want, and every file will inherit those settings.
6
votes
Smart minor mode toggle depending on major mode
A hook expects its entries to be functions. (fci-mode 1) is not a function.
What you want to use as a function is presumably turn-on-fci-mode, if that function exists, or (lambda () (fci-mode 1)), ...
6
votes
Accepted
Run hook on new buffer
You can advise create-file-buffer, if you care only about file buffers.
Likewise, you can advise rename-buffer.
That's what uniquify does, for instance. Are you familiar with uniquify.el? Maybe it ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
hooks × 368org-mode × 36
minor-mode × 22
init-file × 19
buffers × 15
elisp × 15
major-mode × 12
key-bindings × 11
evil × 11
use-package × 11
window × 10
advice × 10
magit × 8
debugging × 8
frames × 8
themes × 8
files × 7
buffer-local × 7
spacemacs × 6
org-agenda × 6
faces × 6
saving × 6
kill-buffer × 6
exit × 6
minibuffer × 5