It's a very common case to persist data structure using `pp` or `prin1`. However, the core function of saving a buffer, `write-region` is not atomic. If your data structure is very very large, and something happened(e.g. you sent SIGKILL to Emacs, or the power of your computer was cut) during saving, it will probably result in a broken file. File system should provide some atomic operation to deal with these conditions. There's a "trick" to do this job -- write to a temp file in same directory, then use libc's rename to override origin file. Because rename is atomic in same mount point. Our file can be replaced atomically. I'd like to suggest a implementation draft here. If you think this is good, I can help make a patch to add it to files.el ~~~ (defmacro with-atomic-file-replacement (file &rest body) "Open FILE in a temp buffer and evaluate BODY in it, replace the FILE with the buffer content atomically" (declare (debug t) (indent 1)) `(call-with-atomic-file-replacement ,file (lambda () ,@body))) (defun call-with-atomic-file-replacement (file func) ;; The atomic of rename in libc was guaranteed by POSIX ;; standard, it will fail if we rename file between two mount point. So create ;; temp file in the same dir of FILE. (let* ((full (expand-file-name file)) (dir (file-name-directory full)) (tmpfile (make-temp-file dir))) (with-temp-buffer (setq buffer-file-name tmpfile) (insert-file-contents-literally full) (funcall func) (basic-save-buffer) (rename-file tmpfile full t))))