[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

M.3 Saving Files

Saving a buffer in Emacs means writing its contents back into the file that was visited in the buffer.

C-x C-s
Save the current buffer in its visited file on disk (save-buffer).
C-x s
Save any or all buffers in their visited files (save-some-buffers).
M-~
Forget that the current buffer has been changed (not-modified). With prefix argument (C-u), mark the current buffer as changed.
C-x C-w
Save the current buffer as a specified file name (write-file).
M-x set-visited-file-name
Change the file name under which the current buffer will be saved.

When you wish to save the file and make your changes permanent, type C-x C-s (save-buffer). After saving is finished, C-x C-s displays a message like this:

 
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), saving is not really done, because it would have no effect. Instead, C-x C-s displays a message like this in the echo area:

 
(No changes need to be saved)

The command C-x s (save-some-buffers) offers to save any or all modified buffers. It asks you what to do with each buffer. The possible responses are analogous to those of query-replace:

y
Save this buffer and ask about the rest of the buffers.
n
Don't save this buffer, but ask about the rest of the buffers.
!
Save this buffer and all the rest with no more questions.
RET
Terminate save-some-buffers without any more saving.
.
Save this buffer, then exit save-some-buffers without even asking about other buffers.
C-r
View the buffer that you are currently being asked about. When you exit View mode, you get back to save-some-buffers, which asks the question again.
C-h
Display a help message about these options.

C-x C-c, the key sequence to exit Emacs, invokes save-some-buffers and therefore asks the same questions.

If you have changed a buffer but you do not want to save the changes, you should take some action to prevent it. Otherwise, each time you use C-x s or C-x C-c, you are liable to save this buffer by mistake. One thing you can do is type M-~ (not-modified), which clears out 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 M-~ is `not', metafied.) You could also use set-visited-file-name (see below) to mark the buffer as visiting a different file name, one which is not in use for anything important. Alternatively, you can cancel all the changes made since the file was visited or saved, by reading the text from the file again. This is called reverting. See section M.4 Reverting a Buffer. You could also undo all the changes by repeating the undo command C-x u until you have undone all the changes; but reverting is easier.

M-x set-visited-file-name alters the name of the file that the current buffer is visiting. It reads the new file name using the minibuffer. Then it marks the buffer as visiting that file name, and changes the buffer name correspondingly. set-visited-file-name does not save the buffer in the newly visited file; it just alters the records inside Emacs in case you do save later. It also marks the buffer as "modified" so that C-x C-s in that buffer will save.

If you wish to mark the 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 (see section M.1 File Names).

If the new file name implies a major mode, then C-x C-w switches to that major mode, in most cases. The command set-visited-file-name also does this. See section R.1 How Major Modes are Chosen.

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 section Simultaneous Editing.

If the value of the variable require-final-newline is t, Emacs silently puts a newline at the end of any file that doesn't already end in one, every time a file is saved or written. If the value is nil, Emacs leaves the end of the file unchanged; if it's neither nil nor t, Emacs asks you whether to add a newline. The default is nil.

M.3.1 Backup Files  How Emacs saves the old version of your file.
M.3.2 Protection against Simultaneous Editing  How Emacs protects against simultaneous editing of one file by two users.
M.3.3 Shadowing Files  Copying files to "shadows" automatically.
M.3.4 Updating Time Stamps Automatically  Emacs can update time stamps on saved files.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

This document was generated on April 2, 2002 using texi2html