5

When typing ls in my home directory, I amm greeted by a lot of this:

#%2Ascratch%2A#1399_po# #%2Ascratch%2A#14484YsZ# [...]

I also have the following in my init.el

(defvar autosave-location (concat user-emacs-directory "data/autosave"))

(setq auto-save-file-name-transforms
  `((".*" ,autosave-location t)))

Why, then are the scratch autosaves not being sent to ~/.emacs.d/data/autosave and instead are being blasted around my file system? Ideally, I just want it to save to that folder, however if that can't be done, disabling autosave for *scratch* buffers entirely would suffice.

I believe this started with Emacs 24.4.

PythonNut
  • 10,243
  • 2
  • 29
  • 75
  • In your setup, is the `*scratch*` a file-visiting-buffer or a NON-file-visiting buffer? The default is the latter and thus is not subject to autosave. I like to kill the `*scratch*` when Emacs opens and prevent it from reappearing, and instead I use a custom file-visiting-buffer called `.scratch`. Whenever `*scratch*` would normally appear, I preempt it with a custom `find-file /path/to/.scratch` – lawlist Jan 26 '15 at 19:21
  • It is _not_ a file-visiting buffer (and I would rather it stay that way), which is why I'm confused. – PythonNut Jan 26 '15 at 19:24
  • Emacs does not, by default, autosave NON-file-visiting buffers. For example, a `*Help*` buffer does not get autosaved, nor does a `*Backtrace*` buffer. – lawlist Jan 26 '15 at 19:25
  • Apparently, it will if you enable `auto-save-mode` in the buffer. – PythonNut Jan 26 '15 at 19:27
  • See also http://stackoverflow.com/questions/8849661/is-it-possible-to-autosave-temporary-buffers-that-are-not-visiting-a-file – phils Jan 26 '15 at 21:17
  • @lawlist I would like to do something similar to what you describe with `.scratch`. Are you able to provide details? It would be a huge help – JonahHuron Jun 28 '23 at 19:28
  • @JonahHuron -- Please feel free to write up a new question once you have conducted a preliminary search to ascertain whether a similar/same quesition/answer already exists. If you find similar questions/answers that do not answer your question, then perhaps mention those links in your new question and state why those other questions/answers are insufficient for your particular use-case; i.e., distinguish them so that your new question is not erroneously closed as being duplicative. – lawlist Jun 28 '23 at 20:24

1 Answers1

5

You'll notice that you're using a variable called auto-save-file-name-transforms. Since *scratch* has no filename, it falls back to autosaving in default-directory.

For the *scratch* buffer, or any other buffers like this with no filename, you can use:

(setq-local default-directory "~/.emacs.d/data/autosave")

Edit: I see that you don't really care about autosaving scratch buffers. Can I ask how you enable auto-save-mode in your init? You should use:

 (setq-default auto-save-default t)

And not something like a hook with (auto-save-mode 1), because the variable only enables it for file-visiting buffers, while a hook could potentially enable it for buffers where you don't want it.

nanny
  • 5,704
  • 18
  • 38