21

I regularly use org-mode and the agenda to keep track of my to-do list. Since I use Dropbox to sync my list, I need the same tasks to be available on all computers. Sometimes during the course of my cleanup I will forget to save my changes, leaving emacs open at home when I go into the office (thus I am not prompted to save on closing emacs). How can I automatically save changes to *.org agenda buffers that are modified through the agenda?

EDIT: To clarify, I use the agenda view of my tasks to show me an overview. From that view, I can change the status of tasks. Additionally, I use remember-mode to add new tasks, which can then be recategorized in the agenda view. These changes in the agenda result in modified org-mode agenda buffers, which must then be saved. When these changes are made, I would like the buffers to be saved automatically.

  • Can you clarify "How can I automatically save changes to *.org agenda buffers that are modified through the agenda?" ? I can't decide if it's "automatically save... through the agenda" (which seems to self-contradict) or "buffers that are modified through the agenda" (which I don't understand) – Sigma Sep 30 '14 at 1:44
  • @Sigma I've updated the question to clarify. – Ryan Sep 30 '14 at 2:07
  • 1
    My solution so far has been to re-train my fingers to tap the s key frequently when in Agenda view. – mankoff Sep 30 '14 at 2:35
  • @mankoff I think I've retrained myself to hit C-x C-s anytime I make any changes in an org-agenda-file (from agenda or directly). Not automatic but does keep everything saved. – Jonathan Leech-Pepin Sep 30 '14 at 15:27
16

A quick hack, which I'm not sure will satisfy your use-case would be

(add-hook 'org-agenda-mode-hook
          (lambda ()
            (add-hook 'auto-save-hook 'org-save-all-org-buffers nil t)
            (auto-save-mode)))

As long as the Org agenda buffer is open, all org buffers will be saved periodically (it equivalent to what would happen if s was pressed regularly from the agenda view).

This is somewhat abusing auto-save-mode in that the agenda buffer itself doesn't make much sense as far as auto-save is concerned.

If you happen to use that hack, you'd better make sure that backup files are kept for all your .org files, to be on the safe side.

  • A major problem with this approach is that auto-save will save the agenda buffer to a file in the active directory. This created a huge number of orphaned auto-save files scattered across my file system. Not sure how to remedy. – holocronweaver Apr 11 '17 at 22:27
7

You can save all org buffers whenever a particular agenda function is called. For instance, to save all org buffers after you quit the agenda:

(advice-add 'org-agenda-quit :before 'org-save-all-org-buffers)

Alternatively, you could save all org-buffers after each edit, say after a deadline is added:

(advice-add 'org-deadline :after 'org-save-all-org-buffers)

This will work both in org-agenda and org buffers. Use org-agenda-deadline instead if you want to restrict auto-saves to the agenda.

You can do the same for any org function, so this method allows you to choose exactly when to save org buffers. This approach covers some corner cases that the method by @Sigma misses: you can have your agenda changes saved even if you leave the agenda before auto-save has a chance to trigger, or make changes outside the agenda and forget to save them. I personally use both methods to cover all my bases. [Edit: See comment on @Sigma solution for why I no longer use his solution.]

  • 2
    This worked better for me than the accepted answer since the only time I forget to save is after I refile. (advice-add 'org-refile :after 'org-save-all-org-buffers) automatically saved the org files after I refiled to different files. – THIS USER NEEDS HELP Apr 13 '16 at 20:06
  • This is the best answer, as it can be applied to whichever org functions you want. – KevinG Jul 28 '17 at 16:47
  • I found that applying this to org-refile means that I can no longer "Go to" a certain headline (as opposed to refiling the one under the cursor) by calling org-refile with a prefix argument.. – quantum285 May 17 at 15:41
4

I use the following snippet to automatically save all the agenda mode buffers after a new capture, but you could hook it anywhere you'd like:

(defun my/save-all-agenda-buffers ()
  "Function used to save all agenda buffers that are
currently open, based on `org-agenda-files'."
  (interactive)
  (save-current-buffer
    (dolist (buffer (buffer-list t))
      (set-buffer buffer)
      (when (member (buffer-file-name)
                    (mapcar 'expand-file-name (org-agenda-files t)))
        (save-buffer)))))

;; save all the agenda files after each capture
(add-hook 'org-capture-after-finalize-hook 'my/save-all-agenda-buffers)

Change the 'org-capture-after-finalize-hook to 'org-agenda-finalize-hook, which I believe is called just before displaying the agenda buffer.

  • Forgot to mention my function requires dash.el to be available, so keep that in mind. – Lee H Sep 30 '14 at 8:28
  • 2
    During capturing, you can immediately re-file items to any of the org-refile-targets destinations, so it's possible that saving org-agenda-files is not sufficient. I'd probably just call org-save-all-org-buffers myself. – sanityinc Sep 30 '14 at 13:06
  • 1
    P.S. No need to use dash there: mapcar is built in and does the same thing. – sanityinc Sep 30 '14 at 13:06
  • @sanityinc thanks, I updated the code to use mapcar instead of dash! – Lee H Sep 30 '14 at 13:22
3

I see many answers that are more complicated, this worked for me:

`(add-hook 'auto-save-hook 'org-save-all-org-buffers)`

Auto Save defaults to running after 30 seconds of inactivity (and in other non-related scenarios documented in the manual)

  • Exactly what I was looking for. – LVB Jun 14 '18 at 19:52

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.