I use Org-mode in Emacs to keep track of time. I sometimes clock in and out of the same task back-to-back, leading to two contiguous clock time intervals, such as:

CLOCK: [2019-02-27 Wed 10:21]--[2019-02-27 Wed 10:24] =>  0:03
CLOCK: [2019-02-27 Wed 08:40]--[2019-02-27 Wed 10:21] =>  1:41

To reduce clutter and disk space, I would like to combine them into a single interval such as:

CLOCK: [2019-02-27 Wed 08:40]--[2019-02-27 Wed 10:24] =>  1:44

Can Org-mode automatically collapse or combine clock times together?

  • What happens to clocks in subheadings? Maybe those could be joined below the common ancestor. But, that depends on the application. What happens to combinations of clocks directly below the heading and clocks in logbook drawers? – Tobias Feb 27 at 14:07
  • @Tobias: it's possible to add a time interval in a heading and a different interval in a subheading, so those would not be contiguous because they are on different headings. I have my clock timer outside of logbooks, so Org mode could check contiguous intervals only with the same setting (inside or outside logbooks). Are these answers what you meant? – miguelmorin Feb 27 at 15:33
  • Org-mode does not do this by default. To get this functionality, someone needs to write a function to this effect. Code that does the opposite, i.e. splits clocked times, can be found here: github.com/justintaft/org-clock-split – Heikki Feb 28 at 9:04

This somewhat naive function does what you want but this initial version leaves choosing the right clock entries to the user:

(defun org-clock-merge ()
  "Merge the org CLOCK line with the next CLOCK line.

Require that the end and start time stamps are identical.
Or should there be a threshold value for difference?
If fail, move the cursor one line down.
Universal argument should merge the lines even
if there is a time gap."

  (let (this-clock-line next-clock-line timestamp-end)
    ;; is this line a valid CLOCK line?
    (setq this-clock-line
          (buffer-substring (line-beginning-position) (line-end-position)))
    (if (not (string-match org-ts-regexp-both this-clock-line))
        (error "Cursor must be placed on line with a valid CLOCK entry"))

    ;; is next line a valid CLOCK line?
    (setq next-clock-line
          (buffer-substring (line-beginning-position) (line-end-position)))
    (if (not (string-match org-ts-regexp-both next-clock-line))
        (error "Next line must have a valid CLOCK entry"))

    ;; check if lines should be merged
    ;; TBD

    ;; copy the end of time range
    (move-beginning-of-line nil)
    (re-search-forward "--")
    (setq timestamp-end (buffer-substring (point) (line-end-position)))
    ;; remove the first line
    (delete-region (line-beginning-position) (1+ (line-end-position)))
    ;; replace the end timestamp
    (re-search-forward "--")
    (delete-region (point) (line-end-position))
    (insert timestamp-end)
    ;; update timestamp to reflect new duration

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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