files.texi 122 KB
Newer Older
Dave Love's avatar
#  
Dave Love committed
1
@c This is part of the Emacs manual.
Lute Kamstra's avatar
Lute Kamstra committed
2
@c Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 1999, 2000,
Glenn Morris's avatar
Glenn Morris committed
3
@c   2001, 2002, 2003, 2004, 2005, 2006, 2007 Free Software Foundation, Inc.
Dave Love's avatar
#  
Dave Love committed
4
@c See file emacs.texi for copying conditions.
5
@node Files, Buffers, Keyboard Macros, Top
Dave Love's avatar
#  
Dave Love committed
6 7 8
@chapter File Handling
@cindex files

9
  The operating system stores data permanently in named @dfn{files}, so
Dave Love's avatar
#  
Dave Love committed
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
most of the text you edit with Emacs comes from a file and is ultimately
stored in a file.

  To edit a file, you must tell Emacs to read the file and prepare a
buffer containing a copy of the file's text.  This is called
@dfn{visiting} the file.  Editing commands apply directly to text in the
buffer; that is, to the copy inside Emacs.  Your changes appear in the
file itself only when you @dfn{save} the buffer back into the file.

  In addition to visiting and saving files, Emacs can delete, copy,
rename, and append to files, keep multiple versions of them, and operate
on file directories.

@menu
* File Names::          How to type and edit file-name arguments.
* Visiting::            Visiting a file prepares Emacs to edit the file.
* Saving::              Saving makes your changes permanent.
* Reverting::           Reverting cancels all the changes not saved.
28 29 30
@ifnottex
* Autorevert::          Auto Reverting non-file buffers.
@end ifnottex
Dave Love's avatar
#  
Dave Love committed
31 32 33 34 35
* Auto Save::           Auto Save periodically protects against loss of data.
* File Aliases::        Handling multiple names for one file.
* Version Control::     Version control systems (RCS, CVS and SCCS).
* Directories::         Creating, deleting, and listing file directories.
* Comparing Files::     Finding where two files differ.
36
* Diff Mode::           Mode for editing file differences.
Dave Love's avatar
#  
Dave Love committed
37 38
* Misc File Ops::       Other things you can do on files.
* Compressed Files::    Accessing compressed files.
39
* File Archives::       Operating on tar, zip, jar etc. archive files.
Dave Love's avatar
#  
Dave Love committed
40 41
* Remote Files::        Accessing files on other sites.
* Quoted File Names::   Quoting special characters in file names.
42
* File Name Cache::     Completion against a list of files you often use.
Dave Love's avatar
Dave Love committed
43
* File Conveniences::   Convenience Features for Finding Files.
Richard M. Stallman's avatar
Richard M. Stallman committed
44
* Filesets::            Handling sets of files.
Dave Love's avatar
#  
Dave Love committed
45 46 47 48 49 50 51 52 53
@end menu

@node File Names
@section File Names
@cindex file names

  Most Emacs commands that operate on a file require you to specify the
file name.  (Saving and reverting are exceptions; the buffer knows which
file name to use for them.)  You enter the file name using the
Richard M. Stallman's avatar
Richard M. Stallman committed
54 55
minibuffer (@pxref{Minibuffer}).  @dfn{Completion} is available
(@pxref{Completion}) to make it easier to specify long file names.  When
56
completing file names, Emacs ignores those whose file-name extensions
Richard M. Stallman's avatar
Richard M. Stallman committed
57
appear in the variable @code{completion-ignored-extensions}; see
58
@ref{Completion Options}.
Dave Love's avatar
#  
Dave Love committed
59 60 61 62 63 64 65 66

  For most operations, there is a @dfn{default file name} which is used
if you type just @key{RET} to enter an empty argument.  Normally the
default file name is the name of the file visited in the current buffer;
this makes it easy to operate on that file with any of the Emacs file
commands.

@vindex default-directory
67
  Each buffer has a default directory which is normally the same as the
Dave Love's avatar
#  
Dave Love committed
68 69 70 71 72 73 74 75 76
directory of the file visited in that buffer.  When you enter a file
name without a directory, the default directory is used.  If you specify
a directory in a relative fashion, with a name that does not start with
a slash, it is interpreted with respect to the default directory.  The
default directory is kept in the variable @code{default-directory},
which has a separate value in every buffer.

@findex cd
@findex pwd
77
  The command @kbd{M-x pwd} displays the current buffer's default
Dave Love's avatar
#  
Dave Love committed
78 79 80
directory, and the command @kbd{M-x cd} sets it (to a value read using
the minibuffer).  A buffer's default directory changes only when the
@code{cd} command is used.  A file-visiting buffer's default directory
Richard M. Stallman's avatar
Richard M. Stallman committed
81 82 83 84 85 86 87 88 89 90
is initialized to the directory of the file it visits.  If you create
a buffer with @kbd{C-x b}, its default directory is copied from that
of the buffer that was current at the time.

  For example, if the default file name is @file{/u/rms/gnu/gnu.tasks}
then the default directory is normally @file{/u/rms/gnu/}.  If you
type just @samp{foo}, which does not specify a directory, it is short
for @file{/u/rms/gnu/foo}.  @samp{../.login} would stand for
@file{/u/rms/.login}.  @samp{new/foo} would stand for the file name
@file{/u/rms/gnu/new/foo}.
Dave Love's avatar
#  
Dave Love committed
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109

@vindex insert-default-directory
  The default directory actually appears in the minibuffer when the
minibuffer becomes active to read a file name.  This serves two
purposes: it @emph{shows} you what the default is, so that you can type
a relative file name and know with certainty what it will mean, and it
allows you to @emph{edit} the default to specify a different directory.
This insertion of the default directory is inhibited if the variable
@code{insert-default-directory} is set to @code{nil}.

  Note that it is legitimate to type an absolute file name after you
