Emacs Hooks - An Introduction

1 - I can do better 2 - Jury's out 3 - Pretty darn good 4 - Splendiferous 5 - Awesometastic by 4 people | Log in to rate

Ranked #2,225 in Tech & Geek, #58,231 overall

Emacs Hooks - How to Customize Emacs

Hooks provide an clean, organised mechanism for extending emacs. Any function in emacs may include one or more hooks where users can add their own functions to be run.

Windows Notepad users can add .LOG to the top of a textfile in order to support a crude diary. Every time that file is opened, the date is inserted. We could add a function to find-file-hook to get a similar effect. How do you do this?

If you like this lens and found it useful, please consider giving it a rating. Thanks.


Once you have used hooks to customize emacs you might want to look at creating a full application using emacs comint.

Photo by Southern Foodways Alliance

Defining The Hook Function 

The step by step guide

First of all define a function called jd-add-timestamp (defun means def ine fun ction).

(defun jd-add-timestamp ()



Then, only when we are looking-at the text ".LOG " ... (in regexes period (.) means match anything, so we need to escape it with double backslash.)

(when (looking-at-p "\\.LOG ")



Go forward 5 characters... (".LOG " is 5 characters long.)

(forward-char 5)



Insert the timestamp... (format-time-string enables you to include whichever time elements you want, and in any order. %Y represents the year, %m the month, %d the day. Type C-h f format-time-string<RET> for more information.)

(insert (format-time-string "%Y%m%d %H:%M:%S"))



Insert two newlines...

(insert "\n\n")



...and go to the previous line

(previous-line)



Finally, add this function to find-file-hook.

(add-hook 'find-file-hook 'jd-add-timestamp)

The complete code looks like this: 

(defun jd-add-timestamp ()
  (when (looking-at-p "\\.LOG ")
    (forward-char 5)
    (insert (format-time-string "%Y%m%d %H:%M:%S"))
    (insert "\n\n")
    (previous-line)
    (message "Added timestamp")))

(add-hook 'find-file-hook 'jd-add-timestamp)

How Do You Find Out Which Hooks are Available? 

M-x apropos allows you to find out which functions in emacs match a regular expression. Hooks all finish with either hook or functions. M-x apropos<RET> hook$\|functions$<RET>.

By convention, the difference between a hook ending in -hook and one ending in -functions is that one ending in -functions will have some arguments. For example, hooks added to after-change-functions, which is called each time the text in a buffer changes, are called with three arguments:

"the positions of the beginning and end of the range of changed text,
and the length in bytes of the pre-change text replaced by that range."

My Emacs Lenses 

Reader Feedback 

Was This Useful

Let me know if there is anything you would add or change. Thanks.

submit

Latest Emacs Blog Posts  

Aquamacs: Emacs for Mac OS X
Aquamacs is an Emacs for Mac OS X that will feel mostly like an Aqua program - while still being a real GNU Emacs with all the ergonomy and extensibility you've come to expect from this world-class editor.

Vanilla Emacs 23 Hook List 

Buffer-menu-mode-hook
abbrev-with-wrapper-hook
activate-mark-hook
after-change-major-mode-hook
after-init-hook
after-revert-hook
after-save-hook
after-setting-font-hook
apropos-mode-hook
auto-save-hook
before-hack-local-variables-hook
before-init-hook
before-make-frame-hook
before-revert-hook
before-save-hook
buffer-menu-mode-hook
change-major-mode-hook
clone-buffer-hook
clone-indirect-buffer-hook
compilation-mode-hook
completion-list-mode-hook
completion-setup-hook
custom-define-hook
cvs-dired-use-hook
deactivate-mark-hook
delete-frame-hook
disabled-command-hook
echo-area-clear-hook
emacs-lisp-mode-hook
emacs-startup-hook
epa-file-find-file-hook
erc-ctcp-query-DCC-hook
exit-language-environment-hook
expand-abbrev-hook
find-file-hook
find-tag-hook
first-change-hook
grep-setup-hook
hack-local-variables-hook
help-mode-hook
inferior-lisp-mode-hook
input-method-activate-hook
input-method-after-insert-chunk-hook
input-method-inactivate-hook
isearch-mode-end-hook
isearch-mode-hook
kill-buffer-hook
kill-emacs-hook
lisp-interaction-mode-hook
lisp-mode-hook
mail-citation-hook
mail-setup-hook
make-local-hook
menu-bar-update-hook
minibuffer-exit-hook
minibuffer-setup-hook
minibuffer-with-setup-hook
mouse-leave-buffer-hook
next-error-follow-mode-post-command-hook
next-error-hook
occur-hook
occur-mode-find-occurrence-hook
occur-mode-hook
paragraph-indent-text-mode-hook
pmail-delete-message-hook
pmail-get-new-mail-hook
pmail-mode-hook
pmail-quit-hook
pmail-show-message-hook
post-command-hook
post-gc-hook
pre-abbrev-expand-hook
pre-command-hook
resume-suspend-hook
rfn-eshadow-setup-minibuffer-hook
rfn-eshadow-update-overlay-hook
rmail-delete-message-hook
rmail-get-new-mail-hook
rmail-mode-hook
rmail-quit-hook
rmail-show-message-hook
set-language-environment-hook
special-mode-hook
suspend-hook
suspend-resume-hook
table-cell-map-hook
table-load-hook
table-point-entered-cell-hook
table-point-left-cell-hook
temp-buffer-setup-hook
temp-buffer-show-hook
term-setup-hook
text-mode-hook
tooltip-hook
vc-before-checkin-hook
vc-checkin-hook
vc-checkout-hook
vc-default-find-file-hook
vc-default-find-file-not-found-hook
vc-file-not-found-hook
vc-find-file-hook
vc-kill-buffer-hook
view-mode-hook
window-configuration-change-hook
window-setup-hook

Vanilla Emacs 23 Functions List 

abbrev-expand-functions
after-change-functions
after-insert-file-functions
after-make-frame-functions
auto-coding-functions
before-change-functions
buffer-access-fontify-functions
choose-completion-string-functions
comint-output-filter-functions
command-line-functions
custom-print-functions
delete-frame-functions
delete-terminal-functions
find-directory-functions
find-file-not-found-functions
font-lock-extend-region-functions
fontification-functions
jit-lock-after-change-extend-region-functions
kill-buffer-query-functions
kill-emacs-query-functions
kkc-after-update-conversion-functions
resume-tty-functions
signal-hook-function
suspend-tty-functions
update-leim-list-functions
window-scroll-functions
window-size-change-functions
window-text-change-functions
write-contents-functions
write-file-functions
write-region-annotate-functions
x-lost-selection-functions
x-sent-selection-functions

Explore related pages