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?
1 Answer
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)
(save-excursion
(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)
(org-clock-in)
;; 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.– ValeriyApr 18, 2017 at 11:31
save-excursion
(see (eintr) save-excursion, (elisp) Excursions)