Emacsist RSS --- If never, make it ever: C-h C-h

Emacs: What's Hook?


这里 查看更多 Emacs 相关推荐文章最新 Emacs 圈的动态. 欢迎关注微信公众账号: Emacsist

Hick 叽喳:

    hook  Emacs 的一个重要机制, 个人感觉挺像 Javascript 里的事件的, 但是有它独特的地方.

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,

  • when html-mode is loaded, html-mode-hook's functions are run.
  • when any command is called, post-command-hook's functions are run.

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 eventsin 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, not events by emacs.)

Examples of Using Hook

Here's example of turning on line number only for some modes:

(add-hook 'emacs-lisp-mode-hook 'linum-mode)
(add-hook 'js-mode-hook 'linum-mode)
(add-hook 'css-mode-hook 'linum-mode)
(add-hook 'html-mode-hook 'linum-mode)
(add-hook 'nxml-mode-hook 'linum-mode)

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

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

〔?see Emacs: Avoid Lambda in Hook

How to List All Hooks?

List of Emacs Hooks

How to find the name of hook?

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.

Call describe-variable, then try to type a name you guessed, press Tab ? for completion.


  1. call apropos-variable
  2. type -hook, Enter ?.

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

How to See the Value of a Hook?

How to see the value of a hook?

Call describe-variable, then type the hook name, to see its current value.

How to Remove a Function in a Hook?

How to remove a hook?

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

How to Bind Keys for Major Mode?

See: Emacs: How to Bind Keys for Major Mode?

;; example of binding keys only when html-mode is active

(defun my-html-mode-config ()
  "Modify keymaps used by `html-mode'."
  (local-set-key (kbd "C-c w") 'my-bold-word)
  (local-set-key (kbd "C-c C-p") nil) ; remove a key
  (local-set-key (kbd "<C-right>") 'my-html-skip-tag-forward)
  (local-set-key (kbd "<C-left>") 'my-html-skip-tag-backward)
  ;; more here

;; add to hook
(add-hook 'html-mode-hook 'my-html-mode-config)
原文出处: 李杀
原文地址: http://ergoemacs.org/emacs/emacs_whats_hook.html
原文时间: 2016-04-12 20:52
本文地址: http://emacsist.com/10850
整理时间: 2016-04-22 04:14

本文由 Hick 整理,转载请保留以上信息;
COPYLEFTThe articles on this site come from Internet, thanks to all the original authors.
      If anything about COPYRIGHT, or LEFT, please contact Emacsist at gmail dot com .