I'm using the clocking functionality quite extensively, and I built a number of tools (outside of Emacs) that will look for clocked-in tasks. The problem is that these tools will parse the file on disk, and therefore only pick up the clock entries if the buffer was saved in the meantime. I could just save the buffer manually, but I'm also using org-mru-clock, so it might not always be evident which buffer to save.

So I wanted to ask whether there is an easy way to hook into the clock-in/-out events to determine the buffer the task is in, and just save it.

  • 2
    One idea would be to advise org-clock-in and org-clock-out to save-buffer at the outset, and several examples of how this might work can be found by Googling: advice before emacs ...
    – lawlist
    Commented Apr 16, 2018 at 14:35

2 Answers 2


Emacs has cool feature - defadvice. You can surround any function with your own operation.

In this case, something along the lines of

(add-hook 'org-mode-hook (lambda ()
                           (defadvice org-clock-in (after org-clock-in-after activate) (save-buffer))
                           (defadvice org-clock-out (after org-clock-out-after activate) (save-buffer))))

in your .emacs would make save-buffer happen after either clock-in or clockout.

Taken from :

  • 1
    Rather than add-hook it might be better to use eval-after-load so that you don't readvise the function every time you open an org file.
    – erikstokes
    Commented Apr 17, 2018 at 21:59

org-clock-in and org-clock-out have "hooks" associated with them --- lists of extra functions that run whenever you clock in/out. You can add your own functions (which will be called with no arugment) using add-hook:

(add-hook 'org-clock-in-hook #'save-buffer)
(add-hook 'org-clock-out-hook #'save-buffer)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.