2 @c This is part of the GNU Emacs Lisp Reference Manual.
3 @c Copyright (C) 1990, 1991, 1992, 1993, 1994 Free Software Foundation, Inc.
4 @c See the file elisp.texi for copying conditions.
5 @setfilename ../info/backups
6 @node Backups and Auto-Saving, Buffers, Files, Top
7 @chapter Backups and Auto-Saving
9 Backup files and auto-save files are two methods by which Emacs tries
10 to protect the user from the consequences of crashes or of the user's
11 own errors. Auto-saving preserves the text from earlier in the current
12 editing session; backup files preserve file contents prior to the
16 * Backup Files:: How backup files are made; how their names are chosen.
17 * Auto-Saving:: How auto-save files are made; how their names are chosen.
18 * Reverting:: @code{revert-buffer}, and how to customize what it does.
25 A @dfn{backup file} is a copy of the old contents of a file you are
26 editing. Emacs makes a backup file the first time you save a buffer
27 into its visited file. Normally, this means that the backup file
28 contains the contents of the file as it was before the current editing
29 session. The contents of the backup file normally remain unchanged once
32 Backups are usually made by renaming the visited file to a new name.
33 Optionally, you can specify that backup files should be made by copying
34 the visited file. This choice makes a difference for files with
35 multiple names; it also can affect whether the edited file remains owned
36 by the original owner or becomes owned by the user editing it.
38 By default, Emacs makes a single backup file for each file edited.
39 You can alternatively request numbered backups; then each new backup
40 file gets a new name. You can delete old numbered backups when you
41 don't want them any more, or Emacs can delete them automatically.
44 * Making Backups:: How Emacs makes backup files, and when.
45 * Rename or Copy:: Two alternatives: renaming the old file or copying it.
46 * Numbered Backups:: Keeping multiple backups for each source file.
47 * Backup Names:: How backup file names are computed; customization.
51 @subsection Making Backup Files
54 This function makes a backup of the file visited by the current
55 buffer, if appropriate. It is called by @code{save-buffer} before
56 saving the buffer the first time.
59 @defvar buffer-backed-up
60 This buffer-local variable indicates whether this buffer's file has
61 been backed up on account of this buffer. If it is non-@code{nil}, then
62 the backup file has been written. Otherwise, the file should be backed
63 up when it is next saved (if backups are enabled). This is a
64 permanent local; @code{kill-local-variables} does not alter it.
67 @defopt make-backup-files
68 This variable determines whether or not to make backup files. If it
69 is non-@code{nil}, then Emacs creates a backup of each file when it is
70 saved for the first time.
72 The following example shows how to change the @code{make-backup-files}
73 variable only in the @file{RMAIL} buffer and not elsewhere. Setting it
74 @code{nil} stops Emacs from making backups of the @file{RMAIL} file,
75 which may save disk space. (You would put this code in your
80 (add-hook 'rmail-mode-hook
84 (setq make-backup-files nil))))
89 @defvar backup-enable-predicate
90 This variable's value is a function to be called on certain occasions to
91 decide whether a file should have backup files. The function receives
92 one argument, a file name to consider. If the function returns
93 @code{nil}, backups are disabled for that file. Otherwise, the other
94 variables in this section say whether and how to make backups.
96 The default value is this:
100 (or (< (length name) 5)
101 (not (string-equal "/tmp/"
102 (substring name 0 5)))))
106 @defvar backup-inhibited
107 If this variable is non-@code{nil}, backups are inhibited. It records
108 the result of testing @code{backup-enable-predicate} on the visited file
109 name. It can also coherently be used by other mechanisms that inhibit
110 backups based on which file is visited. This is a permanent local,
111 so that changing the major mode does not lose its value.
113 Major modes should not set this variable---they should set
114 @code{make-backup-files} instead.
118 @subsection Backup by Renaming or by Copying?
119 @cindex backup files, how to make them
121 There are two ways that Emacs can make a backup file:
125 Emacs can rename the original file so that it becomes a backup file, and
126 then write the buffer being saved into a new file. After this
127 procedure, any other names (i.e., hard links) of the original file now
128 refer to the backup file. The new file is owned by the user doing the
129 editing, and its group is the default for new files written by the user
133 Emacs can copy the original file into a backup file, and then overwrite
134 the original file with new contents. After this procedure, any other
135 names (i.e., hard links) of the original file still refer to the current
136 version of the file. The file's owner and group will be unchanged.
139 The first method, renaming, is the default.
141 The variable @code{backup-by-copying}, if non-@code{nil}, says to use
142 the second method, which is to copy the original file and overwrite it
143 with the new buffer contents. The variable @code{file-precious-flag},
144 if non-@code{nil}, also has this effect (as a sideline of its main
145 significance). @xref{Saving Buffers}.
147 @defvar backup-by-copying
148 If this variable is non-@code{nil}, Emacs always makes backup files by
152 The following two variables, when non-@code{nil}, cause the second
153 method to be used in certain special cases. They have no effect on the
154 treatment of files that don't fall into the special cases.
156 @defvar backup-by-copying-when-linked
157 If this variable is non-@code{nil}, Emacs makes backups by copying for
158 files with multiple names (hard links).
160 This variable is significant only if @code{backup-by-copying} is
161 @code{nil}, since copying is always used when that variable is
165 @defvar backup-by-copying-when-mismatch
166 If this variable is non-@code{nil}, Emacs makes backups by copying in cases
167 where renaming would change either the owner or the group of the file.
169 The value has no effect when renaming would not alter the owner or
170 group of the file; that is, for files which are owned by the user and
171 whose group matches the default for a new file created there by the
174 This variable is significant only if @code{backup-by-copying} is
175 @code{nil}, since copying is always used when that variable is
179 @node Numbered Backups
180 @subsection Making and Deleting Numbered Backup Files
182 If a file's name is @file{foo}, the names of its numbered backup
183 versions are @file{foo.~@var{v}~}, for various integers @var{v}, like
184 this: @file{foo.~1~}, @file{foo.~2~}, @file{foo.~3~}, @dots{},
185 @file{foo.~259~}, and so on.
187 @defopt version-control
188 This variable controls whether to make a single non-numbered backup
189 file or multiple numbered backups.
193 Make numbered backups if the visited file already has numbered backups;
197 Do not make numbered backups.
199 @item @var{anything else}
200 Make numbered backups.
204 The use of numbered backups ultimately leads to a large number of
205 backup versions, which must then be deleted. Emacs can do this
206 automatically or it can ask the user whether to delete them.
208 @defopt kept-new-versions
209 The value of this variable is the number of newest versions to keep
210 when a new numbered backup is made. The newly made backup is included
211 in the count. The default value is 2.
214 @defopt kept-old-versions
215 The value of this variable is the number of oldest versions to keep
216 when a new numbered backup is made. The default value is 2.
219 If there are backups numbered 1, 2, 3, 5, and 7, and both of these
220 variables have the value 2, then the backups numbered 1 and 2 are kept
221 as old versions and those numbered 5 and 7 are kept as new versions;
222 backup version 3 is excess. The function @code{find-backup-file-name}
223 (@pxref{Backup Names}) is responsible for determining which backup
224 versions to delete, but does not delete them itself.
226 @defopt trim-versions-without-asking
227 If this variable is non-@code{nil}, then saving a file deletes excess
228 backup versions silently. Otherwise, it asks the user whether to delete
232 @defopt dired-kept-versions
233 This variable specifies how many of the newest backup versions to keep
234 in the Dired command @kbd{.} (@code{dired-clean-directory}). That's the
235 same thing @code{kept-new-versions} specifies when you make a new backup
236 file. The default value is 2.
240 @subsection Naming Backup Files
242 The functions in this section are documented mainly because you can
243 customize the naming conventions for backup files by redefining them.
244 If you change one, you probably need to change the rest.
246 @defun backup-file-name-p filename
247 This function returns a non-@code{nil} value if @var{filename} is a
248 possible name for a backup file. A file with the name @var{filename}
249 need not exist; the function just checks the name.
253 (backup-file-name-p "foo")
257 (backup-file-name-p "foo~")
262 The standard definition of this function is as follows:
266 (defun backup-file-name-p (file)
267 "Return non-nil if FILE is a backup file \
268 name (numeric or not)..."
269 (string-match "~$" file))
274 Thus, the function returns a non-@code{nil} value if the file name ends
275 with a @samp{~}. (We use a backslash to split the documentation
276 string's first line into two lines in the text, but produce just one
277 line in the string itself.)
279 This simple expression is placed in a separate function to make it easy
280 to redefine for customization.
283 @defun make-backup-file-name filename
284 This function returns a string that is the name to use for a
285 non-numbered backup file for file @var{filename}. On Unix, this is just
286 @var{filename} with a tilde appended.
288 The standard definition of this function is as follows:
292 (defun make-backup-file-name (file)
293 "Create the non-numeric backup file name for FILE.
299 You can change the backup-file naming convention by redefining this
300 function. The following example redefines @code{make-backup-file-name}
301 to prepend a @samp{.} in addition to appending a tilde:
305 (defun make-backup-file-name (filename)
306 (concat "." filename "~"))
310 (make-backup-file-name "backups.texi")
311 @result{} ".backups.texi~"
316 @defun find-backup-file-name filename
317 This function computes the file name for a new backup file for
318 @var{filename}. It may also propose certain existing backup files for
319 deletion. @code{find-backup-file-name} returns a list whose @sc{car} is
320 the name for the new backup file and whose @sc{cdr} is a list of backup
321 files whose deletion is proposed.
323 Two variables, @code{kept-old-versions} and @code{kept-new-versions},
324 determine which backup versions should be kept. This function keeps
325 those versions by excluding them from the @sc{cdr} of the value.
326 @xref{Numbered Backups}.
328 In this example, the value says that @file{~rms/foo.~5~} is the name
329 to use for the new backup file, and @file{~rms/foo.~3~} is an ``excess''
330 version that the caller should consider deleting now.
334 (find-backup-file-name "~rms/foo")
335 @result{} ("~rms/foo.~5~" "~rms/foo.~3~")
341 @defun file-newest-backup filename
342 This function returns the name of the most recent backup file for
343 @var{filename}, or @code{nil} if that file has no backup files.
345 Some file comparison commands use this function so that they can
346 automatically compare a file with its most recent backup.
353 Emacs periodically saves all files that you are visiting; this is
354 called @dfn{auto-saving}. Auto-saving prevents you from losing more
355 than a limited amount of work if the system crashes. By default,
356 auto-saves happen every 300 keystrokes, or after around 30 seconds of
357 idle time. @xref{Auto-Save, Auto-Save, Auto-Saving: Protection Against
358 Disasters, emacs, The GNU Emacs Manual}, for information on auto-save
359 for users. Here we describe the functions used to implement auto-saving
360 and the variables that control them.
362 @defvar buffer-auto-save-file-name
363 This buffer-local variable is the name of the file used for
364 auto-saving the current buffer. It is @code{nil} if the buffer
365 should not be auto-saved.
369 buffer-auto-save-file-name
370 => "/xcssun/users/rms/lewis/#files.texi#"
375 @deffn Command auto-save-mode arg
376 When used interactively without an argument, this command is a toggle
377 switch: it turns on auto-saving of the current buffer if it is off, and
378 vice-versa. With an argument @var{arg}, the command turns auto-saving
379 on if the value of @var{arg} is @code{t}, a nonempty list, or a positive
380 integer. Otherwise, it turns auto-saving off.
383 @defun auto-save-file-name-p filename
384 This function returns a non-@code{nil} value if @var{filename} is a
385 string that could be the name of an auto-save file. It works based on
386 knowledge of the naming convention for auto-save files: a name that
387 begins and ends with hash marks (@samp{#}) is a possible auto-save file
388 name. The argument @var{filename} should not contain a directory part.
392 (make-auto-save-file-name)
393 @result{} "/xcssun/users/rms/lewis/#files.texi#"
396 (auto-save-file-name-p "#files.texi#")
400 (auto-save-file-name-p "files.texi")
405 The standard definition of this function is as follows:
409 (defun auto-save-file-name-p (filename)
410 "Return non-nil if FILENAME can be yielded by..."
411 (string-match "^#.*#$" filename))
415 This function exists so that you can customize it if you wish to
416 change the naming convention for auto-save files. If you redefine it,
417 be sure to redefine the function @code{make-auto-save-file-name}
421 @defun make-auto-save-file-name
422 This function returns the file name to use for auto-saving the current
423 buffer. This is just the file name with hash marks (@samp{#}) appended
424 and prepended to it. This function does not look at the variable
425 @code{auto-save-visited-file-name} (described below); you should check
426 that before calling this function.
430 (make-auto-save-file-name)
431 @result{} "/xcssun/users/rms/lewis/#backup.texi#"
435 The standard definition of this function is as follows:
439 (defun make-auto-save-file-name ()
440 "Return file name to use for auto-saves \
447 (file-name-directory buffer-file-name)
449 (file-name-nondirectory buffer-file-name)
452 (concat "#%" (buffer-name) "#"))))
456 This exists as a separate function so that you can redefine it to
457 customize the naming convention for auto-save files. Be sure to
458 change @code{auto-save-file-name-p} in a corresponding way.
461 @defvar auto-save-visited-file-name
462 If this variable is non-@code{nil}, Emacs auto-saves buffers in
463 the files they are visiting. That is, the auto-save is done in the same
464 file that you are editing. Normally, this variable is @code{nil}, so
465 auto-save files have distinct names that are created by
466 @code{make-auto-save-file-name}.
468 When you change the value of this variable, the value does not take
469 effect until the next time auto-save mode is reenabled in any given
470 buffer. If auto-save mode is already enabled, auto-saves continue to go
471 in the same file name until @code{auto-save-mode} is called again.
474 @defun recent-auto-save-p
475 This function returns @code{t} if the current buffer has been
476 auto-saved since the last time it was read in or saved.
479 @defun set-buffer-auto-saved
480 This function marks the current buffer as auto-saved. The buffer will
481 not be auto-saved again until the buffer text is changed again. The
482 function returns @code{nil}.
485 @defopt auto-save-interval
486 The value of this variable is the number of characters that Emacs
487 reads from the keyboard between auto-saves. Each time this many more
488 characters are read, auto-saving is done for all buffers in which it is
492 @defopt auto-save-timeout
493 The value of this variable is the number of seconds of idle time that
494 should cause auto-saving. Each time the user pauses for this long,
495 Emacs auto-saves any buffers that need it. (Actually, the specified
496 timeout is multiplied by a factor depending on the size of the current
500 @defvar auto-save-hook
501 This normal hook is run whenever an auto-save is about to happen.
504 @defopt auto-save-default
505 If this variable is non-@code{nil}, buffers that are visiting files
506 have auto-saving enabled by default. Otherwise, they do not.
509 @deffn Command do-auto-save &optional no-message
510 This function auto-saves all buffers that need to be auto-saved. It
511 saves all buffers for which auto-saving is enabled and that have been
512 changed since the previous auto-save.
514 Normally, if any buffers are auto-saved, a message that says
515 @samp{Auto-saving...} is displayed in the echo area while auto-saving is
516 going on. However, if @var{no-message} is non-@code{nil}, the message
520 @defun delete-auto-save-file-if-necessary
521 This function deletes the current buffer's auto-save file if
522 @code{delete-auto-save-files} is non-@code{nil}. It is called every
523 time a buffer is saved.
526 @defvar delete-auto-save-files
527 This variable is used by the function
528 @code{delete-auto-save-file-if-necessary}. If it is non-@code{nil},
529 Emacs deletes auto-save files when a true save is done (in the visited
530 file). This saves disk space and unclutters your directory.
533 @defun rename-auto-save-file
534 This function adjusts the current buffer's auto-save file name if the
535 visited file name has changed. It also renames an existing auto-save
536 file. If the visited file name has not changed, this function does
540 @defvar buffer-saved-size
541 The value of this buffer-local variable is the length of the current
542 buffer as of the last time it was read in, saved, or auto-saved. This is
543 used to detect a substantial decrease in size, and turn off auto-saving
546 If it is -1, that means auto-saving is temporarily shut off in this
547 buffer due to a substantial deletion. Explicitly saving the buffer
548 stores a positive value in this variable, thus reenabling auto-save.
549 Turning auto-save mode off or on also alters this variable.
555 If you have made extensive changes to a file and then change your mind
556 about them, you can get rid of them by reading in the previous version
557 of the file with the @code{revert-buffer} command. @xref{Reverting, ,
558 Reverting a Buffer, emacs, The GNU Emacs Manual}.
560 @deffn Command revert-buffer &optional check-auto-save noconfirm
561 This command replaces the buffer text with the text of the visited
562 file on disk. This action undoes all changes since the file was visited
565 If the argument @var{check-auto-save} is non-@code{nil}, and the
566 latest auto-save file is more recent than the visited file,
567 @code{revert-buffer} asks the user whether to use that instead.
568 Otherwise, it always uses the text of the visited file itself.
569 Interactively, @var{check-auto-save} is set if there is a numeric prefix
572 Normally, @code{revert-buffer} asks for confirmation before it changes
573 the buffer; but if the argument @var{noconfirm} is non-@code{nil},
574 @code{revert-buffer} does not ask for confirmation.
576 Reverting tries to preserve marker positions in the buffer by using the
577 replacement feature of @code{insert-file-contents}. If the buffer
578 contents and the file contents are identical before the revert
579 operation, reverting preserves all the markers. If they are not
580 identical, reverting does change the buffer; then it preserves the
581 markers in the unchanged text (if any) at the beginning and end of the
582 buffer. Preserving any additional markers would be problematical.
585 You can customize how @code{revert-buffer} does its work by setting
586 these variables---typically, as buffer-local variables.
588 @defvar revert-buffer-function
589 The value of this variable is the function to use to revert this buffer.
590 If non-@code{nil}, it is called as a function with no arguments to do
591 the work of reverting. If the value is @code{nil}, reverting works the
594 Modes such as Dired mode, in which the text being edited does not
595 consist of a file's contents but can be regenerated in some other
596 fashion, give this variable a buffer-local value that is a function to
597 regenerate the contents.
600 @defvar revert-buffer-insert-file-contents-function
601 The value of this variable, if non-@code{nil}, is the function to use to
602 insert contents when reverting this buffer. The function receives two
603 arguments, first the file name to use; second, @code{t} if the user has
604 asked to read the auto-save file.
607 @defvar before-revert-hook
608 This normal hook is run by @code{revert-buffer} before actually
609 inserting the modified contents---but only if
610 @code{revert-buffer-function} is @code{nil}.
612 Font Lock mode uses this hook to record that the buffer contents are no
616 @defvar after-revert-hook
617 This normal hook is run by @code{revert-buffer} after actually inserting
618 the modified contents---but only if @code{revert-buffer-function} is
621 Font Lock mode uses this hook to recompute the fonts for the updated
625 @deffn Command recover-file filename
626 This function visits @var{filename}, but gets the contents from its
627 last auto-save file. This is useful after the system has crashed, to
628 resume editing the same file without losing all the work done in the
631 An error is signaled if there is no auto-save file for @var{filename},
632 or if @var{filename} is newer than its auto-save file. If
633 @var{filename} does not exist, but its auto-save file does, then the
634 auto-save file is read as usual. This last situation may occur if you
635 visited a nonexistent file and never actually saved it.