33

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.

Malabarba
  • 22,878
  • 6
  • 78
  • 163
Ryan
  • 3,989
  • 1
  • 26
  • 49
  • 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 02: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

5 Answers5

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.

Sigma
  • 4,510
  • 21
  • 27
  • 1
    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
13

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)

Karim Nassar
  • 141
  • 1
  • 2
8

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.]

holocronweaver
  • 1,319
  • 10
  • 22
  • 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
  • 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 '19 at 15:41
  • Just tried this with org-deadline, and it works fine in an agenda buffer. But then when I try to set a deadline in an org-capture buffer, it breaks with a "wrong number of arguments" error. – Vultan May 11 '22 at 15:32
5

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.

Lee H
  • 2,697
  • 1
  • 16
  • 31
  • 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
0

This will work whether you are currently in the Agenda buffer or not:

     (defun my-org-mode-autosave-settings ()
       (add-hook 'auto-save-hook 'org-save-all-org-buffers nil nil))
     (add-hook 'org-mode-hook 'my-org-mode-autosave-settings)

This is based on Karim's and Sigma's answers.

Sami
  • 21
  • 2