Function: save-buffer

save-buffer is an interactive and byte-compiled function defined in files.el.gz.

Signature

(save-buffer &optional ARG)

Documentation

Save current buffer in visited file if modified.

Variations are described below.

By default, makes the previous version into a backup file
 if previously requested or if this is the first save.
Prefixed with one C-u (universal-argument), marks this version
 to become a backup when the next save is done.
Prefixed with two C-u (universal-argument)'s,
 makes the previous version into a backup file.
Prefixed with three C-u (universal-argument)'s, marks this version
 to become a backup when the next save is done,
 and makes the previous version into a backup file.

With a numeric prefix argument of 0, never make the previous version into a backup file.

Note that the various variables that control backups, such as version-control, backup-enable-predicate, vc-make-backup-files, and backup-inhibited, to name just the more popular ones, still control whether a backup will actually be produced, even when you invoke this command prefixed with two or three C-u (universal-argument)'s.

If a file's name is FOO, the names of its numbered backup versions are
 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~.
Numeric backups (rather than FOO~) will be made if value of
 version-control is not the atom never and either there are already
 numeric versions of the file being backed up, or version-control is
 non-nil.
We don't want excessive versions piling up, so there are variables
 kept-old-versions, which tells Emacs how many oldest versions to keep,
 and kept-new-versions, which tells how many newest versions to keep.
 Defaults are 2 old versions and 2 new.
dired-kept-versions controls dired's clean-directory (.) command. If delete-old-versions is nil, system will query user
 before trimming versions. Otherwise it does it silently.

If vc-make-backup-files is nil, which is the default,
 no backup files are made for files managed by version control.
 (This is because the version control system itself records previous versions.)

See the subroutine basic-save-buffer for more information.

Probably introduced at or before Emacs version 26.1.

Key Bindings

Source Code

;; Defined in /usr/src/emacs/lisp/files.el.gz
(defun save-buffer (&optional arg)
  "Save current buffer in visited file if modified.
Variations are described below.

By default, makes the previous version into a backup file
 if previously requested or if this is the first save.
Prefixed with one \\[universal-argument], marks this version
 to become a backup when the next save is done.
Prefixed with two \\[universal-argument]'s,
 makes the previous version into a backup file.
Prefixed with three \\[universal-argument]'s, marks this version
 to become a backup when the next save is done,
 and makes the previous version into a backup file.

With a numeric prefix argument of 0, never make the previous version
into a backup file.

Note that the various variables that control backups, such
as `version-control', `backup-enable-predicate', `vc-make-backup-files',
and `backup-inhibited', to name just the more popular ones, still
control whether a backup will actually be produced, even when you
invoke this command prefixed with two or three \\[universal-argument]'s.

If a file's name is FOO, the names of its numbered backup versions are
 FOO.~i~ for various integers i.  A non-numbered backup file is called FOO~.
Numeric backups (rather than FOO~) will be made if value of
 `version-control' is not the atom `never' and either there are already
 numeric versions of the file being backed up, or `version-control' is
 non-nil.
We don't want excessive versions piling up, so there are variables
 `kept-old-versions', which tells Emacs how many oldest versions to keep,
 and `kept-new-versions', which tells how many newest versions to keep.
 Defaults are 2 old versions and 2 new.
`dired-kept-versions' controls dired's clean-directory (.) command.
If `delete-old-versions' is nil, system will query user
 before trimming versions.  Otherwise it does it silently.

If `vc-make-backup-files' is nil, which is the default,
 no backup files are made for files managed by version control.
 (This is because the version control system itself records previous versions.)

See the subroutine `basic-save-buffer' for more information."
  (interactive "p")
  (let ((modp (buffer-modified-p))
	(make-backup-files (or (and make-backup-files (not (eq arg 0)))
			       (memq arg '(16 64)))))
    (and modp (memq arg '(16 64)) (setq buffer-backed-up nil))
    ;; We used to display the message below only for files > 50KB, but
    ;; then Rmail-mbox never displays it due to buffer swapping.  If
    ;; the test is ever re-introduced, be sure to handle saving of
    ;; Rmail files.
    (if (and modp
             (buffer-file-name)
             (not noninteractive)
             (not save-silently))
	(message "Saving file %s..." (buffer-file-name)))
    (basic-save-buffer (called-interactively-p 'any))
    (and modp (memq arg '(4 64)) (setq buffer-backed-up nil))))