Emacs: What is Hook

By Xah Lee. Date: . Last updated: .

What is Hook

A hook is a variable, its value is a list of functions ( Symbol or Lambda ).

Hook variables are associated with events. When the event happens, all functions in that hook are called.

Hook is similar to the concept of event in JavaScript . Adding functions to a hook is similar to adding event handlers. (note: emacs lisp manual also uses the term “event”, but that is lower level events to emacs (such as pressing a key), not events from emacs.)

For example,

There are hundreds of hooks. Each Major Mode usually have at least 1 hook, designed to run when the mode is loaded.

Add Function to Hook

Here's example of setting proportional width font for some modes:

;; use proportional width font for some modes
(add-hook 'js-mode-hook 'variable-pitch-mode)

Tip: hook is often used to change Keys for Major Mode . see Emacs: Change Major Mode Keybinding

Remove Function in Hook

(remove-hook 'js-mode-hook 'variable-pitch-mode)

Show Value of a Hook

Emacs Lisp: Create a Hook

For emacs lisp programers: Emacs Lisp: Create a Hook

Emacs Hook

Major Mode, Minor Mode, Minibuffer, Hook