I found myself constantly forgetting to clock in tasks using Emacs Org-mode. You know how it is—you’re in the zone, tackling your to-do list, and the last thing you want is to break your flow to manually clock in a task. So, I decided to automate the process. Here’s how I set up a handy hook to automatically clock in a task as soon as it enters the “DOING” state, to make time tracking seamless.

The Code

Here’s the snippet of code that does the trick:

(defun my/org-clock-in-when-doing ()
  "Clock in when the TODO state is switched to DOING."
  (when (string= org-state "DOING")
    (unless (org-clocking-p)
      (org-clock-in))))
 
(add-hook 'org-after-todo-state-change-hook #'my/org-clock-in-when-doing)

Breaking Down the Code

  • Function Definition: The my/org-clock-in-when-doing function is the heart of this automation. It checks if the task state is “DOING”.
  • Conditional Check: It uses a when statement to verify the task’s state. If the condition is met, it proceeds to the next step.
  • Clock-in Command: The org-clock-in function is called only if the task is not already clocked in, ensuring no redundant actions.
  • Hook Addition: By adding my/org-clock-in-when-doing to the org-after-todo-state-change-hook, the function is triggered every time a task state changes.

Clocking Out Automatically

To complement this setup, I also configured Emacs to clock out automatically when a task is marked as done. This ensures that my time logs are accurate and complete. You can achieve this by setting the following property:

(setq org-clock-out-when-done t)

Final Thoughts

This small tweak has significantly improved my productivity. If you’re an Emacs user looking to streamline your workflow, I highly recommend giving this a try. Not only does it save time, but it also frees up mental space for more important tasks.