Emacs Hooks - How to Customize Emacs
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
(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?
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
-
Creating An Emacs Command
-
Whenever you press a key in emacs, a command is run. E.g. if you press 'a' it calls a command called self-insert-command. Much of the power of emacs derives from the fact that anyone can define their own command in emacs lisp. This command will look...
-
The Magic of Emacs Comint
-
Comint is one of the main reasons I moved to emacs after using vi/vim for 14 years. It is an emacs library for interacting with external processes. Many text editors have a way of calling out to a process and synchronously getting the result. Emacs g...
-
Ediff - All About The Emacs Diff Comparison Tool
-
Ediff is a tool for comparing two different pieces of text. How is that useful? Well, consider if you sent an article for someone to proofread. When they send you the revised article (and a bill) how do you find out what they changed? Ediff is the an...
Reader Feedback
Was This Useful
Let me know if there is anything you would add or change. Thanks.
-
Reply
- amy1985 amy1985 Apr 19, 2009 @ 8:50 am
- Thanks this is a great step by step, easy to follow guide. You made something which appears very complicated much more simple - thanks again.
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
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
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






