(emacs)Saving


Next: Reverting Prev: Visiting Up: Files

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 (`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').

`C-x C-w'
     Save the current buffer in a specified file (`write-file').

`M-x set-visited-file-name'
     Change file the 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' 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), saving is not really done,
because it would have no effect.  Instead, `C-x C-s' prints a message
in the echo area saying

     (No changes need to be written)

   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
options 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.

`ESC'
     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 and do not want the changes to be saved,
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 it 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".  Note: Reverting.
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 specifies the visited file name and changes the
buffer name correspondingly (as long as the new name is not in use).
`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 with 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.
Note: 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.

   You can implement other ways to write files, and other things to be
done before writing them, using the variable `write-file-hooks'.  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.  If this variable is local to a buffer, changing major
modes does not clear it.  However, changing the visited file name does
clear this variable to `nil'.

   The list `local-write-file-hooks' is used just like
`write-file-hooks'.  This list is for use as a local variable.  It is a
permanent local, so that switching major modes does not affect it.

   The variable `write-contents-hooks' holds another list of functions
to be called before writing out a buffer to a file.  These functions
are used just like the ones in `write-file-hooks'.  The difference
between this and `write-file-hooks' is that changing the major mode
does clear this variable, and setting the visited file name does not
clear it.

   The hook functions are responsible for writing backup files, if you
want that to be done.  Here is how:

     (or buffer-backed-up (backup-buffer))

You might also wish to save the file modes value returned by
`backup-buffer' and use that to set the mode bits of the file that you
write.  This is what happens ordinarily when backups are made.

   The hook variables described here are not normal hooks, because the
values returned by the hook functions do matter.  This is why they have
names ending in `-hooks' rather than `-hook'.  Note: Hooks.

* Backup
How Emacs saves the old version of your file.
* Interlocking
How Emacs protects against simultaneous editing of one file by two users.

automatically generated by info2www