Skip to main content
Share Your Experience: Take the 2024 Developer Survey
15 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 ...
sds's user avatar
  • 6,124
15 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 :...
dakra's user avatar
  • 406
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's user avatar
  • 33.1k
10 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 ...
phils's user avatar
  • 51.5k
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 ...
Stefan's user avatar
  • 26.5k
9 votes
Accepted

Different themes in different modes

AFAIK you can't do this with the 'theme' system. I'm pretty sure themes are global. You can have buffer-local face remapping however, and that could take place in a mode hook, so I think there's a ...
phils's user avatar
  • 51.5k
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() ...
npostavs's user avatar
  • 9,203
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).
Stefan's user avatar
  • 26.5k
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 ...
Manuel Uberti's user avatar
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))...
phils's user avatar
  • 51.5k
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 ...
Stefan's user avatar
  • 26.5k
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 () ...
Stefan's user avatar
  • 26.5k
7 votes
Accepted

Is there a hook that runs for all read only buffers

If you want this to act in any buffer, not just a file-visiting buffer then find-file-hook is not appropriate. (You said "all buffers", but you also spoke of editable/non-editable "files".) If you ...
Drew's user avatar
  • 77.9k
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 ...
phils's user avatar
  • 51.5k
6 votes

Looking for something like a `first-keypress-in-a-while-hook`

I guess a solution of your problem is setting a flag with an idle timer, https://www.gnu.org/software/emacs/manual/html_node/elisp/Idle-Timers.html and checking/unsetting it with post-self-insert-...
Vera Johanna's user avatar
6 votes

add-to-list vs add-hook?

In some cases you can use either function to add an element to a list. That you can do that does not mean that you should, however. The recommendation by Emacs is to use only add-hook for a hook. ...
Drew's user avatar
  • 77.9k
6 votes
Accepted

Is there a hook that runs when exiting read-only-mode?

Like all minor mode hooks, read-only-mode-hook runs when entering or leaving read-only mode. So you only need to make the setting of the cursor a bit smarter, probably by checking the value of buffer-...
NickD's user avatar
  • 30.5k
6 votes

Hook that fires any time ANY file is opened?

find-file-hook Documentation: List of functions to be called after a buffer is loaded from a file. The buffer’s local variables (if any) will have been processed before the functions are called.
matteol's user avatar
  • 2,018
6 votes
Accepted

How to run a 'prog-mode-hook' after more specific hooks?

You can use after-change-major-mode-hook: (add-hook 'after-change-major-mode-hook 'my-after-change-major-mode-prog-mode) (defun my-after-change-major-mode-prog-mode () "Custom `after-change-major-...
phils's user avatar
  • 51.5k
6 votes

In org mode, how can I make a post-capture hook run only for certain capture templates?

See my answer here: https://emacs.stackexchange.com/a/48566/18296 Here’s the example code from it: (defun tina/test-finalize () (let ((key (plist-get org-capture-plist :key)) (desc (plist-...
Tina Russell's user avatar
6 votes
Accepted

Check if current user undoing/redoing?

C-hv undo-in-progress Documentation: Non-nil while performing an undo. Some change-hooks test this variable to do something different. Bearing in mind that "redoing" is really still "undoing" in ...
phils's user avatar
  • 51.5k
5 votes
Accepted

save a particular buffer without prompting on emacs exit?

When Emacs exits, it saves buffers by calling save-some-buffers. This is the function that does the prompting. You can turn off the prompting in a buffer by setting the buffer-local variable buffer-...
Gilles 'SO- stop being evil''s user avatar
5 votes
Accepted

Is there a way to detect change of monitor in Emacs / Elisp?

One way to do this is to save the original monitor for each frame (using frame-monitor-attributes) and then run a timer, where you check the current monitor. If you run an idle timer and run it a few ...
Lindydancer's user avatar
  • 6,170
5 votes
Accepted

post-command hook for org-cut-subtree

I typed M-x find-function RET org-cut-subtree RET and saw that it was just a few lines of code that calls org-copy-subtree. Then, I typed M-x find-function RET org-copy-subtree RET and looked through ...
lawlist's user avatar
  • 19.2k
5 votes
Accepted

How to make *Messages* use visual-line-mode?

None of those hooks will apply because the buffer (and its mode) has already been created long before your add-hook happens. I suggest you add (with-current-buffer "*Messages*" (visual-line-mode)) ...
Stefan's user avatar
  • 26.5k
5 votes
Accepted

how to disable minor mode (enabled using prog-mode-hook) for a particular major mode

This can be done very easily with hooks for major-modes like so: (add-hook 'python-mode-hook (lambda () (highlight-indent-guides-mode -1))) If for some reason this did ...
Chakravarthy Raghunandan's user avatar
5 votes
Accepted

Detect interactive use of `kill-buffer` in `kill-buffer-hook`

Test the variable this-command to see whether it is kill-buffer
phils's user avatar
  • 51.5k
5 votes

Only enable auto-revert-mode for the current buffer

Since Emacs 24.4, auto-revert-mode does not poll anymore. Instead, it uses file notifications from your underlying OS. There shouldn't be any performance problem; people have reported 600+ buffers in ...
Michael Albinus's user avatar
5 votes
Accepted

Delete trailing whitespace when saving, except certain modes

(add-hook 'before-save-hook (when '(not fundamental-mode)) 'delete-trailing-whitespace) You are not passing a function as the second arg to add-hook. You are passing the ...
Drew's user avatar
  • 77.9k
5 votes

Running a function every time a character is inserted

Edit: Trying to compile all comments to this answer into one large text. Please upvote the comments instead of the answer. There are several ways how you can approach this. It depends on the details ...
Stefan Kamphausen's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible