Saving a buffer in Emacs means writing its contents back into the file that was visited in the buffer.
Save the current buffer in its visited file (save-buffer
).
Save any or all buffers in their visited files (save-some-buffers
).
Forget that the current buffer has been changed (not-modified
).
Save the current buffer in a specified file, and record that file as
the one visited in the buffer (write-file
).
Change file the name under which the current buffer will be saved.
To save a file and make your changes permanent, type
C-x C-s (save-buffer
). After saving is finished, C-x C-s
prints a message such as:
Wrote /u/rms/gnu/gnu.tasks
If the selected buffer is not modified (no changes have been made in it since the buffer was created or last saved), Emacs does not save it because it would have no effect. Instead, C-x C-s prints a message in the echo area saying:
(No changes need to be saved)
The command C-x s (save-some-buffers
) can save any or all
modified buffers. First it asks, for each modified buffer, whether to
save it. The questions should be answered with y or n.
C-x C-c, the key that kills Emacs, invokes
save-some-buffers
and therefore asks the same questions.
If you have changed a buffer and do not want the changes to be saved,
you should take some action to prevent it. Otherwise, you are liable to
save it by mistake each time you use save-some-buffers
or a
related command. One thing you can do is type M-~
(not-modified
), which removes the indication that the buffer
is modified. If you do this, none of the save commands will believe
that the buffer needs to be saved. (‘~’ is often used as a
mathematical symbol for ‘not’; thus Meta-~ is ‘not’, metafied.)
You could also use set-visited-file-name
(see below) to mark the
buffer as visiting a different file name, not in use for
anything important.
You can also undo all the changes made since the file was visited or saved, by reading the text from the file again. This is called reverting. See Reverting. Alternatively, you can undo all the changes by repeating the undo command C-x u; but this only works if you have not made more changes than the undo mechanism can remember.
M-x set-visited-file-name alters the name of the file that the
current buffer is visiting. It prompts you for the new file name in the
minibuffer. You can also use set-visited-file-name
on a buffer
that is not visiting a file. The buffer’s name is changed to correspond
to the file it is now visiting unless the new name is already used by a
different buffer; in that case, the buffer name is not changed.
set-visited-file-name
does not save the buffer in the newly
visited file; it just alters the records inside Emacs so that it will
save the buffer in that file. It also marks the buffer as “modified”
so that C-x C-s will save.
If you wish to mark a buffer as visiting a different file and save it
right away, use C-x C-w (write-file
). It is precisely
equivalent to set-visited-file-name
followed by C-x C-s.
C-x C-s used on a buffer that is not visiting a file has the
same effect as C-x C-w; that is, it reads a file name, marks the
buffer as visiting that file, and saves it there. The default file name in
a buffer that is not visiting a file is made by combining the buffer name
with the buffer’s default directory.
If Emacs is about to save a file and sees that the date of the latest version on disk does not match what Emacs last read or wrote, Emacs notifies you of this fact, because it probably indicates a problem caused by simultaneous editing and requires your immediate attention. See Simultaneous Editing.
If the variable require-final-newline
is non-nil
, Emacs
puts a newline at the end of any file that doesn’t already end in one,
every time a file is saved or written.
Use the hook variable write-file-hooks
to implement other ways
to write files, and specify things to be done before files are written. The
value of this variable should be a list of Lisp functions. When a file
is to be written, the functions in the list are called, one by one, with
no arguments. If one of them returns a non-nil
value, Emacs
takes this to mean that the file has been written in some suitable
fashion; the rest of the functions are not called, and normal writing is
not done. Use the hook variable after-save-hook
to list
all the functions to be called after writing out a buffer to a file.
• Backup: | How Emacs saves the old version of your file. | |
• Interlocking: | How Emacs protects against simultaneous editing of one file by two users. |