Hot answers tagged

32 votes
Accepted

Proper way to enable minor mode

It depends on which version(s) of Emacs you are using (or targeting). Iff you are exclusively using Emacs 24+ then you can safely use variant A: * Incompatible Lisp Changes in Emacs 24.1 ** Passing ...
user avatar
  • 43k
27 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 ...
user avatar
  • 5,480
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 '...
user avatar
  • 31k
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. ...
user avatar
19 votes
Accepted

Where should I include a lazy initialization?

A lazy init is a good init. (IMO) When to use with-eval-after-load with-eval-after-load is loaded once when a certain feature or file is first loaded, so a keymap-change clearly goes inside one of ...
user avatar
  • 2,622
19 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 ...
user avatar
  • 19.1k
17 votes

How to enter view-only mode when browsing Emacs source code from help?

You can use directory-local variables to make Emacs' source files read-only by default. (See also C-hig (emacs) Directory Variables RET). Create a file called .dir-locals.el at the root of the ...
user avatar
  • 43k
16 votes
Accepted

Execute external script upon save when in a certain mode?

Here's a simple setup that you can modify that allows you to sync org files to your Raspberry Pi, based on your bash script (untested, of course, because I have neither a Pi nor your script). Modify ...
user avatar
  • 31k
14 votes
Accepted

How to find out what a key sequence really does

There is no easy way to know exactly what a single key press will do. If you see additional behavior always check the common hooks. See the list here: http://www.gnu.org/software/emacs/manual/...
user avatar
14 votes
Accepted

before-save-hook for cc-mode

One fairly common way of restricting the action of a hook to a particular major mode is to check the value of major-mode. For example, this hook prints a message before saving buffers using c++-mode: ...
user avatar
  • 8,912
12 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 ...
user avatar
  • 5,417
11 votes
Accepted

Is there any way to run a hook function only once?

I'm guessing that you are not really looking for a way to "execute the hook only once". I'm guessing that you are looking for a way to execute that particular function only once, whenever the hook is ...
user avatar
  • 69.1k
11 votes
Accepted

Disable hl-line-mode only for eshell and ansi-term

global-hl-line-mode sets variable global-hl-line-mode to t. The global mode is separate from the non-global (hl-line-mode). The following will disable it in the selected mode: (add-hook 'eshell-...
user avatar
11 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-...
user avatar
  • 20.9k
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 ...
user avatar
  • 7,278
11 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 ...
user avatar
  • 43k
11 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 :...
user avatar
  • 346
10 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 ...
user avatar
  • 31k
9 votes

Proper way to enable minor mode

Why would you define a new command that does exactly what bar-minor-mode does? Starting with 24.1, all of these are completely equivalent, so just use the less redundant one: option A (add-hook '...
user avatar
  • 22.3k
9 votes
Accepted

latex-mode-hook or LaTeX-mode-hook?

LaTeX-mode-hook is used by AUCTeX's LaTeX mode. latex-mode-hook is used by Emacs' built-in latex mode.
user avatar
9 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) ...
user avatar
  • 5,417
9 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 ...
user avatar
  • 25.2k
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() ...
user avatar
  • 8,783
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))...
user avatar
  • 43k
7 votes
Accepted

Turn off jabber notifications when Emacs has focus

In Emacs 24.4 you should be able to use (add-hook 'focus-out-hook #'turn-on-my-jabber-sound) (add-hook 'focus-in-hook #'turn-off-my-jabber-sound) (defun turn-on-my-jabber-sound () ;; t for `on' (...
user avatar
7 votes

Proper way to enable minor mode

Here's one more way to do it that has some advantages, assuming that you're editing your own config, not a package that you distribute. (add-hook 'foo-mode-hook #'custom-foo-hook) (defun ...
user avatar
  • 13.6k
7 votes
Accepted

Turn a non prog-mode derived major-mode into a prog-mode derived major-mode

All you have to do is add (run-hooks 'prog-mode-hook) to erlang-mode-hook: (add-hook 'erlang-mode-hook (lambda () (run-hooks 'prog-mode-hook))) You should place this after anything else ...
user avatar
  • 12.1k
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 ...
user avatar
  • 22.3k
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.
user avatar
  • 69.1k

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