Context When working on a larger document in org-mode I would like to clock the whole work time without gaps. As I usually have a logbook in each section I wonder if it's possible to automatically clock in when moving the cursor into a section and clock out when leaving it.

As far as I know there are only projects that clock out automatically when finishing tasks and that clock in a default clock afterwards. Although I really like the approach I have the feeling that it's not appropriate when writing a longer document whose sections usually get into the finished state very late and where sometimes jumping between section occurs, for example in periods in which only small issues are fixed.

The first time at least I saw such automatic clocking was in Bernt Hansen's org-mode configuration.

Question: How can automatic clocking be implemented, such that there is a clock-in when moving into a section and a clock-out when moving out of a section?

  • Do you think one could use keyboard input event listener to check wether the key stroke inserted a new character into the buffer and if yes just clock in if a clock is not already running in this section?
    – Christoph
    Jun 3, 2015 at 10:55

1 Answer 1


This seems pretty close. I set this up to work on sections with an AUTOCLOCK property. I don't know a more elegant way to do it than the post-command-hook.

#+BEGIN_SRC emacs-lisp
(defun action-1 ()
  "hook function to run in headings with a clock"
  (and (eq major-mode 'org-mode)
       (not (org-before-first-heading-p)))
  (if (org-entry-get (point) "AUTOCLOCK")
    (when (marker-buffer org-clock-marker) (org-clock-out))))

(add-hook 'post-command-hook 'action-1)

EDIT 12-17-2020 The solution above seems to have quit working at some time. Here is a more sophisticated approach that seems to work today.

(defun in-same-heading-as-clock-p ()
  "Check if the cursor is in the same heading as the current clock.
That means:
1. There is a current clock
2. The cursor is in the same buffer as that clock.
3. The cursor is in the same heading as that clock."
  (let ((cb (current-buffer))
    (clockb (marker-buffer org-clock-marker))

    (when (and
       clockb             ; clock buffer
       ;; clock buffer is the same as this buffer
       (eq cb clockb))
      (setq clock-hb (save-excursion
               (goto-char (marker-position org-clock-marker))
               (org-back-to-heading t)
        cursor-hb (save-excursion
            (org-back-to-heading t)
      (= cursor-hb clock-hb))))

(defun action-1 ()
   ;; clock is running in this heading, do nothing

   ;; clock is running in another heading. IF this heading has autoclock
   ((and (marker-buffer org-clock-marker)
     (not (in-same-heading-as-clock-p)))
    ;; first clock out
    (when (org-entry-get (point) "AUTOCLOCK")
   ;; no clock is running, and 
   ((and (null (marker-buffer org-clock-marker))
     (org-entry-get (point) "AUTOCLOCK"))

(add-hook 'post-command-hook 'action-1)
  • That's great I'll check it out tomorrow!
    – Christoph
    Apr 27, 2016 at 13:42
  • Thanks, me too I was looking for exactly this. Setting "INHERIT" to t, like (if (org-entry-get (point) "AUTOCLOCK" t), makes it possible that only level-1-headings have to have the AUTOCLOCK tag (in case someone, like me, wants to clock in on any heading).
    – purlupar
    Dec 11, 2020 at 12:25
  • @John-Kitchin, do you also experience org inserting a new clocking line (wiht 0 minutes sum) on each single character that is edited (typed into) a headline, while typing?
    – purlupar
    Dec 15, 2020 at 15:59
  • yes, I see that now. I guess something has changed maybe since I wrote that solution. Dec 16, 2020 at 23:16

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.