Emacs: What's Hook?

By Xah Lee. Date: . Last updated: .

What's a hook?

A hook is a variable, its value is a list of functions (lisp symbols or lambda).

Each hook is designed to “run” when some event happens. When a hook “run”, all functions in that hook are called.

For example,

There are hundreds of hooks. Each major mode usually have at least 1 hook, designed to run when the mode is loaded. [see List of Emacs Hooks]

A hook is similar to the concept of events in other systems. Adding function 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.)

Examples of Using Hook

Here's example of use variable-width font for some modes:

;; use variable-width font for some modes
(add-hook 'emacs-lisp-mode-hook 'variable-pitch-mode)
(add-hook 'ruby-mode-hook 'variable-pitch-mode)
(add-hook 'js-mode-hook 'variable-pitch-mode)
(add-hook 'css-mode-hook 'variable-pitch-mode)
(add-hook 'nxml-mode-hook 'variable-pitch-mode)
(add-hook 'html-mode-hook 'variable-pitch-mode)
(add-hook 'mhtml-mode-hook 'variable-pitch-mode) ; emacs 26

[see Emacs: Avoid Lambda in Hook]

How to List All Hooks?

List of Emacs Hooks

How to find the name of hook?

Most major modes have a hook. If a mode's name is “xyz-mode”, its hook by convention is named “xyz-mode-hook”.

First, be sure you load the mode first. Some hook many not show when the mode isn't loaded or initialized.

To load a mode, just e.g. Alt+x js-mode

To find a mode's hook,

Alt+x describe-variable, then try to type a name you guessed, press Tab for completion.


  1. Alt+x apropos-variable
  2. Type “-hook”, press Enter.

Note: A major mode may have more than 1 hook.

How to See the Value of a Hook?

Alt+x describe-variable, then type the hook name, to see its current value.

How to Remove a Function in a Hook?

;; remove a hook
(remove-hook 'html-mode-hook 'xyz-function-name)

How to Bind Keys for Major Mode?

Emacs: Change Major Mode Keys

Avoid Lambda in Hook

Emacs: Avoid Lambda in Hook

Emacs Customization

  1. Emacs init file
  2. Install Packages
  3. Install Package Manually
  4. Define Keys
  5. M-x customize
  6. What's Major Mode?
  7. What's Minor Mode?
  8. Set File to Open in a Major Mode
  9. Organize Init File
  10. Byte Compile Elisp
  11. What's Hook?
  12. Environment Variables in Emacs
  13. Set Default Window Size
  14. Font Setup
  15. Set Color Theme
  16. Turn Off Auto Backup
  17. Check OS, Version, Host Name
  18. Check Defined/Loaded

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp