What is the problem if I keep writing a file while editing it instead of saving it?
What exactly am I going to lose? Some sort of backup? Is this a "bad" practice?
Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. It only takes a minute to sign up.
Sign up to join this communityThe source for write-file is available.
As of May 2021, it does some checks (e.g., to make sure the location being saved to isn't a directory, see if a file is being overwritten), then calls save-buffer.
So you're not missing anything by calling write-file
; it calls the same function you would call to save the buffer. I'm not exactly sure why you would want to do this, as it seems like you'd have to specify the filename each time you want to save. But you can.
To make this a self-contained answer, the complete source is here:
(defun write-file (filename &optional confirm)
"Write current buffer into file FILENAME.
This makes the buffer visit that file, and marks it as not modified.
Interactively, prompt for FILENAME.
If you specify just a directory name as FILENAME, that means to write
to a file in that directory. In this case, the base name of the file
is the same as that of the file visited in the buffer, or the buffer
name sans leading directories, if any, if the buffer is not already
visiting a file.
You can also yank the file name into the minibuffer to edit it,
using \\<minibuffer-local-map>\\[next-history-element].
If optional second arg CONFIRM is non-nil, this function
asks for confirmation before overwriting an existing file.
Interactively, confirmation is required unless you supply a prefix argument."
;; (interactive "FWrite file: ")
(interactive
(list (if buffer-file-name
(read-file-name "Write file: "
nil nil nil nil)
(read-file-name "Write file: " default-directory
(expand-file-name
(file-name-nondirectory (buffer-name))
default-directory)
nil nil))
(not current-prefix-arg)))
(or (null filename) (string-equal filename "")
(progn
;; If arg is a directory name,
;; use the default file name, but in that directory.
(if (directory-name-p filename)
(setq filename (concat filename
(file-name-nondirectory
(or buffer-file-name (buffer-name))))))
(and confirm
(file-exists-p filename)
;; NS does its own confirm dialog.
(not (and (eq (framep-on-display) 'ns)
(listp last-nonmenu-event)
use-dialog-box))
(or (y-or-n-p (format-message
"File `%s' exists; overwrite? " filename))
(user-error "Canceled")))
(set-visited-file-name filename (not confirm))))
(set-buffer-modified-p t)
;; Make buffer writable if file is writable.
(and buffer-file-name
(file-writable-p buffer-file-name)
(setq buffer-read-only nil))
(save-buffer)
;; It's likely that the VC status at the new location is different from
;; the one at the old location.
(vc-refresh-state))
write-file
etc. are general - they ask for the file to write the buffer contents to. Save commands write the current buffer to its file. That's the only difference I can think of. But someone else might remember other differences.