I would like to set a global hotkey in Emacs to perform a org-clock-in for a defined task in a defined file. This means that I have to open the file ~/org/main.org, then go to the beginning of the buffer, then find the first occurrence of "Some Task", then go to the beginning of the line and running org-clock-in and then save the file. At the same time I must remain in the current buffer at the cursor position. How can I do this?

  • Will this work if I write such a function? I think opening the file in the buffer will transfer me from my current buffer
    – Valeriy
    Apr 18, 2017 at 9:47
  • I've never seen examples of similar behavior. Can you show how to implement such things?
    – Valeriy
    Apr 18, 2017 at 9:55
  • Yes, I'm interesting how to write such a function.
    – Valeriy
    Apr 18, 2017 at 9:57
  • 1
    use save-excursion (see (eintr) save-excursion, (elisp) Excursions)
    – npostavs
    Apr 18, 2017 at 10:23

1 Answer 1


You should probably look at the documentation of orgmode agenda views before coding your own todo list. There is already a global TODO list available. See also the variable org-agenda-files.

Nevertheless, the following example code would do what you want. You can paste it into your init file.

(defvar valeriy-org-main-file "~/org/main.org"
  "My main org file.")

(defun valeriy-org-clock-in (task)
  "Clock in for TASK in `valeriy-org-main-file'."
  (interactive "sTask: ")
  (assert (file-readable-p valeriy-org-main-file) nil "File %s not existing." valeriy-org-main-file)
  (with-current-buffer (find-file-noselect valeriy-org-main-file)
      (let ((pos (org-find-exact-headline-in-buffer task)))
        (assert pos nil "Task %s not found in file %s." task valeriy-org-main-file)
        (goto-char pos)
        ;; maybe... (save-buffer)

(global-set-key (kbd "C-c i") #'valeriy-org-clock-in)
  • Wow! Thank you very much! =) I think it would be a good example for that, so I was able the next time to write something myself.
    – Valeriy
    Apr 18, 2017 at 11:31

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.