Writing to Files
You can write the contents of a buffer, or part of a buffer, directly to a file on disk using the append-to-file and write-region functions. Don’t use these functions to write to files that are being visited; that could cause confusion in the mechanisms for visiting.
- Command: append-to-file start end filename
-
This function appends the contents of the region delimited by start and end in the current buffer to the end of file filename. If that file does not exist, it is created. This function returns
nil.An error is signaled if you cannot write or create filename.
When called from Lisp, this function is completely equivalent to:
(write-region start end filename t)
- Command: write-region start end filename &optional append visit lockname mustbenew
-
This function writes the region delimited by start and end in the current buffer into the file specified by filename.
If start is
nil, then the command writes the entire buffer contents (not just the accessible portion) to the file and ignores end.If start is a string, then
write-regionwrites or appends that string, rather than text from the buffer. end is ignored in this case.If append is non-
nil, then the specified text is appended to the existing file contents (if any). If append is a number,write-regionseeks to that byte offset from the start of the file and writes the data from there.If mustbenew is non-
nil, thenwrite-regionasks for confirmation if filename names an existing file. If mustbenew is the symbolexcl, thenwrite-regiondoes not ask for confirmation, but instead it signals an errorfile-already-existsif the file already exists. Althoughwrite-regionnormally follows a symbolic link and creates the pointed-to file if the symbolic link is dangling, it does not follow symbolic links if mustbenew isexcl.The test for an existing file, when mustbenew is
excl, uses a special system feature. At least for files on a local disk, there is no chance that some other program could create a file of the same name before Emacs does, without Emacs’s noticing.If visit is
t, then Emacs establishes an association between the buffer and the file: the buffer is then visiting that file. It also sets the last file modification time for the current buffer to filename’s modtime, and marks the buffer as not modified. This feature is used bysave-buffer, but you probably should not use it yourself.If visit is a string, it specifies the file name to visit. This way, you can write the data to one file (filename) while recording the buffer as visiting another file (visit). The argument visit is used in the echo area message and also for file locking; visit is stored in
buffer-file-name. This feature is used to implementfile-precious-flag; don’t use it yourself unless you really know what you’re doing.The optional argument lockname, if non-
nil, specifies the file name to use for purposes of locking and unlocking, overriding filename and visit for that purpose.The function
write-regionconverts the data which it writes to the appropriate file formats specified bybuffer-file-formatand also calls the functions in the listwrite-region-annotate-functions. See Format Conversion.Normally,
write-regiondisplays the message ‘Wrote filename’ in the echo area. This message is inhibited if visit is neithertnornilnor a string, or if Emacs is operating in batch mode (see Batch Mode). This feature is useful for programs that use files for internal purposes, files that the user does not need to know about.
- Variable: write-region-inhibit-fsync
If this variable’s value is
nil,write-regionuses thefsyncsystem call after writing a file. Although this slows Emacs down, it lessens the risk of data loss after power failure. If the value ist, Emacs does not usefsync. The default value isnilwhen Emacs is interactive, andtwhen Emacs runs in batch mode. See Files and Storage.
- Macro: with-temp-file file body…
-
The
with-temp-filemacro evaluates the body forms with a temporary buffer as the current buffer; then, at the end, it writes the buffer contents into file file. It kills the temporary buffer when finished, restoring the buffer that was current before thewith-temp-fileform. Then it returns the value of the last form in body.The current buffer is restored even in case of an abnormal exit via
throwor error (see Nonlocal Exits).See also
with-temp-bufferin The Current Buffer.
Copyright © 1990-1996, 1998-2021 Free Software Foundation, Inc.
Licensed under the GNU GPL license.
https://www.gnu.org/software/emacs/manual/html_node/elisp/Writing-to-Files.html