7

I often do something like:

C-x b foobarbaz RET

to get a new buffer for taking notes, etc. I'd love for emacs to just snapshot and dump all open buffers to disk occasionally. There's lots of documentation out there for improving auto-save and backup functionality but those all relate to buffers that are currently visiting a file.

I'd like emacs to periodically snapshot/save ALL buffers to disk. This will surely cause some noisey 'output' buffers to get written to disk, but who cares? Disk space is cheap.

Is there anything built in to do this? Right now if my computer (or emacs) crashes, I often lose lots of notes that I've taken in various buffers.

4
  • save-some-buffers has an option for non-file-visiting buffers -- perhaps you can marry that with a timer and a custom function, which save-some-buffers accepts as an argument -- see the doc-string. There is probably already something that does this -- e.g., Google "save-some-buffers" auto save etc.; and, see the function auto-save-mode in simple.el for some ideas. My recollection is that auto-save-buffers-enhanced may have done something like this: github.com/kentaro/auto-save-buffers-enhanced/blob/master/…
    – lawlist
    Commented Jul 20, 2015 at 16:06
  • What OS are you on and what's your emacs version? I am on RHEL 5.10 and emacs 25 (dev version from git) and I don't recall emacs crashing once, probably ever since 24.4.. Commented Jul 21, 2015 at 11:26
  • 1
    For taking quick notes that auto-save, I would suggest the deft package. Commented Jul 21, 2015 at 11:27
  • Will check out deft -- looks great. Commented Aug 7, 2015 at 18:20

1 Answer 1

8

This does not answer your question as posed, so feel free to ignore or downvote.

  1. I doubt that you really want Emacs to save all buffers to disk, including all of the internal, normally invisible buffers. Why would you want that?

  2. It's not about the disk space. It's about the noise. Do you really want to dig stuff out of a directory where your important files might be mixed in with such stuff? Or do you want to fish for the occasional pearl in a temp directory (whose purpose is to stash noise)?

  3. Buffers that have names like *...* (not starting with a space char) are typically output-only (e.g., *info*) or for temporary interaction (e.g. *scratch*). For such buffers, my recommendation would be to leave things as they are. You can always save any such buffer on demand, using C-x C-w, if you really need to.

  4. Which brings me to what I suspect is your real use case: "buffer[s] for taking notes, etc.". The answer here is to use C-x C-f instead of C-x b. IOW, yes, visit them as file buffers. If you never save the buffer there is no file created - except an autosave file, which is probably just what you want and need.

    When you visit a buffer as a file buffer, you automatically control which major mode is used by default (using auto-mode-alist). This is just as important for note-taking as it is for programming code.

    In particular, I would suggest that if you use *scratch* for temporary Lisp interaction, you might reconsider. Many users (including me) have found that it helps to instead use C-x C-f foo.el (or whatever other *.el file name). One reason is that you get autosave files for free (and backup files, if you save the buffer). Another reason is that you get Emacs-Lisp mode instead of Lisp interaction mode. A third reason is that yes, you might well want to save your "temporary" work. And as you say, disk space is cheap.

What about #4 conflicting with what I said in #1? You don't want to mix files of temporary scribbles with important files, and you don't want to fish the occasional pearl out of a directory of temp files. The answer here is that it's up to you to organize things so that this effect is minimized. Working with such scratch files in a particular directory can help, as can using a naming convention (esp. a prefix) that helps you distinguish such files.

Another thing that can help: Dired. You can use ~ and # to mark backup and autosave files for deletion (then x to delete them). And if you use a naming convention for your presumably temporary, scratch files then you can use % or similar to mark all of them for deletion. You can even execute a function periodically to clean out such files. And you can easily rename any that you really want to keep longer.

So my advice is to perhaps reconsider how you're interacting with such temporary, note-taking buffers. Consider using file buffers, which get autosaved. In sum, use C-x C-f foobarbaz.txt RET (or .el or whatever) instead of C-x b foobarbaz RET.

3
  • 1
    I guess you meant "In sum, use C-x C-f" instead of "In sum, use C-x b" in the last paragraph. (I can't suggest an edit directly because this change is less than 6 characters.)
    – YoungFrog
    Commented Jul 20, 2015 at 17:57
  • 1
    Over the past couple of weeks I've tried to get in the habit of C-x C-f instead, and it's been working out pretty well. Combined with better use of bookmarks and Helm, I'm able to get to a notes directory and create a new scratch file very quickly. I got in the original C-x b <gibberish> habit via Yegge's effective emacs tip #5 but if there is any chance of the buffer sticking around, I think you're right to get in the habit of using a proper file. Thanks! Commented Aug 7, 2015 at 18:20
  • 1
    Glad it helped.
    – Drew
    Commented Aug 8, 2015 at 20:15

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.