18.3.3 Customizing Saving of Files
If the value of the variable require-final-newline
is t
, saving or writing a file silently puts a newline at the end if there isn’t already one there. If the value is visit
, Emacs adds a newline at the end of any file that doesn’t have one, just after it visits the file. (This marks the buffer as modified, and you can undo it.) If the value is visit-save
, Emacs adds such newlines both on visiting and on saving. If the value is nil
, Emacs leaves the end of the file unchanged; any other non-nil
value means Emacs asks you whether to add a newline. The default is nil
.
Some major modes are designed for specific kinds of files that are always supposed to end in newlines. Such major modes set the variable require-final-newline
to the value of mode-require-final-newline
, which defaults to t
. By setting the latter variable, you can control how these modes handle final newlines.
Normally, when a program writes a file, the operating system briefly caches the file’s data in main memory before committing the data to disk. This can greatly improve performance; for example, when running on laptops, it can avoid a disk spin-up each time a file is written. However, it risks data loss if the operating system crashes before committing the cache to disk.
To lessen this risk, Emacs can invoke the fsync
system call after saving a file. Using fsync
does not eliminate the risk of data loss, partly because many systems do not implement fsync
properly, and partly because Emacs’s file-saving procedure typically relies also on directory updates that might not survive a crash even if fsync
works properly.
The write-region-inhibit-fsync
variable controls whether Emacs invokes fsync
after saving a file. The variable’s default value is nil
when Emacs is interactive, and t
when Emacs runs in batch mode (see Batch Mode).
Emacs never uses fsync
when writing auto-save files, as these files might lose data anyway.