enter the minibuffer, ignoring the presence of the default directory
name as part of the text.  The final minibuffer contents may look
invalid, but that is not so.  For example, if the minibuffer starts out
with @samp{/usr/tmp/} and you add @samp{/x1/rms/foo}, you get
@samp{/usr/tmp//x1/rms/foo}; but Emacs ignores everything through the
first slash in the double slash; the result is @samp{/x1/rms/foo}.
@xref{Minibuffer File}.

Richard M. Stallman's avatar
Richard M. Stallman committed
110 111 112
@cindex home directory shorthand
  You can use @file{~/} in a file name to mean your home directory,
or @file{~@var{user-id}/} to mean the home directory of a user whose
113 114
login name is @code{user-id}@footnote{
On MS-Windows and MS-DOS systems, where a user doesn't have a home
Karl Berry's avatar
Karl Berry committed
115 116 117
directory, Emacs replaces @file{~/} with the value of the
environment variable @code{HOME}; see @ref{General Variables}.  On
these systems, the @file{~@var{user-id}/} construct is supported only
118 119
for the current user, i.e., only if @var{user-id} is the current
user's login name.}.
Richard M. Stallman's avatar
Richard M. Stallman committed
120

121 122
@cindex environment variables in file names
@cindex expansion of environment variables
123
@cindex @code{$} in file names
124 125 126 127 128
  @anchor{File Names with $}@samp{$} in a file name is used to
substitute an environment variable.  The environment variable name
consists of all the alphanumeric characters after the @samp{$};
alternatively, it can be enclosed in braces after the @samp{$}.  For
example, if you have used the shell command @command{export
Gerd Moellmann's avatar
Gerd Moellmann committed
129
FOO=rms/hacks} to set up an environment variable named @env{FOO}, then
Dave Love's avatar
#  
Dave Love committed
130
you can use @file{/u/$FOO/test.c} or @file{/u/$@{FOO@}/test.c} as an
131 132 133 134 135 136 137
abbreviation for @file{/u/rms/hacks/test.c}.  If the environment
variable is not defined, no substitution occurs: @file{/u/$notdefined}
stands for itself (assuming the environment variable @env{notdefined}
is not defined).

  Note that shell commands to set environment variables affect Emacs
only when done before Emacs is started.
Dave Love's avatar
#  
Dave Love committed
138

139 140 141 142 143 144
  To access a file with @samp{$} in its name, if the @samp{$} causes
expansion, type @samp{$$}.  This pair is converted to a single
@samp{$} at the same time as variable substitution is performed for a
single @samp{$}.  Alternatively, quote the whole file name with
@samp{/:} (@pxref{Quoted File Names}).  File names which begin with a
literal @samp{~} should also be quoted with @samp{/:}.
Dave Love's avatar
#  
Dave Love committed
145 146

@findex substitute-in-file-name
Richard M. Stallman's avatar
Richard M. Stallman committed
147
  The Lisp function that performs the @samp{$}-substitution is called
Dave Love's avatar
#  
Dave Love committed
148 149 150
@code{substitute-in-file-name}.  The substitution is performed only on
file names read as such using the minibuffer.

151
  You can include non-@acronym{ASCII} characters in file names if you set the
Dave Love's avatar
#  
Dave Love committed
152
variable @code{file-name-coding-system} to a non-@code{nil} value.
Luc Teirlinck's avatar
Luc Teirlinck committed
153
@xref{File Name Coding}.
Dave Love's avatar
#  
Dave Love committed
154 155 156 157

@node Visiting
@section Visiting Files
@cindex visiting files
158
@cindex open file
Dave Love's avatar
#  
Dave Love committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

@table @kbd
@item C-x C-f
Visit a file (@code{find-file}).
@item C-x C-r
Visit a file for viewing, without allowing changes to it
(@code{find-file-read-only}).
@item C-x C-v
Visit a different file instead of the one visited last
(@code{find-alternate-file}).
@item C-x 4 f
Visit a file, in another window (@code{find-file-other-window}).  Don't
alter what is displayed in the selected window.
@item C-x 5 f
Visit a file, in a new frame (@code{find-file-other-frame}).  Don't
alter what is displayed in the selected frame.
@item M-x find-file-literally
Visit a file with no conversion of the contents.
@end table

@cindex files, visiting and saving
@cindex saving files
Eli Zaretskii's avatar
Eli Zaretskii committed
181
  @dfn{Visiting} a file means reading its contents into an Emacs
Richard M. Stallman's avatar
Richard M. Stallman committed
182 183 184 185 186 187 188 189 190
buffer so you can edit them.  Emacs makes a new buffer for each file
that you visit.  We often say that this buffer ``is visiting'' that
file, or that the buffer's ``visited file'' is that file.  Emacs
constructs the buffer name from the file name by throwing away the
directory, keeping just the name proper.  For example, a file named
@file{/usr/rms/emacs.tex} would get a buffer named @samp{emacs.tex}.
If there is already a buffer with that name, Emacs constructs a unique
name---the normal method is to append @samp{<2>}, @samp{<3>}, and so
on, but you can select other methods (@pxref{Uniquify}).
Dave Love's avatar
#  
Dave Love committed
191 192 193 194 195 196

  Each window's mode line shows the name of the buffer that is being displayed
in that window, so you can always tell what buffer you are editing.

  The changes you make with editing commands are made in the Emacs
buffer.  They do not take effect in the file that you visited, or any
Richard M. Stallman's avatar
Richard M. Stallman committed
197
permanent place, until you @dfn{save} the buffer.  Saving the buffer
Dave Love's avatar
#  
Dave Love committed
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
means that Emacs writes the current contents of the buffer into its
visited file.  @xref{Saving}.

@cindex modified (buffer)
  If a buffer contains changes that have not been saved, we say the
buffer is @dfn{modified}.  This is important because it implies that
some changes will be lost if the buffer is not saved.  The mode line
displays two stars near the left margin to indicate that the buffer is
modified.

@kindex C-x C-f
@findex find-file
  To visit a file, use the command @kbd{C-x C-f} (@code{find-file}).  Follow
the command with the name of the file you wish to visit, terminated by a
@key{RET}.

  The file name is read using the minibuffer (@pxref{Minibuffer}), with
defaulting and completion in the standard manner (@pxref{File Names}).
216
While in the minibuffer, you can abort @kbd{C-x C-f} by typing
217
@kbd{C-g}.  File-name completion ignores certain file names; for more
218
about this, see @ref{Completion Options}.
Dave Love's avatar
#  
Dave Love committed
219

Richard M. Stallman's avatar
Richard M. Stallman committed
220 221 222 223 224
  Your confirmation that @kbd{C-x C-f} has completed successfully is
the appearance of new text on the screen and a new buffer name in the
mode line.  If the specified file does not exist and you could not
create it, or exists but you can't read it, then you get an error,
with an error message displayed in the echo area.
Dave Love's avatar
#  
Dave Love committed
225 226 227

  If you visit a file that is already in Emacs, @kbd{C-x C-f} does not make
another copy.  It selects the existing buffer containing that file.
Richard M. Stallman's avatar
Richard M. Stallman committed
228 229 230
However, before doing so, it checks whether the file itself has changed
since you visited or saved it last.  If the file has changed, Emacs offers
to reread it.
Dave Love's avatar
#  
Dave Love committed
231

232
@vindex large-file-warning-threshold
233
@cindex maximum buffer size exceeded, error message
234 235 236 237 238 239 240 241 242 243
  If you try to visit a file larger than
@code{large-file-warning-threshold} (the default is 10000000, which is
about 10 megabytes), Emacs will ask you for confirmation first.  You
can answer @kbd{y} to proceed with visiting the file.  Note, however,
that Emacs cannot visit files that are larger than the maximum Emacs
buffer size, which is around 256 megabytes on 32-bit machines
(@pxref{Buffers}).  If you try, Emacs will display an error message
saying that the maximum buffer size has been exceeded.

@cindex file selection dialog
Richard M. Stallman's avatar
Richard M. Stallman committed
244
  On graphical displays there are two additional methods for
245 246 247 248 249
visiting files.  Firstly, when Emacs is built with a suitable GUI
toolkit, commands invoked with the mouse (by clicking on the menu bar
or tool bar) use the toolkit's standard File Selection dialog instead
of prompting for the file name in the minibuffer.  On Unix and
GNU/Linux platforms, Emacs does that when built with GTK, LessTif, and
250
Motif toolkits; on MS-Windows and Mac, the GUI version does that by default.
251
For information on how to customize this, see @ref{Dialog Boxes}.
252

253 254 255 256 257
  Secondly, Emacs supports ``drag and drop''; dropping a file into an
ordinary Emacs window visits the file using that window.  However,
dropping a file into a window displaying a Dired buffer moves or
copies the file into the displayed directory.  For details, see
@ref{Drag and Drop}, and @ref{Misc Dired Features}.
258

Dave Love's avatar
#  
Dave Love committed
259
@cindex creating files
260
  What if you want to create a new file?  Just visit it.  Emacs displays
261
@samp{(New file)} in the echo area, but in other respects behaves as if
Dave Love's avatar
#  
Dave Love committed
262 263 264
you had visited an existing empty file.  If you make any changes and
save them, the file is created.

265 266 267 268 269 270 271 272 273 274 275
  Emacs recognizes from the contents of a file which end-of-line
convention it uses to separate lines---newline (used on GNU/Linux and
on Unix), carriage-return linefeed (used on Microsoft systems), or
just carriage-return (used on the Macintosh)---and automatically
converts the contents to the normal Emacs convention, which is that
the newline character separates lines.  This is a part of the general
feature of coding system conversion (@pxref{Coding Systems}), and
makes it possible to edit files imported from different operating
systems with equal convenience.  If you change the text and save the
file, Emacs performs the inverse conversion, changing newlines back
into carriage-return linefeed or just carriage-return if appropriate.
Dave Love's avatar
#  
Dave Love committed
276 277 278 279

@vindex find-file-run-dired
  If the file you specify is actually a directory, @kbd{C-x C-f} invokes
Dired, the Emacs directory browser, so that you can ``edit'' the contents
Richard M. Stallman's avatar
Richard M. Stallman committed
280 281 282 283
of the directory (@pxref{Dired}).  Dired is a convenient way to view, delete,
or operate on the files in the directory.  However, if the variable
@code{find-file-run-dired} is @code{nil}, then it is an error to try
to visit a directory.
Dave Love's avatar
#  
Dave Love committed
284

285 286 287 288 289
  Files which are actually collections of other files, or @dfn{file
archives}, are visited in special modes which invoke a Dired-like
environment to allow operations on archive members.  @xref{File
Archives}, for more about these features.

290 291
@cindex wildcard characters in file names
@vindex find-file-wildcards
292
  If the file name you specify contains shell-style wildcard
293 294 295 296 297 298 299 300
characters, Emacs visits all the files that match it.  (On
case-insensitive filesystems, Emacs matches the wildcards disregarding
the letter case.)  Wildcards include @samp{?}, @samp{*}, and
@samp{[@dots{}]} sequences.  To enter the wild card @samp{?} in a file
name in the minibuffer, you need to type @kbd{C-q ?}.  @xref{Quoted
File Names}, for information on how to visit a file whose name
actually contains wildcard characters.  You can disable the wildcard
feature by customizing @code{find-file-wildcards}.
Dave Love's avatar
#  
Dave Love committed
301 302

  If you visit a file that the operating system won't let you modify,
303 304 305
or that is marked read-only, Emacs makes the buffer read-only too, so
that you won't go ahead and make changes that you'll have trouble
saving afterward.  You can make the buffer writable with @kbd{C-x C-q}
306
(@code{toggle-read-only}).  @xref{Misc Buffer}.
Dave Love's avatar
#  
Dave Love committed
307 308 309

@kindex C-x C-r
@findex find-file-read-only
310 311 312
  If you want to visit a file as read-only in order to protect
yourself from entering changes accidentally, visit it with the command
@kbd{C-x C-r} (@code{find-file-read-only}) instead of @kbd{C-x C-f}.
Dave Love's avatar
#  
Dave Love committed
313 314 315 316 317 318 319

@kindex C-x C-v
@findex find-alternate-file
  If you visit a nonexistent file unintentionally (because you typed the
wrong file name), use the @kbd{C-x C-v} command
(@code{find-alternate-file}) to visit the file you really wanted.
@kbd{C-x C-v} is similar to @kbd{C-x C-f}, but it kills the current
320 321 322 323
buffer (after first offering to save it if it is modified).  When
@kbd{C-x C-v} reads the file name to visit, it inserts the entire
default file name in the buffer, with point just after the directory
part; this is convenient if you made a slight error in typing the name.
Dave Love's avatar
#  
Dave Love committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342

@kindex C-x 4 f
@findex find-file-other-window
  @kbd{C-x 4 f} (@code{find-file-other-window}) is like @kbd{C-x C-f}
except that the buffer containing the specified file is selected in another
window.  The window that was selected before @kbd{C-x 4 f} continues to
show the same buffer it was already showing.  If this command is used when
only one window is being displayed, that window is split in two, with one
window showing the same buffer as before, and the other one showing the
newly requested file.  @xref{Windows}.

@kindex C-x 5 f
@findex find-file-other-frame
  @kbd{C-x 5 f} (@code{find-file-other-frame}) is similar, but opens a
new frame, or makes visible any existing frame showing the file you
seek.  This feature is available only when you are using a window
system.  @xref{Frames}.

@findex find-file-literally
343
  If you wish to edit a file as a sequence of @acronym{ASCII} characters with no special
Dave Love's avatar
#  
Dave Love committed
344 345 346
encoding or conversion, use the @kbd{M-x find-file-literally} command.
It visits a file, like @kbd{C-x C-f}, but does not do format conversion
(@pxref{Formatted Text}), character code conversion (@pxref{Coding
Gerd Moellmann's avatar
Gerd Moellmann committed
347 348
Systems}), or automatic uncompression (@pxref{Compressed Files}), and
does not add a final newline because of @code{require-final-newline}.
Dave Love's avatar
#  
Dave Love committed
349 350 351
If you already have visited the same file in the usual (non-literal)
manner, this command asks you whether to visit it literally instead.

Stefan Monnier's avatar
Stefan Monnier committed
352 353
@vindex find-file-hook
@vindex find-file-not-found-functions
Dave Love's avatar
#  
Dave Love committed
354 355
  Two special hook variables allow extensions to modify the operation of
visiting files.  Visiting a file that does not exist runs the functions
Stefan Monnier's avatar
Stefan Monnier committed
356
in the list @code{find-file-not-found-functions}; this variable holds a list
Dave Love's avatar
#  
Dave Love committed
357 358
of functions, and the functions are called one by one (with no
arguments) until one of them returns non-@code{nil}.  This is not a
Stefan Monnier's avatar
Stefan Monnier committed
359
normal hook, and the name ends in @samp{-functions} rather than @samp{-hook}
Dave Love's avatar
#  
Dave Love committed
360 361
to indicate that fact.

Richard M. Stallman's avatar
Richard M. Stallman committed
362
  Successful visiting of any file, whether existing or not, calls the
Stefan Monnier's avatar
Stefan Monnier committed
363 364 365
functions in the list @code{find-file-hook}, with no arguments.
This variable is a normal hook.  In the case of a nonexistent file, the
@code{find-file-not-found-functions} are run first.  @xref{Hooks}.
Dave Love's avatar
#  
Dave Love committed
366 367 368 369 370 371 372 373 374 375 376

  There are several ways to specify automatically the major mode for
editing the file (@pxref{Choosing Modes}), and to specify local
variables defined for that file (@pxref{File Variables}).

@node Saving
@section Saving Files

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

377 378 379 380 381 382 383 384 385 386
@menu
* Save Commands::       Commands for saving files.
* Backup::              How Emacs saves the old version of your file.
* Customize Save::      Customizing the saving of files.
* Interlocking::        How Emacs protects against simultaneous editing
                          of one file by two users.
* Shadowing: File Shadowing.  Copying files to "shadows" automatically.
* Time Stamps::         Emacs can update time stamps on saved files.
@end menu

Richard M. Stallman's avatar
Richard M. Stallman committed
387
@node Save Commands
388 389 390 391
@subsection Commands for Saving Files

  These are the commands that relate to saving and writing files.

Dave Love's avatar
#  
Dave Love committed
392 393
@table @kbd
@item C-x C-s
Richard M. Stallman's avatar
Richard M. Stallman committed
394
Save the current buffer in its visited file on disk (@code{save-buffer}).
Dave Love's avatar
#  
Dave Love committed
395 396 397 398
@item C-x s
Save any or all buffers in their visited files (@code{save-some-buffers}).
@item M-~
Forget that the current buffer has been changed (@code{not-modified}).
Gerd Moellmann's avatar
Gerd Moellmann committed
399
With prefix argument (@kbd{C-u}), mark the current buffer as changed.
Dave Love's avatar
#  
Dave Love committed
400
@item C-x C-w
Richard M. Stallman's avatar
Richard M. Stallman committed
401
Save the current buffer with a specified file name (@code{write-file}).
Dave Love's avatar
#  
Dave Love committed
402
@item M-x set-visited-file-name
403
Change the file name under which the current buffer will be saved.
Dave Love's avatar
#  
Dave Love committed
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448
@end table

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

@example
Wrote /u/rms/gnu/gnu.tasks
@end example

@noindent
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, @kbd{C-x C-s} displays a message
like this in the echo area:

@example
(No changes need to be saved)
@end example

@kindex C-x s
@findex save-some-buffers
  The command @kbd{C-x s} (@code{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 @code{query-replace}:

@table @kbd
@item y
Save this buffer and ask about the rest of the buffers.
@item n
Don't save this buffer, but ask about the rest of the buffers.
@item !
Save this buffer and all the rest with no more questions.
@c following generates acceptable underfull hbox
@item @key{RET}
Terminate @code{save-some-buffers} without any more saving.
@item .
Save this buffer, then exit @code{save-some-buffers} without even asking
about other buffers.
@item C-r
View the buffer that you are currently being asked about.  When you exit
View mode, you get back to @code{save-some-buffers}, which asks the
question again.
Richard M. Stallman's avatar
Richard M. Stallman committed
449 450 451
@item d
Diff the buffer against its corresponding file, so you can see
what changes you would be saving.
Dave Love's avatar
#  
Dave Love committed
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472
@item C-h
Display a help message about these options.
@end table

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

@kindex M-~
@findex not-modified
  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
@kbd{C-x s} or @kbd{C-x C-c}, you are liable to save this buffer by
mistake.  One thing you can do is type @kbd{M-~} (@code{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.  (@samp{~} is often used as a mathematical symbol for `not'; thus
@kbd{M-~} is `not', metafied.)  You could also use
@code{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
Richard M. Stallman's avatar
Richard M. Stallman committed
473
called @dfn{reverting}.  @xref{Reverting}.  (You could also undo all the
Dave Love's avatar
#  
Dave Love committed
474
changes by repeating the undo command @kbd{C-x u} until you have undone
Richard M. Stallman's avatar
Richard M. Stallman committed
475
all the changes; but reverting is easier.)  You can also kill the buffer.
Dave Love's avatar
#  
Dave Love committed
476 477 478 479

@findex set-visited-file-name
  @kbd{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
Richard M. Stallman's avatar
Richard M. Stallman committed
480 481 482 483 484 485
minibuffer.  Then it marks the buffer as visiting that file name, and
changes the buffer name correspondingly.  @code{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 @kbd{C-x C-s} in that buffer
@emph{will} save.
Dave Love's avatar
#  
Dave Love committed
486 487 488 489

@kindex C-x C-w
@findex write-file
  If you wish to mark the buffer as visiting a different file and save it
490 491 492
right away, use @kbd{C-x C-w} (@code{write-file}).  It is
equivalent to @code{set-visited-file-name} followed by @kbd{C-x C-s}
(except that @kbd{C-x C-w} asks for confirmation if the file exists).
Dave Love's avatar
#  
Dave Love committed
493 494 495 496
@kbd{C-x C-s} used on a buffer that is not visiting a file has the
same effect as @kbd{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
Richard M. Stallman's avatar
Richard M. Stallman committed
497
with the buffer's default directory (@pxref{File Names}).
Dave Love's avatar
#  
Dave Love committed
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526

  If the new file name implies a major mode, then @kbd{C-x C-w} switches
to that major mode, in most cases.  The command
@code{set-visited-file-name} also does this.  @xref{Choosing Modes}.

  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.
@xref{Interlocking,, Simultaneous Editing}.

@node Backup
@subsection Backup Files
@cindex backup file
@vindex make-backup-files
@vindex vc-make-backup-files

  On most operating systems, rewriting a file automatically destroys all
record of what the file used to contain.  Thus, saving a file from Emacs
throws away the old contents of the file---or it would, except that
Emacs carefully copies the old contents to another file, called the
@dfn{backup} file, before actually saving.

  For most files, the variable @code{make-backup-files} determines
whether to make backup files.  On most operating systems, its default
value is @code{t}, so that Emacs does write backup files.

  For files managed by a version control system (@pxref{Version
Control}), the variable @code{vc-make-backup-files} determines whether
527
to make backup files.  By default it is @code{nil}, since backup files
Dave Love's avatar
#  
Dave Love committed
528
are redundant when you store all the previous versions in a version
529 530 531 532 533 534 535 536
control system.
@iftex
@xref{General VC Options,,,emacs-xtra, Specialized Emacs Features}.
@end iftex
@ifnottex
@xref{General VC Options}.
@end ifnottex

Dave Love's avatar
#  
Dave Love committed
537

Richard M. Stallman's avatar
Richard M. Stallman committed
538 539 540
  At your option, Emacs can keep either a single backup for each file,
or make a series of numbered backup files for each file that you edit.

Dave Love's avatar
Dave Love committed
541 542 543
@vindex backup-enable-predicate
@vindex temporary-file-directory
@vindex small-temporary-file-directory
Dave Love's avatar
#  
Dave Love committed
544
  The default value of the @code{backup-enable-predicate} variable
545 546 547
prevents backup files being written for files in the directories used
for temporary files, specified by @code{temporary-file-directory} or
@code{small-temporary-file-directory}.
Dave Love's avatar
#  
Dave Love committed
548 549 550 551 552 553 554 555 556 557 558 559 560 561

  Emacs makes a backup for a file only the first time the file is saved
from one buffer.  No matter how many times you save a file, its backup file
continues to contain the contents from before the file was visited.
Normally this means that the backup file contains the contents from before
the current editing session; however, if you kill the buffer and then visit
the file again, a new backup file will be made by the next save.

  You can also explicitly request making another backup file from a
buffer even though it has already been saved at least once.  If you save
the buffer with @kbd{C-u C-x C-s}, the version thus saved will be made
into a backup file if you save the buffer again.  @kbd{C-u C-u C-x C-s}
saves the buffer, but first makes the previous file contents into a new
backup file.  @kbd{C-u C-u C-u C-x C-s} does both things: it makes a
Richard M. Stallman's avatar
Richard M. Stallman committed
562 563
backup from the previous contents, and arranges to make another from the
newly saved contents if you save again.
Dave Love's avatar
#  
Dave Love committed
564 565

@menu
566
* One or Many: Numbered Backups. Whether to make one backup file or many.
Richard M. Stallman's avatar
Richard M. Stallman committed
567
* Names: Backup Names.		How backup files are named.
Dave Love's avatar
#  
Dave Love committed
568 569 570 571
* Deletion: Backup Deletion.	Emacs deletes excess numbered backups.
* Copying: Backup Copying.	Backups can be made by copying or renaming.
@end menu

Richard M. Stallman's avatar
Richard M. Stallman committed
572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
@node Numbered Backups
@subsubsection Numbered Backups

@vindex version-control
  The choice of single backup file or multiple numbered backup files
is controlled by the variable @code{version-control}.  Its possible
values are:

@table @code
@item t
Make numbered backups.
@item nil
Make numbered backups for files that have numbered backups already.
Otherwise, make single backups.
@item never
Never make numbered backups; always make single backups.
@end table

@noindent
The usual way to set this variable is globally, through your
@file{.emacs} file or the customization buffer.  However, you can set
@code{version-control} locally in an individual buffer to control the
making of backups for that buffer's file.  For example, Rmail mode
locally sets @code{version-control} to @code{never} to make sure that
there is only one backup for an Rmail file.  @xref{Locals}.

@cindex @env{VERSION_CONTROL} environment variable
  If you set the environment variable @env{VERSION_CONTROL}, to tell
various GNU utilities what to do with backup files, Emacs also obeys the
environment variable by setting the Lisp variable @code{version-control}
accordingly at startup.  If the environment variable's value is @samp{t}
or @samp{numbered}, then @code{version-control} becomes @code{t}; if the
value is @samp{nil} or @samp{existing}, then @code{version-control}
becomes @code{nil}; if it is @samp{never} or @samp{simple}, then
@code{version-control} becomes @code{never}.

Dave Love's avatar
#  
Dave Love committed
608 609 610
@node Backup Names
@subsubsection Single or Numbered Backups

Richard M. Stallman's avatar
Richard M. Stallman committed
611 612 613
  When Emacs makes a single backup file, its name is normally
constructed by appending @samp{~} to the file name being edited; thus,
the backup file for @file{eval.c} would be @file{eval.c~}.
Dave Love's avatar
#  
Dave Love committed
614

Dave Love's avatar
Dave Love committed
615 616
@vindex make-backup-file-name-function
@vindex backup-directory-alist
617
  You can change this behavior by defining the variable
Dave Love's avatar
Dave Love committed
618 619
@code{make-backup-file-name-function} to a suitable function.
Alternatively you can customize the variable
Dave Love's avatar
Dave Love committed
620
@code{backup-directory-alist} to specify that files matching certain
621 622 623 624 625 626
patterns should be backed up in specific directories.

  A typical use is to add an element @code{("." . @var{dir})} to make
all backups in the directory with absolute name @var{dir}; Emacs
modifies the backup file names to avoid clashes between files with the
same names originating in different directories.  Alternatively,
Andreas Schwab's avatar
Andreas Schwab committed
627
adding, say, @code{("." . ".~")} would make backups in the invisible
628 629 630 631 632 633 634
subdirectory @file{.~} of the original file's directory.  Emacs
creates the directory, if necessary, to make the backup.

  If access control stops Emacs from writing backup files under the usual
names, it writes the backup file as @file{%backup%~} in your home
directory.  Only one such file can exist, so only the most recently
made such backup is available.
Dave Love's avatar
Dave Love committed
635

Dave Love's avatar
#  
Dave Love committed
636
  If you choose to have a series of numbered backup files, backup file
637 638 639 640 641 642
names contain @samp{.~}, the number, and another @samp{~} after the
original file name.  Thus, the backup files of @file{eval.c} would be
called @file{eval.c.~1~}, @file{eval.c.~2~}, and so on, all the way
through names like @file{eval.c.~259~} and beyond.  The variable
@code{backup-directory-alist} applies to numbered backups just as
usual.
Dave Love's avatar
#  
Dave Love committed
643 644 645 646

@node Backup Deletion
@subsubsection Automatic Deletion of Backups

Eli Zaretskii's avatar
Eli Zaretskii committed
647
  To prevent excessive consumption of disk space, Emacs can delete numbered
Dave Love's avatar
#  
Dave Love committed
648 649 650 651 652 653 654 655
backup versions automatically.  Generally Emacs keeps the first few backups
and the latest few backups, deleting any in between.  This happens every
time a new backup is made.

@vindex kept-old-versions
@vindex kept-new-versions
  The two variables @code{kept-old-versions} and
@code{kept-new-versions} control this deletion.  Their values are,
Richard M. Stallman's avatar
Richard M. Stallman committed
656 657 658 659 660 661 662 663
respectively, the number of oldest (lowest-numbered) backups to keep
and the number of newest (highest-numbered) ones to keep, each time a
new backup is made.  The backups in the middle (excluding those oldest
and newest) are the excess middle versions---those backups are
deleted.  These variables' values are used when it is time to delete
excess versions, just after a new backup version is made; the newly
made backup is included in the count in @code{kept-new-versions}.  By
default, both variables are 2.
Dave Love's avatar
#  
Dave Love committed
664 665

@vindex delete-old-versions
666 667 668 669
  If @code{delete-old-versions} is @code{t}, Emacs deletes the excess
backup files silently.  If it is @code{nil}, the default, Emacs asks
you whether it should delete the excess backup versions.  If it has
any other value, then Emacs never automatically deletes backups.
Dave Love's avatar
#  
Dave Love committed
670 671 672 673 674 675 676

  Dired's @kbd{.} (Period) command can also be used to delete old versions.
@xref{Dired Deletion}.

@node Backup Copying
@subsubsection Copying vs.@: Renaming

Richard M. Stallman's avatar
Richard M. Stallman committed
677 678 679 680 681 682 683
  Backup files can be made by copying the old file or by renaming it.
This makes a difference when the old file has multiple names (hard
links).  If the old file is renamed into the backup file, then the
alternate names become names for the backup file.  If the old file is
copied instead, then the alternate names remain names for the file
that you are editing, and the contents accessed by those names will be
the new contents.
Dave Love's avatar
#  
Dave Love committed
684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699

  The method of making a backup file may also affect the file's owner
and group.  If copying is used, these do not change.  If renaming is used,
you become the file's owner, and the file's group becomes the default
(different operating systems have different defaults for the group).

  Having the owner change is usually a good idea, because then the owner
always shows who last edited the file.  Also, the owners of the backups
show who produced those versions.  Occasionally there is a file whose
owner should not change; it is a good idea for such files to contain
local variable lists to set @code{backup-by-copying-when-mismatch}
locally (@pxref{File Variables}).

@vindex backup-by-copying
@vindex backup-by-copying-when-linked
@vindex backup-by-copying-when-mismatch
700 701
@vindex backup-by-copying-when-privileged-mismatch
@cindex file ownership, and backup
702
@cindex backup, and user-id
703
  The choice of renaming or copying is controlled by four variables.
Dave Love's avatar
#  
Dave Love committed
704 705 706 707 708 709 710 711
Renaming is the default choice.  If the variable
@code{backup-by-copying} is non-@code{nil}, copying is used.  Otherwise,
if the variable @code{backup-by-copying-when-linked} is non-@code{nil},
then copying is used for files that have multiple names, but renaming
may still be used when the file being edited has only one name.  If the
variable @code{backup-by-copying-when-mismatch} is non-@code{nil}, then
copying is used if renaming would cause the file's owner or group to
change.  @code{backup-by-copying-when-mismatch} is @code{t} by default
712 713
if you start Emacs as the superuser.  The fourth variable,
@code{backup-by-copying-when-privileged-mismatch}, gives the highest
714
numeric user-id for which @code{backup-by-copying-when-mismatch} will be
715
forced on.  This is useful when low-numbered user-ids are assigned to
716 717
special system users, such as @code{root}, @code{bin}, @code{daemon},
etc., which must maintain ownership of files.
Dave Love's avatar
#  
Dave Love committed
718 719 720 721 722 723 724 725 726

  When a file is managed with a version control system (@pxref{Version
Control}), Emacs does not normally make backups in the usual way for
that file.  But check-in and check-out are similar in some ways to
making backups.  One unfortunate similarity is that these operations
typically break hard links, disconnecting the file name you visited from
any alternate names for the same file.  This has nothing to do with
Emacs---the version control system does it.

727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
@node Customize Save
@subsection Customizing Saving of Files

@vindex require-final-newline
  If the value of the variable @code{require-final-newline} is
@code{t}, saving or writing a file silently puts a newline at the end
if there isn't already one there.  If the value is @code{visit}, Emacs
adds a newline at the end of any file that doesn't have one, just
after it visits the file.  (This marks the buffer as modified, and you
can undo it.)  If the value is @code{visit-save}, that means to add
newlines both on visiting and on saving.  If the value is @code{nil},
Emacs leaves the end of the file unchanged; if it's neither @code{nil}
nor @code{t}, Emacs asks you whether to add a newline.  The default is
@code{nil}.

@vindex mode-require-final-newline
  Many major modes are designed for specific kinds of files that are
always supposed to end in newlines.  These major modes set the
variable @code{require-final-newline} according to
@code{mode-require-final-newline}.  By setting the latter variable,
you can control how these modes handle final newlines.

@vindex write-region-inhibit-fsync
  When Emacs saves a file, it invokes the @code{fsync} system call to
force the data immediately out to disk.  This is important for safety
if the system crashes or in case of power outage.  However, it can be
disruptive on laptops using power saving, because it requires the disk
to spin up each time you save a file.  Setting
@code{write-region-inhibit-fsync} to a non-@code{nil} value disables
this synchronization.  Be careful---this means increased risk of data
loss.

Dave Love's avatar
#  
Dave Love committed
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799
@node Interlocking
@subsection Protection against Simultaneous Editing

@cindex file dates
@cindex simultaneous editing
  Simultaneous editing occurs when two users visit the same file, both
make changes, and then both save them.  If nobody were informed that
this was happening, whichever user saved first would later find that his
changes were lost.

  On some systems, Emacs notices immediately when the second user starts
to change the file, and issues an immediate warning.  On all systems,
Emacs checks when you save the file, and warns if you are about to
overwrite another user's changes.  You can prevent loss of the other
user's work by taking the proper corrective action instead of saving the
file.

@findex ask-user-about-lock
@cindex locking files
  When you make the first modification in an Emacs buffer that is
visiting a file, Emacs records that the file is @dfn{locked} by you.
(It does this by creating a symbolic link in the same directory with a
different name.)  Emacs removes the lock when you save the changes.  The
idea is that the file is locked whenever an Emacs buffer visiting it has
unsaved changes.

@cindex collision
  If you begin to modify the buffer while the visited file is locked by
someone else, this constitutes a @dfn{collision}.  When Emacs detects a
collision, it asks you what to do, by calling the Lisp function
@code{ask-user-about-lock}.  You can redefine this function for the sake
of customization.  The standard definition of this function asks you a
question and accepts three possible answers:

@table @kbd
@item s
Steal the lock.  Whoever was already changing the file loses the lock,
and you gain the lock.
@item p
Proceed.  Go ahead and edit the file despite its being locked by someone else.
@item q
Richard M. Stallman's avatar
Richard M. Stallman committed
800 801 802
Quit.  This causes an error (@code{file-locked}), and the buffer
contents remain unchanged---the modification you were trying to make
does not actually take place.
Dave Love's avatar
#  
Dave Love committed
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
@end table

  Note that locking works on the basis of a file name; if a file has
multiple names, Emacs does not realize that the two names are the same file
and cannot prevent two users from editing it simultaneously under different
names.  However, basing locking on names means that Emacs can interlock the
editing of new files that will not really exist until they are saved.

  Some systems are not configured to allow Emacs to make locks, and
there are cases where lock files cannot be written.  In these cases,
Emacs cannot detect trouble in advance, but it still can detect the
collision when you try to save a file and overwrite someone else's
changes.

  If Emacs or the operating system crashes, this may leave behind lock
818
files which are stale, so you may occasionally get warnings about
Dave Love's avatar
#  
Dave Love committed
819 820 821 822 823 824 825 826
spurious collisions.  When you determine that the collision is spurious,
just use @kbd{p} to tell Emacs to go ahead anyway.

  Every time Emacs saves a buffer, it first checks the last-modification
date of the existing file on disk to verify that it has not changed since the
file was last visited or saved.  If the date does not match, it implies
that changes were made in the file in some other way, and these changes are
about to be lost if Emacs actually does save.  To prevent this, Emacs
827
displays a warning message and asks for confirmation before saving.
Dave Love's avatar
#  
Dave Love committed
828 829 830 831 832 833 834 835 836 837 838
Occasionally you will know why the file was changed and know that it does
not matter; then you can answer @kbd{yes} and proceed.  Otherwise, you should
cancel the save with @kbd{C-g} and investigate the situation.

  The first thing you should do when notified that simultaneous editing
has already taken place is to list the directory with @kbd{C-u C-x C-d}
(@pxref{Directories}).  This shows the file's current author.  You
should attempt to contact him to warn him not to continue editing.
Often the next step is to save the contents of your Emacs buffer under a
different name, and use @code{diff} to compare the two files.@refill

Dave Love's avatar
Dave Love committed
839 840 841 842
@node File Shadowing
@subsection Shadowing Files
@cindex shadow files
@cindex file shadows
Richard M. Stallman's avatar
Richard M. Stallman committed
843
@findex shadow-initialize
Dave Love's avatar
Dave Love committed
844 845 846 847 848 849

@table @kbd
@item M-x shadow-initialize
Set up file shadowing.
@item M-x shadow-define-literal-group
Declare a single file to be shared between sites.
850 851 852 853
@item M-x shadow-define-regexp-group
Make all files that match each of a group of files be shared between hosts.
@item M-x shadow-define-cluster @key{RET} @var{name} @key{RET}
Define a shadow file cluster @var{name}.
Dave Love's avatar
Dave Love committed
854 855
@item M-x shadow-copy-files
Copy all pending shadow files.
856 857
@item M-x shadow-cancel
Cancel the instruction to shadow some files.
Dave Love's avatar
Dave Love committed
858 859
@end table

860 861 862 863 864 865 866 867 868 869
You can arrange to keep identical @dfn{shadow} copies of certain files
in more than one place---possibly on different machines.  To do this,
first you must set up a @dfn{shadow file group}, which is a set of
identically-named files shared between a list of sites.  The file
group is permanent and applies to further Emacs sessions as well as
the current one.  Once the group is set up, every time you exit Emacs,
it will copy the file you edited to the other files in its group.  You
can also do the copying without exiting Emacs, by typing @kbd{M-x
shadow-copy-files}.

870 871 872
To set up a shadow file group, use @kbd{M-x
shadow-define-literal-group} or @kbd{M-x shadow-define-regexp-group}.
See their documentation strings for further information.
873 874 875 876 877 878 879 880 881 882

Before copying a file to its shadows, Emacs asks for confirmation.
You can answer ``no'' to bypass copying of this file, this time.  If
you want to cancel the shadowing permanently for a certain file, use
@kbd{M-x shadow-cancel} to eliminate or change the shadow file group.

A @dfn{shadow cluster} is a group of hosts that share directories, so
that copying to or from one of them is sufficient to update the file
on all of them.  Each shadow cluster has a name, and specifies the
network address of a primary host (the one we copy files to), and a
Richard M. Stallman's avatar
Richard M. Stallman committed
883
regular expression that matches the host names of all the other hosts
884 885
in the cluster.  You can define a shadow cluster with @kbd{M-x
shadow-define-cluster}.
Dave Love's avatar
Dave Love committed
886

Dave Love's avatar
Dave Love committed
887 888 889 890
@node Time Stamps
@subsection Updating Time Stamps Automatically
@cindex time stamps
@cindex modification dates
Dave Love's avatar
Dave Love committed
891
@cindex locale, date format
Dave Love's avatar
Dave Love committed
892

893
You can arrange to put a time stamp in a file, so that it will be updated
894 895 896 897
automatically each time you edit and save the file.  The time stamp
has to be in the first eight lines of the file, and you should
insert it like this:

Dave Love's avatar
Dave Love committed
898 899 900
@example
Time-stamp: <>
@end example
901

Dave Love's avatar
Dave Love committed
902
@noindent
903 904
or like this:

Dave Love's avatar
Dave Love committed
905
@example
906
Time-stamp: " "
Dave Love's avatar
Dave Love committed
907 908
@end example

Richard M. Stallman's avatar
Richard M. Stallman committed
909
@findex time-stamp
910
  Then add the hook function @code{time-stamp} to the hook
911
@code{before-save-hook}; that hook function will automatically update
912 913 914 915 916
the time stamp, inserting the current date and time when you save the
file.  You can also use the command @kbd{M-x time-stamp} to update the
time stamp manually.  For other customizations, see the Custom group
@code{time-stamp}.  Note that non-numeric fields in the time stamp are
formatted according to your locale setting (@pxref{Environment}).
Dave Love's avatar
Dave Love committed
917

Dave Love's avatar
#  
Dave Love committed
918 919 920 921
@node Reverting
@section Reverting a Buffer
@findex revert-buffer
@cindex drastic changes
922
@cindex reread a file
Dave Love's avatar
#  
Dave Love committed
923 924 925 926 927 928 929

  If you have made extensive changes to a file and then change your mind
about them, you can get rid of them by reading in the previous version
of the file.  To do this, use @kbd{M-x revert-buffer}, which operates on
the current buffer.  Since reverting a buffer unintentionally could lose
a lot of work, you must confirm this command with @kbd{yes}.

930 931 932 933
  @code{revert-buffer} tries to position point in such a way that, if
the file was edited only slightly, you will be at approximately the
same piece of text after reverting as before.  However, if you have made
drastic changes, point may wind up in a totally different piece of text.
Dave Love's avatar
#  
Dave Love committed
934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956

  Reverting marks the buffer as ``not modified'' until another change is
made.

  Some kinds of buffers whose contents reflect data bases other than files,
such as Dired buffers, can also be reverted.  For them, reverting means
recalculating their contents from the appropriate data base.  Buffers
created explicitly with @kbd{C-x b} cannot be reverted; @code{revert-buffer}
reports an error when asked to do so.

@vindex revert-without-query
  When you edit a file that changes automatically and frequently---for
example, a log of output from a process that continues to run---it may be
useful for Emacs to revert the file without querying you, whenever you
visit the file again with @kbd{C-x C-f}.

  To request this behavior, set the variable @code{revert-without-query}
to a list of regular expressions.  When a file name matches one of these
regular expressions, @code{find-file} and @code{revert-buffer} will
revert it automatically if it has changed---provided the buffer itself
is not modified.  (If you have edited the text, it would be wrong to
discard your changes.)

Dave Love's avatar
Dave Love committed
957 958 959 960 961 962
@cindex Global Auto-Revert mode
@cindex mode, Global Auto-Revert
@cindex Auto-Revert mode
@cindex mode, Auto-Revert
@findex global-auto-revert-mode
@findex auto-revert-mode
963 964 965 966 967
@findex auto-revert-tail-mode

  You may find it useful to have Emacs revert files automatically when
they change.  Three minor modes are available to do this.

Richard M. Stallman's avatar
Richard M. Stallman committed
968
  @kbd{M-x global-auto-revert-mode} enables Global Auto-Revert mode,
969
which periodically checks all file buffers and reverts when the
Richard M. Stallman's avatar
Richard M. Stallman committed
970 971 972 973 974 975 976 977 978 979
corresponding file has changed.  @kbd{M-x auto-revert-mode} enables a
local version, Auto-Revert mode, which applies only to the current
buffer.

  You can use Auto-Revert mode to ``tail'' a file such as a system
log, so that changes made to that file by other programs are
continuously displayed.  To do this, just move the point to the end of
the buffer, and it will stay there as the file contents change.
However, if you are sure that the file will only change by growing at
the end, use Auto-Revert Tail mode instead
Luc Teirlinck's avatar
Luc Teirlinck committed
980
(@code{auto-revert-tail-mode}).  It is more efficient for this.
981

Dave Love's avatar
Dave Love committed
982
@vindex auto-revert-interval
983 984 985
  The variable @code{auto-revert-interval} controls how often to check
for a changed file.  Since checking a remote file is too slow, these
modes do not check or revert remote files.
Dave Love's avatar
Dave Love committed
986

Richard M. Stallman's avatar
Richard M. Stallman committed
987
  @xref{VC Mode Line}, for Auto Revert peculiarities in buffers that
988
visit files under version control.
989

990 991 992 993
@ifnottex
@include arevert-xtra.texi
@end ifnottex

Dave Love's avatar
#  
Dave Love committed
994 995 996 997 998 999 1000 1001 1002 1003 1004
@node Auto Save
@section Auto-Saving: Protection Against Disasters
@cindex Auto Save mode
@cindex mode, Auto Save
@cindex crashes

  Emacs saves all the visited files from time to time (based on counting
your keystrokes) without being asked.  This is called @dfn{auto-saving}.
It prevents you from losing more than a limited amount of work if the
system crashes.

Richard M. Stallman's avatar
Richard M. Stallman committed
1005 1006 1007 1008 1009 1010 1011
  When Emacs determines that it is time for auto-saving, it considers
each buffer, and each is auto-saved if auto-saving is enabled for it
and it has been changed since the last time it was auto-saved.  The
message @samp{Auto-saving...} is displayed in the echo area during
auto-saving, if any files are actually auto-saved.  Errors occurring
during auto-saving are caught so that they do not interfere with the
execution of commands you have been typing.
Dave Love's avatar
#  
Dave Love committed
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034

@menu
* Files: Auto Save Files.       The file where auto-saved changes are
                                  actually made until you save the file.
* Control: Auto Save Control.   Controlling when and how often to auto-save.
* Recover::		        Recovering text from auto-save files.
@end menu

@node Auto Save Files
@subsection Auto-Save Files

  Auto-saving does not normally save in the files that you visited, because
it can be very undesirable to save a program that is in an inconsistent
state when you have made half of a planned change.  Instead, auto-saving
is done in a different file called the @dfn{auto-save file}, and the
visited file is changed only when you request saving explicitly (such as
with @kbd{C-x C-s}).

  Normally, the auto-save file name is made by appending @samp{#} to the
front and rear of the visited file name.  Thus, a buffer visiting file
@file{foo.c} is auto-saved in a file @file{#foo.c#}.  Most buffers that
are not visiting files are auto-saved only if you request it explicitly;
when they are auto-saved, the auto-save file name is made by appending
1035 1036
@samp{#} to the front and rear of buffer name, then
adding digits and letters at the end for uniqueness.  For
Dave Love's avatar
#  
Dave Love committed
1037
example, the @samp{*mail*} buffer in which you compose messages to be
1038
sent might be auto-saved in a file named @file{#*mail*#704juu}.  Auto-save file
Dave Love's avatar
#  
Dave Love committed
1039 1040 1041 1042 1043
names are made this way unless you reprogram parts of Emacs to do
something different (the functions @code{make-auto-save-file-name} and
@code{auto-save-file-name-p}).  The file name to be used for auto-saving
in a buffer is calculated when auto-saving is turned on in that buffer.

1044 1045
@cindex auto-save for remote files
@vindex auto-save-file-name-transforms
1046 1047 1048 1049 1050 1051
  The variable @code{auto-save-file-name-transforms} allows a degree
of control over the auto-save file name.  It lets you specify a series
of regular expressions and replacements to transform the auto save
file name.  The default value puts the auto-save files for remote
files (@pxref{Remote Files}) into the temporary file directory on the
local machine.
1052

Dave Love's avatar
#  
Dave Love committed
1053 1054 1055 1056 1057
  When you delete a substantial part of the text in a large buffer, auto
save turns off temporarily in that buffer.  This is because if you
deleted the text unintentionally, you might find the auto-save file more
useful if it contains the deleted text.  To reenable auto-saving after
this happens, save the buffer with @kbd{C-x C-s}, or use @kbd{C-u 1 M-x
1058
auto-save-mode}.
Dave Love's avatar
#  
Dave Love committed
1059 1060

@vindex auto-save-visited-file-name
Richard M. Stallman's avatar
Richard M. Stallman committed
1061 1062 1063 1064 1065
  If you want auto-saving to be done in the visited file rather than
in a separate auto-save file, set the variable
@code{auto-save-visited-file-name} to a non-@code{nil} value.  In this
mode, there is no real difference between auto-saving and explicit
saving.
Dave Love's avatar
#  
Dave Love committed
1066 1067 1068

@vindex delete-auto-save-files
  A buffer's auto-save file is deleted when you save the buffer in its
Richard M. Stallman's avatar
Richard M. Stallman committed
1069 1070 1071 1072
visited file.  (You can inhibit this by setting the variable
@code{delete-auto-save-files} to @code{nil}.)  Changing the visited
file name with @kbd{C-x C-w} or @code{set-visited-file-name} renames
any auto-save file to go with the new visited name.
Dave Love's avatar
#  
Dave Love committed
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091

@node Auto Save Control
@subsection Controlling Auto-Saving

@vindex auto-save-default
@findex auto-save-mode
  Each time you visit a file, auto-saving is turned on for that file's
buffer if the variable @code{auto-save-default} is non-@code{nil} (but not
in batch mode; @pxref{Entering Emacs}).  The default for this variable is
@code{t}, so auto-saving is the usual practice for file-visiting buffers.
Auto-saving can be turned on or off for any existing buffer with the
command @kbd{M-x auto-save-mode}.  Like other minor mode commands, @kbd{M-x
auto-save-mode} turns auto-saving on with a positive argument, off with a
zero or negative argument; with no argument, it toggles.

@vindex auto-save-interval
  Emacs does auto-saving periodically based on counting how many characters
you have typed since the last time auto-saving was done.  The variable
@code{auto-save-interval} specifies how many characters there are between
1092 1093 1094
auto-saves.  By default, it is 300.  Emacs doesn't accept values that are
too small: if you customize @code{auto-save-interval} to a value less
than 20, Emacs will behave as if the value is 20.
Dave Love's avatar
#  
Dave Love committed
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155

@vindex auto-save-timeout
  Auto-saving also takes place when you stop typing for a while.  The
variable @code{auto-save-timeout} says how many seconds Emacs should
wait before it does an auto save (and perhaps also a garbage
collection).  (The actual time period is longer if the current buffer is
long; this is a heuristic which aims to keep out of your way when you
are editing long buffers, in which auto-save takes an appreciable amount
of time.)  Auto-saving during idle periods accomplishes two things:
first, it makes sure all your work is saved if you go away from the
terminal for a while; second, it may avoid some auto-saving while you
are actually typing.

  Emacs also does auto-saving whenever it gets a fatal error.  This
includes killing the Emacs job with a shell command such as @samp{kill
%emacs}, or disconnecting a phone line or network connection.

@findex do-auto-save
  You can request an auto-save explicitly with the command @kbd{M-x
do-auto-save}.

@node Recover
@subsection Recovering Data from Auto-Saves

@findex recover-file
  You can use the contents of an auto-save file to recover from a loss
of data with the command @kbd{M-x recover-file @key{RET} @var{file}
@key{RET}}.  This visits @var{file} and then (after your confirmation)
restores the contents from its auto-save file @file{#@var{file}#}.
You can then save with @kbd{C-x C-s} to put the recovered text into
@var{file} itself.  For example, to recover file @file{foo.c} from its
auto-save file @file{#foo.c#}, do:@refill

@example
M-x recover-file @key{RET} foo.c @key{RET}
yes @key{RET}
C-x C-s
@end example

  Before asking for confirmation, @kbd{M-x recover-file} displays a
directory listing describing the specified file and the auto-save file,
so you can compare their sizes and dates.  If the auto-save file
is older, @kbd{M-x recover-file} does not offer to read it.

@findex recover-session
  If Emacs or the computer crashes, you can recover all the files you
were editing from their auto save files with the command @kbd{M-x
recover-session}.  This first shows you a list of recorded interrupted
sessions.  Move point to the one you choose, and type @kbd{C-c C-c}.

  Then @code{recover-session} asks about each of the files that were
being edited during that session, asking whether to recover that file.
If you answer @kbd{y}, it calls @code{recover-file}, which works in its
normal fashion.  It shows the dates of the original file and its
auto-save file, and asks once again whether to recover that file.

  When @code{recover-session} is done, the files you've chosen to
recover are present in Emacs buffers.  You should then save them.  Only
this---saving them---updates the files themselves.

@vindex auto-save-list-file-prefix
Karl Berry's avatar
Karl Berry committed
1156 1157
  Emacs records information about interrupted sessions for later
recovery in files named
1158
@file{~/.emacs.d/auto-save-list/.saves-@var{pid}-@var{hostname}}.  All
Karl Berry's avatar
Karl Berry committed
1159 1160 1161 1162 1163
of this name except the @file{@var{pid}-@var{hostname}} part comes
from the value of @code{auto-save-list-file-prefix}.  You can record
sessions in a different place by customizing that variable.  If you
set @code{auto-save-list-file-prefix} to @code{nil} in your
@file{.emacs} file, sessions are not recorded for recovery.
Dave Love's avatar
#  
Dave Love committed
1164 1165 1166

@node File Aliases
@section File Name Aliases
1167 1168
@cindex symbolic links (visiting)
@cindex hard links (visiting)
Dave Love's avatar
#  
Dave Love committed
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178

  Symbolic links and hard links both make it possible for several file
names to refer to the same file.  Hard links are alternate names that
refer directly to the file; all the names are equally valid, and no one
of them is preferred.  By contrast, a symbolic link is a kind of defined
alias: when @file{foo} is a symbolic link to @file{bar}, you can use
either name to refer to the file, but @file{bar} is the real name, while
@file{foo} is just an alias.  More complex cases occur when symbolic
links point to directories.

1179
@vindex find-file-existing-other-name
1180
@vindex find-file-suppress-same-file-warnings
1181

1182 1183 1184
  Normally, if you visit a file which Emacs is already visiting under
a different name, Emacs displays a message in the echo area and uses
the existing buffer visiting that file.  This can happen on systems
1185 1186 1187 1188 1189 1190 1191 1192
that support hard or symbolic links, or if you use a long file name on
a system that truncates long file names, or on a case-insensitive file
system.  You can suppress the message by setting the variable
@code{find-file-suppress-same-file-warnings} to a non-@code{nil}
value.  You can disable this feature entirely by setting the variable
@code{find-file-existing-other-name} to @code{nil}: then if you visit
the same file under two different names, you get a separate buffer for
each file name.
Dave Love's avatar
#  
Dave Love committed
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209

@vindex find-file-visit-truename
@cindex truenames of files
@cindex file truenames
  If the variable @code{find-file-visit-truename} is non-@code{nil},
then the file name recorded for a buffer is the file's @dfn{truename}
(made by replacing all symbolic links with their target names), rather
than the name you specify.  Setting @code{find-file-visit-truename} also
implies the effect of @code{find-file-existing-other-name}.

@node Version Control
@section Version Control
@cindex version control

  @dfn{Version control systems} are packages that can record multiple
versions of a source file, usually storing the unchanged parts of the
file just once.  Version control systems also record history information
1210
such as the creation time of each version, who created it, and a
Dave Love's avatar
#  
Dave Love committed
1211 1212
description of what was changed in that version.

1213
  The Emacs version control interface is called VC.  Its commands work
1214 1215 1216 1217 1218 1219 1220
with different version control systems---currently, it supports CVS,
GNU Arch, RCS, Meta-CVS, Subversion, and SCCS.  Of these, the GNU
project distributes CVS, GNU Arch, and RCS; we recommend that you use
either CVS or GNU Arch for your projects, and RCS for individual
files.  We also have free software to replace SCCS, known as CSSC; if
you are using SCCS and don't want to make the incompatible change to
RCS or CVS, you can switch to CSSC.
Dave Love's avatar
#  
Dave Love committed
1221

André Spiegel's avatar
André Spiegel committed
1222 1223
  VC is enabled by default in Emacs.  To disable it, set the
customizable variable @code{vc-handled-backends} to @code{nil}
1224
@iftex
1225
(@pxref{Customizing VC,,,emacs-xtra, Specialized Emacs Features}).
1226 1227 1228 1229 1230
@end iftex
@ifnottex
(@pxref{Customizing VC}).
@end ifnottex

André Spiegel's avatar
André Spiegel committed
1231

Dave Love's avatar
#  
Dave Love committed
1232 1233
@menu
* Introduction to VC::  How version control works in general.
1234
* VC Mode Line::        How the mode line shows version control status.
Dave Love's avatar
#  
Dave Love committed
1235 1236 1237 1238
* Basic VC Editing::    How to edit a file under version control.
* Old Versions::        Examining and comparing old versions.
* Secondary VC Commands::    The commands used a little less frequently.
* Branches::            Multiple lines of development.
1239 1240 1241 1242 1243 1244
@ifnottex
* Remote Repositories:: Efficient access to remote CVS servers.
* Snapshots::           Sets of file versions treated as a unit.
* Miscellaneous VC::    Various other commands and features of VC.
* Customizing VC::      Variables that change VC's behavior.
@end ifnottex
Dave Love's avatar
#  
Dave Love committed
1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262
@end menu

@node Introduction to VC
@subsection Introduction to Version Control

  VC allows you to use a version control system from within Emacs,
integrating the version control operations smoothly with editing.  VC
provides a uniform interface to version control, so that regardless of
which version control system is in use, you can use it the same way.

  This section provides a general overview of version control, and
describes the version control systems that VC supports.  You can skip
this section if you are already familiar with the version control system
you want to use.

@menu
* Version Systems::  Supported version control back-end systems.
* VC Concepts::      Words and concepts related to version control.
1263
* Types of Log File::    The per-file VC log in contrast to the ChangeLog.
Dave Love's avatar
#  
Dave Love committed
1264 1265 1266 1267 1268 1269
@end menu

@node Version Systems
@subsubsection Supported Version Control Systems

@cindex back end (version control)
1270 1271
  VC currently works with six different version control systems or
``back ends'': CVS, GNU Arch, RCS, Meta-CVS, Subversion, and SCCS.
Dave Love's avatar
#  
Dave Love committed
1272 1273

@cindex CVS
1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311
  CVS is a free version control system that is used for the majority
of free software projects today.  It allows concurrent multi-user
development either locally or over the network.  Some of its
shortcomings, corrected by newer systems such as GNU Arch, are that it
lacks atomic commits or support for renaming files.  VC supports all
basic editing operations under CVS, but for some less common tasks you
still need to call CVS from the command line.  Note also that before
using CVS you must set up a repository, which is a subject too complex
to treat here.

@cindex GNU Arch
@cindex Arch
  GNU Arch is a new version control system that is designed for
distributed work.  It differs in many ways from old well-known
systems, such as CVS and RCS.  It supports different transports for
interoperating between users, offline operations, and it has good
branching and merging features.  It also supports atomic commits, and
history of file renaming and moving.  VC does not support all
operations provided by GNU Arch, so you must sometimes invoke it from
the command line, or use a specialized module.

@cindex RCS
  RCS is the free version control system around which VC was initially
built.  The VC commands are therefore conceptually closest to RCS.
Almost everything you can do with RCS can be done through VC.  You
cannot use RCS over the network though, and it only works at the level
of individual files, rather than projects.  You should use it if you
want a simple, yet reliable tool for handling individual files.

@cindex SVN
@cindex Subversion
  Subversion is a free version control system designed to be similar
to CVS but without CVS's problems.  Subversion supports atomic commits,
and versions directories, symbolic links, meta-data, renames, copies,
and deletes.  It can be used via http or via its own protocol.

@cindex MCVS
@cindex Meta-CVS
1312
  Meta-CVS is another attempt to solve problems arising in CVS.  It
1313 1314
supports directory structure versioning, improved branching and
merging, and use of symbolic links and meta-data in repositories.
Dave Love's avatar
#  
Dave Love committed
1315 1316 1317

@cindex SCCS
  SCCS is a proprietary but widely used version control system.  In
1318 1319 1320
terms of capabilities, it is the weakest of the six that VC supports.
VC compensates for certain features missing in SCCS (snapshots, for
example) by implementing them itself, but some other VC features, such
Richard M. Stallman's avatar
Richard M. Stallman committed
1321
as multiple branches, are not available with SCCS.  Since SCCS is
1322
non-free, not respecting its users freedom, you should not use it;
Richard M. Stallman's avatar
Richard M. Stallman committed
1323 1324 1325
use its free replacement CSSC instead.  But you should use CSSC only
if for some reason you cannot use RCS, or one of the higher-level
systems such as CVS or GNU Arch.
Dave Love's avatar
#  
Dave Love committed
1326

1327
In the following, we discuss mainly RCS, SCCS and CVS.  Nearly
1328
everything said about CVS applies to GNU Arch, Subversion and Meta-CVS
1329 1330
as well.

Dave Love's avatar
#  
Dave Love committed
1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381
@node VC Concepts
@subsubsection Concepts of Version Control

@cindex master file
@cindex registered file
   When a file is under version control, we also say that it is
@dfn{registered} in the version control system.  Each registered file
has a corresponding @dfn{master file} which represents the file's
present state plus its change history---enough to reconstruct the
current version or any earlier version.  Usually the master file also
records a @dfn{log entry} for each version, describing in words what was
changed in that version.

@cindex work file
@cindex checking out files
  The file that is maintained under version control is sometimes called
the @dfn{work file} corresponding to its master file.  You edit the work
file and make changes in it, as you would with an ordinary file.  (With
SCCS and RCS, you must @dfn{lock} the file before you start to edit it.)
After you are done with a set of changes, you @dfn{check the file in},
which records the changes in the master file, along with a log entry for
them.

  With CVS, there are usually multiple work files corresponding to a
single master file---often each user has his own copy.  It is also
possible to use RCS in this way, but this is not the usual way to use
RCS.

@cindex locking and version control
  A version control system typically has some mechanism to coordinate
between users who want to change the same file.  One method is
@dfn{locking} (analogous to the locking that Emacs uses to detect
simultaneous editing of a file, but distinct from it).  The other method
is to merge your changes with other people's changes when you check them
in.

  With version control locking, work files are normally read-only so
that you cannot change them.  You ask the version control system to make
a work file writable for you by locking it; only one user can do
this at any given time.  When you check in your changes, that unlocks
the file, making the work file read-only again.  This allows other users
to lock the file to make further changes.  SCCS always uses locking, and
RCS normally does.

  The other alternative for RCS is to let each user modify the work file
at any time.  In this mode, locking is not required, but it is
permitted; check-in is still the way to record a new version.

  CVS normally allows each user to modify his own copy of the work file
at any time, but requires merging with changes from other users at
check-in time.  However, CVS can also be set up to require locking.
1382
@iftex
1383
(@pxref{CVS Options,,,emacs-xtra, Specialized Emacs Features}).
1384 1385 1386 1387 1388
@end iftex
@ifnottex
(@pxref{CVS Options}).
@end ifnottex

Dave Love's avatar
#  
Dave Love committed
1389

1390 1391
@node Types of Log File
@subsubsection Types of Log File
1392
@cindex types of log file
1393
@cindex log File, types of
1394
@cindex version control log
1395

1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407