Function: write-region

write-region is an interactive function defined in fileio.c.

Signature

(write-region START END FILENAME &optional APPEND VISIT LOCKNAME MUSTBENEW)

Documentation

Write current region into specified file.

When called from a program, requires three arguments: START, END and FILENAME. START and END are normally buffer positions specifying the part of the buffer to write. If START is nil, that means to use the entire buffer contents; END is ignored. If START is a string, then output that string to the file instead of any buffer contents; END is ignored.

Optional fourth argument APPEND if non-nil means
  append to existing file contents (if any). If it is a number,
  seek to that offset in the file before writing.
Optional fifth argument VISIT, if t or a string, means
  set the last-save-file-modtime of buffer to this file's modtime
  and mark buffer not modified.
If VISIT is t, the buffer is marked as visiting FILENAME. If VISIT is a string, it is a second file name;
  the output goes to FILENAME, but the buffer is marked as visiting VISIT.
  VISIT is also the file name to lock and unlock for clash detection.
If VISIT is neither t nor nil nor a string, or if Emacs is in batch mode,
  do not display the "Wrote file" message.
The optional sixth arg LOCKNAME, if non-nil, specifies the name to
  use for locking and unlocking, overriding FILENAME and VISIT.
The optional seventh arg MUSTBENEW, if non-nil, insists on a check
  for an existing file with the same name. If MUSTBENEW is excl,
  that means to get an error if the file already exists; never overwrite.
  If MUSTBENEW is neither nil nor excl, that means ask for
  confirmation before overwriting, but do go ahead and overwrite the file
  if the user confirms.

This does code conversion according to the value of coding-system-for-write, buffer-file-coding-system, or file-coding-system-alist, and sets the variable last-coding-system-used to the coding system actually used.

This calls write-region-annotate-functions at the start, and write-region-post-annotation-function at the end.

Other relevant functions are documented in the file group.

View in manual

Probably introduced at or before Emacs version 19.20.

Key Bindings

Shortdoc

;; file
(write-region (point-min) (point-max) "/tmp/foo")

Source Code

// Defined in /usr/src/emacs/src/fileio.c
{
  return write_region (start, end, filename, append, visit, lockname, mustbenew,
		       -1);
}