* admin/gitmerge.el (gitmerge-missing):
[emacs.git] / doc / emacs / files.texi
blobdc59e13e081dccfd5f62129248419336b687f9bb
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985-1987, 1993-1995, 1997, 1999-2017 Free Software
3 @c Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Files
6 @chapter File Handling
7 @cindex files
9   The operating system stores data permanently in named @dfn{files}, so
10 most of the text you edit with Emacs comes from a file and is ultimately
11 stored in a file.
13   To edit a file, you must tell Emacs to read the file and prepare a
14 buffer containing a copy of the file's text.  This is called
15 @dfn{visiting} the file.  Editing commands apply directly to text in the
16 buffer; that is, to the copy inside Emacs.  Your changes appear in the
17 file itself only when you @dfn{save} the buffer back into the file.
19   In addition to visiting and saving files, Emacs can delete, copy,
20 rename, and append to files, keep multiple versions of them, and operate
21 on file directories.
23 @menu
24 * File Names::          How to type and edit file-name arguments.
25 * Visiting::            Visiting a file prepares Emacs to edit the file.
26 * Saving::              Saving makes your changes permanent.
27 * Reverting::           Reverting cancels all the changes not saved.
28 @ifnottex
29 * Autorevert::          Auto Reverting non-file buffers.
30 @end ifnottex
31 * Auto Save::           Auto Save periodically protects against loss of data.
32 * File Aliases::        Handling multiple names for one file.
33 * Directories::         Creating, deleting, and listing file directories.
34 * Comparing Files::     Finding where two files differ.
35 * Diff Mode::           Mode for editing file differences.
36 * Copying and Naming::  Copying, naming and renaming files.
37 * Misc File Ops::       Other things you can do on files.
38 * Compressed Files::    Accessing compressed files.
39 * File Archives::       Operating on tar, zip, jar etc. archive files.
40 * Remote Files::        Accessing files on other machines.
41 * Quoted File Names::   Quoting special characters in file names.
42 * File Name Cache::     Completion against a list of files you often use.
43 * File Conveniences::   Convenience Features for Finding Files.
44 * Filesets::            Handling sets of files.
45 @end menu
47 @node File Names
48 @section File Names
49 @cindex file names
51 @cindex default file name
52   Many Emacs commands that operate on a file require you to specify
53 the file name, using the minibuffer (@pxref{Minibuffer File}).
55   While in the minibuffer, you can use the usual completion and
56 history commands (@pxref{Minibuffer}).  Note that file name completion
57 ignores file names whose extensions appear in the variable
58 @code{completion-ignored-extensions} (@pxref{Completion Options}).
59 Note also that most commands use permissive completion with
60 confirmation for reading file names: you are allowed to submit a
61 nonexistent file name, but if you type @key{RET} immediately after
62 completing up to a nonexistent file name, Emacs prints
63 @samp{[Confirm]} and you must type a second @key{RET} to confirm.
64 @xref{Completion Exit}, for details.
66 Minibuffer history commands offer some special features for reading
67 file names, see @ref{Minibuffer History}.
69 @cindex default directory
70 @vindex default-directory
71 @vindex insert-default-directory
72   Each buffer has a @dfn{default directory}, stored in the
73 buffer-local variable @code{default-directory}.  Whenever Emacs reads
74 a file name using the minibuffer, it usually inserts the default
75 directory into the minibuffer as the initial contents.  You can
76 inhibit this insertion by changing the variable
77 @code{insert-default-directory} to @code{nil} (@pxref{Minibuffer
78 File}).  Regardless, Emacs always assumes that any relative file name
79 is relative to the default directory, e.g., entering a file name
80 without a directory specifies a file in the default directory.
82 @findex cd
83 @findex pwd
84   When you visit a file, Emacs sets @code{default-directory} in the
85 visiting buffer to the directory of its file.  When you create a new
86 buffer that is not visiting a file, via a command like @kbd{C-x b},
87 its default directory is usually copied from the buffer that was
88 current at the time (@pxref{Select Buffer}).  You can use the command
89 @kbd{M-x pwd} to see the value of @code{default-directory} in the
90 current buffer.  The command @kbd{M-x cd} prompts for a directory's
91 name, and sets the buffer's @code{default-directory} to that directory
92 (doing this does not change the buffer's file name, if any).
94   As an example, when you visit the file @file{/u/rms/gnu/gnu.tasks},
95 the default directory is set to @file{/u/rms/gnu/}.  If you invoke a
96 command that reads a file name, entering just @samp{foo} in the
97 minibuffer, with a directory omitted, specifies the file
98 @file{/u/rms/gnu/foo}; entering @samp{../.login} specifies
99 @file{/u/rms/.login}; and entering @samp{new/foo} specifies
100 @file{/u/rms/gnu/new/foo}.
102   When typing a file name into the minibuffer, you can make use of a
103 couple of shortcuts: a double slash ignores everything before the
104 second slash in the pair, and @samp{~/} is your home directory.
105 @xref{Minibuffer File}.
107 @cindex environment variables in file names
108 @cindex expansion of environment variables
109 @cindex @code{$} in file names
110   @anchor{File Names with $}The character @samp{$} is used to
111 substitute an environment variable into a file name.  The name of the
112 environment variable consists of all the alphanumeric characters after
113 the @samp{$}; alternatively, it can be enclosed in braces after the
114 @samp{$}.  For example, if you have used the shell command
115 @command{export FOO=rms/hacks} to set up an environment variable named
116 @env{FOO}, then both @file{/u/$FOO/test.c} and
117 @file{/u/$@{FOO@}/test.c} are abbreviations for
118 @file{/u/rms/hacks/test.c}.  If the environment variable is not
119 defined, no substitution occurs, so that the character @samp{$} stands
120 for itself.  Note that environment variables affect Emacs only if they
121 are applied before Emacs is started.
123   To access a file with @samp{$} in its name, if the @samp{$} causes
124 expansion, type @samp{$$}.  This pair is converted to a single
125 @samp{$} at the same time that variable substitution is performed for
126 a single @samp{$}.  Alternatively, quote the whole file name with
127 @samp{/:} (@pxref{Quoted File Names}).  File names which begin with a
128 literal @samp{~} should also be quoted with @samp{/:}.
130   You can include non-@acronym{ASCII} characters in file names.
131 @xref{File Name Coding}.
133 @node Visiting
134 @section Visiting Files
135 @cindex visiting files
136 @cindex open file
138 @table @kbd
139 @item C-x C-f
140 Visit a file (@code{find-file}).
141 @item C-x C-r
142 Visit a file for viewing, without allowing changes to it
143 (@code{find-file-read-only}).
144 @item C-x C-v
145 Visit a different file instead of the one visited last
146 (@code{find-alternate-file}).
147 @item C-x 4 f
148 Visit a file, in another window (@code{find-file-other-window}).  Don't
149 alter what is displayed in the selected window.
150 @item C-x 5 f
151 Visit a file, in a new frame (@code{find-file-other-frame}).  Don't
152 alter what is displayed in the selected frame.
153 @item M-x find-file-literally
154 Visit a file with no conversion of the contents.
155 @end table
157 @cindex files, visiting and saving
158 @cindex saving files
159   @dfn{Visiting} a file means reading its contents into an Emacs
160 buffer so you can edit them.  Emacs makes a new buffer for each file
161 that you visit.
163 @kindex C-x C-f
164 @findex find-file
165   To visit a file, type @kbd{C-x C-f} (@code{find-file}) and use the
166 minibuffer to enter the name of the desired file.  While in the
167 minibuffer, you can abort the command by typing @kbd{C-g}.  @xref{File
168 Names}, for details about entering file names into minibuffers.
170   If the specified file exists but the system does not allow you to
171 read it, an error message is displayed in the echo area.  Otherwise,
172 you can tell that @kbd{C-x C-f} has completed successfully by the
173 appearance of new text on the screen, and by the buffer name shown in
174 the mode line (@pxref{Mode Line}).  Emacs normally constructs the
175 buffer name from the file name, omitting the directory name.  For
176 example, a file named @file{/usr/rms/emacs.tex} is visited in a buffer
177 named @samp{emacs.tex}.  If there is already a buffer with that name,
178 Emacs constructs a unique name; the normal method is to add a suffix
179 based on the directory name (e.g., @samp{<rms>}, @samp{<tmp>},
180 and so on), but you can select other methods.  @xref{Uniquify}.
182 @cindex creating files
183   To create a new file, just visit it using the same command, @kbd{C-x
184 C-f}.  Emacs displays @samp{(New file)} in the echo area, but in other
185 respects behaves as if you had visited an existing empty file.
187 @cindex modified (buffer)
188   After visiting a file, the changes you make with editing commands are
189 made in the Emacs buffer.  They do not take effect in the visited
190 file, until you @dfn{save} the buffer (@pxref{Saving}).  If a buffer
191 contains changes that have not been saved, we say the buffer is
192 @dfn{modified}.  This implies that some changes will be lost if the
193 buffer is not saved.  The mode line displays two stars near the left
194 margin to indicate that the buffer is modified.
196   If you visit a file that is already in Emacs, @kbd{C-x C-f} switches
197 to the existing buffer instead of making another copy.  Before doing
198 so, it checks whether the file has changed since you last visited or
199 saved it.  If the file has changed, Emacs offers to reread it.
201 @vindex large-file-warning-threshold
202 @cindex file, warning when size is large
203 @cindex size of file, warning when visiting
204 @cindex maximum buffer size exceeded, error message
205   If you try to visit a file larger than
206 @code{large-file-warning-threshold} (the default is 10000000, which is
207 about 10 megabytes), Emacs asks you for confirmation first.  You can
208 answer @kbd{y} to proceed with visiting the file.  Note, however, that
209 Emacs cannot visit files that are larger than the maximum Emacs buffer
210 size, which is limited by the amount of memory Emacs can allocate and
211 by the integers that Emacs can represent (@pxref{Buffers}).  If you
212 try, Emacs displays an error message saying that the maximum buffer
213 size has been exceeded.
215 @cindex wildcard characters in file names
216 @vindex find-file-wildcards
217   If the file name you specify contains shell-style wildcard
218 characters, Emacs visits all the files that match it.  (On
219 case-insensitive filesystems, Emacs matches the wildcards disregarding
220 the letter case.)  Wildcards include @samp{?}, @samp{*}, and
221 @samp{[@dots{}]} sequences.  To enter the wild card @samp{?} in a file
222 name in the minibuffer, you need to type @kbd{C-q ?}.  @xref{Quoted
223 File Names}, for information on how to visit a file whose name
224 actually contains wildcard characters.  You can disable the wildcard
225 feature by customizing @code{find-file-wildcards}.
227 @kindex C-x C-v
228 @findex find-alternate-file
229   If you visit the wrong file unintentionally by typing its name
230 incorrectly, type @kbd{C-x C-v} (@code{find-alternate-file}) to visit
231 the file you really wanted.  @kbd{C-x C-v} is similar to @kbd{C-x
232 C-f}, but it kills the current buffer (after first offering to save it
233 if it is modified).  When @kbd{C-x C-v} reads the file name to visit,
234 it inserts the entire default file name in the buffer, with point just
235 after the directory part; this is convenient if you made a slight
236 error in typing the name.
238 @vindex find-file-run-dired
239   If you visit a file that is actually a directory, Emacs invokes
240 Dired, the Emacs directory browser.  @xref{Dired}.  You can disable
241 this behavior by setting the variable @code{find-file-run-dired} to
242 @code{nil}; in that case, it is an error to try to visit a directory.
244   Files which are actually collections of other files, or @dfn{file
245 archives}, are visited in special modes which invoke a Dired-like
246 environment to allow operations on archive members.  @xref{File
247 Archives}, for more about these features.
249   If you visit a file that the operating system won't let you modify,
250 or that is marked read-only, Emacs makes the buffer read-only too, so
251 that you won't go ahead and make changes that you'll have trouble
252 saving afterward.  You can make the buffer writable with @kbd{C-x C-q}
253 (@code{read-only-mode}).  @xref{Misc Buffer}.
255 @kindex C-x C-r
256 @findex find-file-read-only
257   If you want to visit a file as read-only in order to protect
258 yourself from entering changes accidentally, visit it with the command
259 @kbd{C-x C-r} (@code{find-file-read-only}) instead of @kbd{C-x C-f}.
261 @kindex C-x 4 f
262 @findex find-file-other-window
263   @kbd{C-x 4 f} (@code{find-file-other-window}) is like @kbd{C-x C-f}
264 except that the buffer containing the specified file is selected in another
265 window.  The window that was selected before @kbd{C-x 4 f} continues to
266 show the same buffer it was already showing.  If this command is used when
267 only one window is being displayed, that window is split in two, with one
268 window showing the same buffer as before, and the other one showing the
269 newly requested file.  @xref{Windows}.
271 @kindex C-x 5 f
272 @findex find-file-other-frame
273   @kbd{C-x 5 f} (@code{find-file-other-frame}) is similar, but opens a
274 new frame, or selects any existing frame showing the specified file.
275 @xref{Frames}.
277 @cindex file selection dialog
278   On graphical displays, there are two additional methods for visiting
279 files.  Firstly, when Emacs is built with a suitable GUI toolkit,
280 commands invoked with the mouse (by clicking on the menu bar or tool
281 bar) use the toolkit's standard file selection dialog instead of
282 prompting for the file name in the minibuffer.  On GNU/Linux and Unix
283 platforms, Emacs does this when built with GTK, LessTif, and Motif
284 toolkits; on MS-Windows and Mac, the GUI version does that by default.
285 For information on how to customize this, see @ref{Dialog Boxes}.
287   Secondly, Emacs supports drag and drop: dropping a file into an
288 ordinary Emacs window visits the file using that window.  As an
289 exception, dropping a file into a window displaying a Dired buffer
290 moves or copies the file into the displayed directory.  For details,
291 see @ref{Drag and Drop}, and @ref{Misc Dired Features}.
293   On text-mode terminals and on graphical displays when Emacs was
294 built without a GUI toolkit, you can visit files via the menu-bar
295 @samp{File} menu, which has a @samp{Visit New File} item.
297   Each time you visit a file, Emacs automatically scans its contents
298 to detect what character encoding and end-of-line convention it uses,
299 and converts these to Emacs's internal encoding and end-of-line
300 convention within the buffer.  When you save the buffer, Emacs
301 performs the inverse conversion, writing the file to disk with its
302 original encoding and end-of-line convention.  @xref{Coding Systems}.
304 @findex find-file-literally
305   If you wish to edit a file as a sequence of @acronym{ASCII}
306 characters with no special encoding or conversion, use the @kbd{M-x
307 find-file-literally} command.  This visits a file, like @kbd{C-x C-f},
308 but does not do format conversion (@pxref{Format Conversion,, Format
309 Conversion, elisp, the Emacs Lisp Reference Manual}), character code
310 conversion (@pxref{Coding Systems}), or automatic uncompression
311 (@pxref{Compressed Files}), and does not add a final newline because
312 of @code{require-final-newline} (@pxref{Customize Save}).  If you have
313 already visited the same file in the usual (non-literal) manner, this
314 command asks you whether to visit it literally instead.
316 @vindex find-file-hook
317 @vindex find-file-not-found-functions
318   Two special hook variables allow extensions to modify the operation
319 of visiting files.  Visiting a file that does not exist runs the
320 functions in @code{find-file-not-found-functions}; this variable holds
321 a list of functions, which are called one by one (with no arguments)
322 until one of them returns non-@code{nil}.  This is not a normal hook,
323 and the name ends in @samp{-functions} rather than @samp{-hook} to
324 indicate that fact.
326   Successful visiting of any file, whether existing or not, calls the
327 functions in @code{find-file-hook}, with no arguments.  This variable
328 is a normal hook.  In the case of a nonexistent file, the
329 @code{find-file-not-found-functions} are run first.  @xref{Hooks}.
331   There are several ways to specify automatically the major mode for
332 editing the file (@pxref{Choosing Modes}), and to specify local
333 variables defined for that file (@pxref{File Variables}).
335 @node Saving
336 @section Saving Files
338   @dfn{Saving} a buffer in Emacs means writing its contents back into the file
339 that was visited in the buffer.
341 @menu
342 * Save Commands::       Commands for saving files.
343 * Backup::              How Emacs saves the old version of your file.
344 * Customize Save::      Customizing the saving of files.
345 * Interlocking::        How Emacs protects against simultaneous editing
346                           of one file by two users.
347 * Shadowing: File Shadowing.  Copying files to ``shadows'' automatically.
348 * Time Stamps::         Emacs can update time stamps on saved files.
349 @end menu
351 @node Save Commands
352 @subsection Commands for Saving Files
354   These are the commands that relate to saving and writing files.
356 @table @kbd
357 @item C-x C-s
358 Save the current buffer to its file (@code{save-buffer}).
359 @item C-x s
360 Save any or all buffers to their files (@code{save-some-buffers}).
361 @item M-~
362 Forget that the current buffer has been changed (@code{not-modified}).
363 With prefix argument (@kbd{C-u}), mark the current buffer as changed.
364 @item C-x C-w
365 Save the current buffer with a specified file name (@code{write-file}).
366 @item M-x set-visited-file-name
367 Change the file name under which the current buffer will be saved.
368 @end table
370 @kindex C-x C-s
371 @findex save-buffer
372   When you wish to save the file and make your changes permanent, type
373 @kbd{C-x C-s} (@code{save-buffer}).  After saving is finished, @kbd{C-x C-s}
374 displays a message like this:
376 @example
377 Wrote /u/rms/gnu/gnu.tasks
378 @end example
380 @noindent
381 If the current buffer is not modified (no changes have been made in it
382 since the buffer was created or last saved), saving is not really
383 done, because it would have no effect.  Instead, @kbd{C-x C-s}
384 displays a message like this in the echo area:
386 @example
387 (No changes need to be saved)
388 @end example
390 With a prefix argument, @kbd{C-u C-x C-s}, Emacs also marks the buffer
391 to be backed up when the next save is done.  @xref{Backup}.
393 @kindex C-x s
394 @findex save-some-buffers
395   The command @kbd{C-x s} (@code{save-some-buffers}) offers to save any
396 or all modified buffers.  It asks you what to do with each buffer.  The
397 possible responses are analogous to those of @code{query-replace}:
399 @table @kbd
400 @item y
401 Save this buffer and ask about the rest of the buffers.
402 @item n
403 Don't save this buffer, but ask about the rest of the buffers.
404 @item !
405 Save this buffer and all the rest with no more questions.
406 @c following generates acceptable underfull hbox
407 @item @key{RET}
408 Terminate @code{save-some-buffers} without any more saving.
409 @item .
410 Save this buffer, then exit @code{save-some-buffers} without even asking
411 about other buffers.
412 @item C-r
413 View the buffer that you are currently being asked about.  When you exit
414 View mode, you get back to @code{save-some-buffers}, which asks the
415 question again.
416 @item d
417 Diff the buffer against its corresponding file, so you can see what
418 changes you would be saving.  This calls the command
419 @code{diff-buffer-with-file} (@pxref{Comparing Files}).
420 @item C-h
421 Display a help message about these options.
422 @end table
424 @noindent
425 @vindex save-some-buffers-default-predicate
426 You can customize the value of
427 @code{save-some-buffers-default-predicate} to control which buffers
428 Emacs will ask about.
430   @kbd{C-x C-c}, the key sequence to exit Emacs, invokes
431 @code{save-some-buffers} and therefore asks the same questions.
433 @kindex M-~
434 @findex not-modified
435   If you have changed a buffer but do not wish to save the changes,
436 you should take some action to prevent it.  Otherwise, each time you
437 use @kbd{C-x s} or @kbd{C-x C-c}, you are liable to save this buffer
438 by mistake.  One thing you can do is type @kbd{M-~}
439 (@code{not-modified}), which clears out the indication that the buffer
440 is modified.  If you do this, none of the save commands will believe
441 that the buffer needs to be saved.  (@samp{~} is often used as a
442 mathematical symbol for ``not''; thus @kbd{M-~} is ``not'', metafied.)
443 Alternatively, you can cancel all the changes made since the file was
444 visited or saved, by reading the text from the file again.  This is
445 called @dfn{reverting}.  @xref{Reverting}.  (You could also undo all
446 the changes by repeating the undo command @kbd{C-x u} until you have
447 undone all the changes; but reverting is easier.)
449 @findex set-visited-file-name
450   @kbd{M-x set-visited-file-name} alters the name of the file that the
451 current buffer is visiting.  It reads the new file name using the
452 minibuffer.  Then it marks the buffer as visiting that file name, and
453 changes the buffer name correspondingly.  @code{set-visited-file-name}
454 does not save the buffer in the newly visited file; it just alters the
455 records inside Emacs in case you do save later.  It also marks the
456 buffer as modified so that @kbd{C-x C-s} in that buffer
457 @emph{will} save.
459 @kindex C-x C-w
460 @findex write-file
461   If you wish to mark the buffer as visiting a different file and save
462 it right away, use @kbd{C-x C-w} (@code{write-file}).  This is
463 equivalent to @code{set-visited-file-name} followed by @kbd{C-x C-s},
464 except that @kbd{C-x C-w} asks for confirmation if the file exists.
465 @kbd{C-x C-s} used on a buffer that is not visiting a file has the
466 same effect as @kbd{C-x C-w}; that is, it reads a file name, marks the
467 buffer as visiting that file, and saves it there.  The default file
468 name in a buffer that is not visiting a file is made by combining the
469 buffer name with the buffer's default directory (@pxref{File Names}).
471   If the new file name implies a major mode, then @kbd{C-x C-w} switches
472 to that major mode, in most cases.  The command
473 @code{set-visited-file-name} also does this.  @xref{Choosing Modes}.
475   If Emacs is about to save a file and sees that the date of the latest
476 version on disk does not match what Emacs last read or wrote, Emacs
477 notifies you of this fact, because it probably indicates a problem caused
478 by simultaneous editing and requires your immediate attention.
479 @xref{Interlocking,, Simultaneous Editing}.
481 @node Backup
482 @subsection Backup Files
483 @cindex backup file
484 @vindex make-backup-files
485 @vindex vc-make-backup-files
487   On most operating systems, rewriting a file automatically destroys all
488 record of what the file used to contain.  Thus, saving a file from Emacs
489 throws away the old contents of the file---or it would, except that
490 Emacs carefully copies the old contents to another file, called the
491 @dfn{backup} file, before actually saving.
493   Emacs makes a backup for a file only the first time the file is
494 saved from a buffer.  No matter how many times you subsequently save
495 the file, its backup remains unchanged.  However, if you kill the
496 buffer and then visit the file again, a new backup file will be made.
498   For most files, the variable @code{make-backup-files} determines
499 whether to make backup files.  On most operating systems, its default
500 value is @code{t}, so that Emacs does write backup files.
502   For files managed by a version control system (@pxref{Version
503 Control}), the variable @code{vc-make-backup-files} determines whether
504 to make backup files.  By default it is @code{nil}, since backup files
505 are redundant when you store all the previous versions in a version
506 control system.
507 @iftex
508 @xref{General VC Options,,,emacs-xtra, Specialized Emacs Features}.
509 @end iftex
510 @ifnottex
511 @xref{General VC Options}.
512 @end ifnottex
514   At your option, Emacs can keep either a single backup for each file,
515 or make a series of numbered backup files for each file that you edit.
516 @xref{Backup Names}.
518 @vindex backup-enable-predicate
519 @vindex temporary-file-directory
520 @vindex small-temporary-file-directory
521   The default value of the @code{backup-enable-predicate} variable
522 prevents backup files being written for files in the directories used
523 for temporary files, specified by @code{temporary-file-directory} or
524 @code{small-temporary-file-directory}.
526   You can explicitly tell Emacs to make another backup file from a
527 buffer, even though that buffer has been saved before.  If you save
528 the buffer with @kbd{C-u C-x C-s}, the version thus saved will be made
529 into a backup file if you save the buffer again.  @kbd{C-u C-u C-x
530 C-s} saves the buffer, but first makes the previous file contents into
531 a new backup file.  @kbd{C-u C-u C-u C-x C-s} does both things: it
532 makes a backup from the previous contents, and arranges to make
533 another from the newly saved contents if you save again.
535 @vindex backup-directory-alist
536   You can customize the variable @code{backup-directory-alist} to
537 specify that files matching certain patterns should be backed up in
538 specific directories.  A typical use is to add an element @code{("."
539 . @var{dir})} to make all backups in the directory with absolute name
540 @var{dir}.  Emacs modifies the backup file names to avoid clashes
541 between files with the same names originating in different
542 directories.  Alternatively, adding, @code{("." . ".~")} would make
543 backups in the invisible subdirectory @file{.~} of the original file's
544 directory.  Emacs creates the directory, if necessary, to make the
545 backup.
547 @menu
548 * Names: Backup Names.          How backup files are named.
549 * Deletion: Backup Deletion.    Emacs deletes excess numbered backups.
550 * Copying: Backup Copying.      Backups can be made by copying or renaming.
551 @end menu
553 @node Backup Names
554 @subsubsection Single or Numbered Backups
555 @cindex backup file names
556 @cindex names of backup files
558 @cindex @file{~}, in names of backup files
559 @cindex tilde (@file{~}) at end of backup file name
560   When Emacs makes a backup file, its name is normally constructed by
561 appending @samp{~} to the file name being edited; thus, the backup
562 file for @file{eval.c} would be @file{eval.c~}.
564 @cindex @file{~/.emacs.d/%backup%~}
565   If access control stops Emacs from writing backup files under the
566 usual names, it writes the backup file as @file{~/.emacs.d/%backup%~}.
567 Only one such file can exist, so only the most recently made such
568 backup is available.
570   Emacs can also make @dfn{numbered backup files}.  Numbered backup
571 file names contain @samp{.~}, the number, and another @samp{~} after
572 the original file name.  Thus, the backup files of @file{eval.c} would
573 be called @file{eval.c.~1~}, @file{eval.c.~2~}, and so on, all the way
574 through names like @file{eval.c.~259~} and beyond.
576 @vindex version-control
577   The variable @code{version-control} determines whether to make
578 single backup files or multiple numbered backup files.  Its possible
579 values are:
581 @table @code
582 @item nil
583 Make numbered backups for files that have numbered backups already.
584 Otherwise, make single backups.  This is the default.
585 @item t
586 Make numbered backups.
587 @item never
588 Never make numbered backups; always make single backups.
589 @end table
591 @noindent
592 The usual way to set this variable is globally, through your init file
593 or the customization buffer.  However, you can set
594 @code{version-control} locally in an individual buffer to control the
595 making of backups for that buffer's file (@pxref{Locals}).  You can
596 have Emacs set @code{version-control} locally whenever you visit a
597 given file (@pxref{File Variables}).  Some modes, such as Rmail mode,
598 set this variable.
600 @cindex @env{VERSION_CONTROL} environment variable
601   If you set the environment variable @env{VERSION_CONTROL}, to tell
602 various GNU utilities what to do with backup files, Emacs also obeys the
603 environment variable by setting the Lisp variable @code{version-control}
604 accordingly at startup.  If the environment variable's value is @samp{t}
605 or @samp{numbered}, then @code{version-control} becomes @code{t}; if the
606 value is @samp{nil} or @samp{existing}, then @code{version-control}
607 becomes @code{nil}; if it is @samp{never} or @samp{simple}, then
608 @code{version-control} becomes @code{never}.
610 @vindex make-backup-file-name-function
611   If you set the variable @code{make-backup-file-name-function} to
612 a suitable Lisp function, you can override the usual way Emacs
613 constructs backup file names.
615 @node Backup Deletion
616 @subsubsection Automatic Deletion of Backups
618   To prevent excessive consumption of disk space, Emacs can delete numbered
619 backup versions automatically.  Generally Emacs keeps the first few backups
620 and the latest few backups, deleting any in between.  This happens every
621 time a new backup is made.
623 @vindex kept-old-versions
624 @vindex kept-new-versions
625   The two variables @code{kept-old-versions} and
626 @code{kept-new-versions} control this deletion.  Their values are,
627 respectively, the number of oldest (lowest-numbered) backups to keep
628 and the number of newest (highest-numbered) ones to keep, each time a
629 new backup is made.  The backups in the middle (excluding those oldest
630 and newest) are the excess middle versions---those backups are
631 deleted.  These variables' values are used when it is time to delete
632 excess versions, just after a new backup version is made; the newly
633 made backup is included in the count in @code{kept-new-versions}.  By
634 default, both variables are 2.
636 @vindex delete-old-versions
637   If @code{delete-old-versions} is @code{t}, Emacs deletes the excess
638 backup files silently.  If it is @code{nil}, the default, Emacs asks
639 you whether it should delete the excess backup versions.  If it has
640 any other value, then Emacs never automatically deletes backups.
642   Dired's @kbd{.} (Period) command can also be used to delete old versions.
643 @xref{Dired Deletion}.
645 @node Backup Copying
646 @subsubsection Copying vs.@: Renaming
648   Backup files can be made by copying the old file or by renaming it.
649 This makes a difference when the old file has multiple names (hard
650 links).  If the old file is renamed into the backup file, then the
651 alternate names become names for the backup file.  If the old file is
652 copied instead, then the alternate names remain names for the file
653 that you are editing, and the contents accessed by those names will be
654 the new contents.
656   The method of making a backup file may also affect the file's owner
657 and group.  If copying is used, these do not change.  If renaming is used,
658 you become the file's owner, and the file's group becomes the default
659 (different operating systems have different defaults for the group).
661 @vindex backup-by-copying
662 @vindex backup-by-copying-when-linked
663 @vindex backup-by-copying-when-mismatch
664 @vindex backup-by-copying-when-privileged-mismatch
665 @cindex file ownership, and backup
666 @cindex backup, and user-id
667   The choice of renaming or copying is made as follows:
669 @itemize
670 @item
671 If the variable @code{backup-by-copying} is non-@code{nil} (the
672 default is @code{nil}), use copying.
674 @item
675 Otherwise, if the variable @code{backup-by-copying-when-linked} is
676 non-@code{nil} (the default is @code{nil}), and the file has multiple
677 names, use copying.
679 @item
680 Otherwise, if the variable @code{backup-by-copying-when-mismatch} is
681 non-@code{nil} (the default is @code{t}), and renaming would change
682 the file's owner or group, use copying.
684 If you change @code{backup-by-copying-when-mismatch} to @code{nil},
685 Emacs checks the numeric user-id of the file's owner.  If this is
686 higher than @code{backup-by-copying-when-privileged-mismatch}, then it
687 behaves as though @code{backup-by-copying-when-mismatch} is
688 non-@code{nil} anyway.
690 @item
691 Otherwise, renaming is the default choice.
692 @end itemize
694   When a file is managed with a version control system (@pxref{Version
695 Control}), Emacs does not normally make backups in the usual way for
696 that file.  But check-in and check-out are similar in some ways to
697 making backups.  One unfortunate similarity is that these operations
698 typically break hard links, disconnecting the file name you visited from
699 any alternate names for the same file.  This has nothing to do with
700 Emacs---the version control system does it.
702 @node Customize Save
703 @subsection Customizing Saving of Files
705 @vindex require-final-newline
706   If the value of the variable @code{require-final-newline} is
707 @code{t}, saving or writing a file silently puts a newline at the end
708 if there isn't already one there.  If the value is @code{visit}, Emacs
709 adds a newline at the end of any file that doesn't have one, just
710 after it visits the file.  (This marks the buffer as modified, and you
711 can undo it.)  If the value is @code{visit-save}, Emacs adds such
712 newlines both on visiting and on saving.  If the value is @code{nil},
713 Emacs leaves the end of the file unchanged; any other non-@code{nil}
714 value means Emacs asks you whether to add a newline.  The default is
715 @code{nil}.
717 @vindex mode-require-final-newline
718   Some major modes are designed for specific kinds of files that are
719 always supposed to end in newlines.  Such major modes set the variable
720 @code{require-final-newline} to the value of
721 @code{mode-require-final-newline}, which defaults to @code{t}.  By
722 setting the latter variable, you can control how these modes handle
723 final newlines.
725 @vindex write-region-inhibit-fsync
726   Normally, when a program writes a file, the operating system briefly
727 caches the file's data in main memory before committing the data to
728 disk.  This can greatly improve performance; for example, when running
729 on laptops, it can avoid a disk spin-up each time a file is written.
730 However, it risks data loss if the operating system crashes before
731 committing the cache to disk.
733   To lessen this risk, Emacs can invoke the @code{fsync} system call
734 after saving a file.  Using @code{fsync} does not eliminate the risk
735 of data loss, partly because many systems do not implement
736 @code{fsync} properly, and partly because Emacs's file-saving
737 procedure typically relies also on directory updates that might not
738 survive a crash even if @code{fsync} works properly.
740   The @code{write-region-inhibit-fsync} variable controls whether
741 Emacs invokes @code{fsync} after saving a file.  The variable's
742 default value is @code{nil} when Emacs is interactive, and @code{t}
743 when Emacs runs in batch mode.
745   Emacs never uses @code{fsync} when writing auto-save files, as these
746 files might lose data anyway.
748 @node Interlocking
749 @subsection Protection against Simultaneous Editing
751 @cindex file dates
752 @cindex simultaneous editing
753   Simultaneous editing occurs when two users visit the same file, both
754 make changes, and then both save them.  If nobody is informed that
755 this is happening, whichever user saves first would later find that
756 his changes were lost.
758   On some systems, Emacs notices immediately when the second user starts
759 to change the file, and issues an immediate warning.  On all systems,
760 Emacs checks when you save the file, and warns if you are about to
761 overwrite another user's changes.  You can prevent loss of the other
762 user's work by taking the proper corrective action instead of saving the
763 file.
765 @findex ask-user-about-lock
766 @cindex locking files
767   When you make the first modification in an Emacs buffer that is
768 visiting a file, Emacs records that the file is @dfn{locked} by you.
769 (It does this by creating a specially-named symbolic link@footnote{If
770 your file system does not support symbolic links, a regular file is
771 used.} with special contents in the same directory.)  Emacs removes the lock
772 when you save the changes.  The idea is that the file is locked
773 whenever an Emacs buffer visiting it has unsaved changes.
775 @vindex create-lockfiles
776   You can prevent the creation of lock files by setting the variable
777 @code{create-lockfiles} to @code{nil}.  @strong{Caution:} by
778 doing so you will lose the benefits that this feature provides.
780 @cindex collision
781   If you begin to modify the buffer while the visited file is locked by
782 someone else, this constitutes a @dfn{collision}.  When Emacs detects a
783 collision, it asks you what to do, by calling the Lisp function
784 @code{ask-user-about-lock}.  You can redefine this function for the sake
785 of customization.  The standard definition of this function asks you a
786 question and accepts three possible answers:
788 @table @kbd
789 @item s
790 Steal the lock.  Whoever was already changing the file loses the lock,
791 and you gain the lock.
792 @item p
793 Proceed.  Go ahead and edit the file despite its being locked by someone else.
794 @item q
795 Quit.  This causes an error (@code{file-locked}), and the buffer
796 contents remain unchanged---the modification you were trying to make
797 does not actually take place.
798 @end table
800   If Emacs or the operating system crashes, this may leave behind lock
801 files which are stale, so you may occasionally get warnings about
802 spurious collisions.  When you determine that the collision is
803 spurious, just use @kbd{p} to tell Emacs to go ahead anyway.
805   Note that locking works on the basis of a file name; if a file has
806 multiple names, Emacs does not prevent two users from editing it
807 simultaneously under different names.
809   A lock file cannot be written in some circumstances, e.g., if Emacs
810 lacks the system permissions or cannot create lock files for some
811 other reason.  In these cases, Emacs can still detect the collision
812 when you try to save a file, by checking the file's last-modification
813 date.  If the file has changed since the last time Emacs visited or
814 saved it, that implies that changes have been made in some other way,
815 and will be lost if Emacs proceeds with saving.  Emacs then displays a
816 warning message and asks for confirmation before saving; answer
817 @kbd{yes} to save, and @kbd{no} or @kbd{C-g} cancel the save.
819   If you are notified that simultaneous editing has already taken
820 place, one way to compare the buffer to its file is the @kbd{M-x
821 diff-buffer-with-file} command.  @xref{Comparing Files}.
823 @node File Shadowing
824 @subsection Shadowing Files
825 @cindex shadow files
826 @cindex file shadows
827 @findex shadow-initialize
829 @table @kbd
830 @item M-x shadow-initialize
831 Set up file shadowing.
832 @item M-x shadow-define-literal-group
833 Declare a single file to be shared between sites.
834 @item M-x shadow-define-regexp-group
835 Make all files that match each of a group of files be shared between hosts.
836 @item M-x shadow-define-cluster @key{RET} @var{name} @key{RET}
837 Define a shadow file cluster @var{name}.
838 @item M-x shadow-copy-files
839 Copy all pending shadow files.
840 @item M-x shadow-cancel
841 Cancel the instruction to shadow some files.
842 @end table
844 You can arrange to keep identical @dfn{shadow} copies of certain files
845 in more than one place---possibly on different machines.  To do this,
846 first you must set up a @dfn{shadow file group}, which is a set of
847 identically-named files shared between a list of sites.  The file
848 group is permanent and applies to further Emacs sessions as well as
849 the current one.  Once the group is set up, every time you exit Emacs,
850 it will copy the file you edited to the other files in its group.  You
851 can also do the copying without exiting Emacs, by typing @kbd{M-x
852 shadow-copy-files}.
854 To set up a shadow file group, use @kbd{M-x
855 shadow-define-literal-group} or @kbd{M-x shadow-define-regexp-group}.
856 See their documentation strings for further information.
858 Before copying a file to its shadows, Emacs asks for confirmation.
859 You can answer ``no'' to bypass copying of this file, this time.  If
860 you want to cancel the shadowing permanently for a certain file, use
861 @kbd{M-x shadow-cancel} to eliminate or change the shadow file group.
863 A @dfn{shadow cluster} is a group of hosts that share directories, so
864 that copying to or from one of them is sufficient to update the file
865 on all of them.  Each shadow cluster has a name, and specifies the
866 network address of a primary host (the one we copy files to), and a
867 regular expression that matches the host names of all the other hosts
868 in the cluster.  You can define a shadow cluster with @kbd{M-x
869 shadow-define-cluster}.
871 @node Time Stamps
872 @subsection Updating Time Stamps Automatically
873 @cindex time stamps
874 @cindex modification dates
875 @cindex locale, date format
877 You can arrange to put a time stamp in a file, so that it is updated
878 automatically each time you edit and save the file.  The time stamp
879 must be in the first eight lines of the file, and you should insert it
880 like this:
882 @example
883 Time-stamp: <>
884 @end example
886 @noindent
887 or like this:
889 @example
890 Time-stamp: " "
891 @end example
893 @findex time-stamp
894   Then add the function @code{time-stamp} to the hook
895 @code{before-save-hook} (@pxref{Hooks}).  When you save the file, this
896 function then automatically updates the time stamp with the current
897 date and time.  You can also use the command @kbd{M-x time-stamp} to
898 update the time stamp manually.  By default the time stamp is
899 formatted according to your locale setting (@pxref{Environment}) and
900 time zone (@pxref{Time of Day,,, elisp, The Emacs Lisp Reference
901 Manual}).  For customizations, see the Custom group @code{time-stamp}.
903 @node Reverting
904 @section Reverting a Buffer
905 @findex revert-buffer
906 @cindex drastic changes
907 @cindex reread a file
909   If you have made extensive changes to a file-visiting buffer and
910 then change your mind, you can @dfn{revert} the changes and go back to
911 the saved version of the file.  To do this, type @kbd{M-x
912 revert-buffer}.  Since reverting unintentionally could lose a lot of
913 work, Emacs asks for confirmation first.
915   The @code{revert-buffer} command tries to position point in such a
916 way that, if the file was edited only slightly, you will be at
917 approximately the same part of the text as before.  But if you have
918 made major changes, point may end up in a totally different location.
920   Reverting marks the buffer as not modified.  It also clears the
921 buffer's undo history (@pxref{Undo}).  Thus, the reversion cannot be
922 undone---if you change your mind yet again, you can't use the undo
923 commands to bring the reverted changes back.
925   Some kinds of buffers that are not associated with files, such as
926 Dired buffers, can also be reverted.  For them, reverting means
927 recalculating their contents.  Buffers created explicitly with
928 @kbd{C-x b} cannot be reverted; @code{revert-buffer} reports an error
929 if you try.
931 @vindex revert-without-query
932   When you edit a file that changes automatically and frequently---for
933 example, a log of output from a process that continues to run---it may
934 be useful for Emacs to revert the file without querying you.  To
935 request this behavior, set the variable @code{revert-without-query} to
936 a list of regular expressions.  When a file name matches one of these
937 regular expressions, @code{find-file} and @code{revert-buffer} will
938 revert it automatically if it has changed---provided the buffer itself
939 is not modified.  (If you have edited the text, it would be wrong to
940 discard your changes.)
942 @cindex Global Auto-Revert mode
943 @cindex mode, Global Auto-Revert
944 @cindex Auto-Revert mode
945 @cindex mode, Auto-Revert
946 @findex global-auto-revert-mode
947 @findex auto-revert-mode
948 @findex auto-revert-tail-mode
949 @vindex auto-revert-interval
950 @vindex auto-revert-remote-files
951 @vindex auto-revert-verbose
952   You can also tell Emacs to revert buffers periodically.  To do this
953 for a specific buffer, enable the minor mode Auto-Revert mode by
954 typing @kbd{M-x auto-revert-mode}.  This automatically reverts the
955 current buffer every five seconds; you can change the interval through
956 the variable @code{auto-revert-interval}.  To do the same for all file
957 buffers, type @kbd{M-x global-auto-revert-mode} to enable Global
958 Auto-Revert mode.  These minor modes do not check or revert remote
959 files, because that is usually too slow.  This behavior can be changed
960 by setting the variable @code{auto-revert-remote-files} to non-@code{nil}.
962   One use of Auto-Revert mode is to ``tail'' a file such as a system
963 log, so that changes made to that file by other programs are
964 continuously displayed.  To do this, just move the point to the end of
965 the buffer, and it will stay there as the file contents change.
966 However, if you are sure that the file will only change by growing at
967 the end, use Auto-Revert Tail mode instead
968 (@code{auto-revert-tail-mode}).  It is more efficient for this.
969 Auto-Revert Tail mode works also for remote files.
971   When a buffer is auto-reverted, a message is generated.  This can be
972 suppressed by setting @code{auto-revert-verbose} to @code{nil}.
974   @xref{VC Undo}, for commands to revert to earlier versions of files
975 under version control.  @xref{VC Mode Line}, for Auto Revert
976 peculiarities when visiting files under version control.
978 @ifnottex
979 @include arevert-xtra.texi
980 @end ifnottex
982 @node Auto Save
983 @section Auto-Saving: Protection Against Disasters
984 @cindex Auto Save mode
985 @cindex mode, Auto Save
986 @cindex crashes
988   From time to time, Emacs automatically saves each visited file in a
989 separate file, without altering the file you actually use.  This is
990 called @dfn{auto-saving}.  It prevents you from losing more than a
991 limited amount of work if the system crashes.
993   When Emacs determines that it is time for auto-saving, it considers
994 each buffer, and each is auto-saved if auto-saving is enabled for it
995 and it has been changed since the last time it was auto-saved.  The
996 message @samp{Auto-saving...} is displayed in the echo area during
997 auto-saving, if any files are actually auto-saved.  Errors occurring
998 during auto-saving are caught so that they do not interfere with the
999 execution of commands you have been typing.
1001 @menu
1002 * Files: Auto Save Files.       The file where auto-saved changes are
1003                                   actually made until you save the file.
1004 * Control: Auto Save Control.   Controlling when and how often to auto-save.
1005 * Recover::                     Recovering text from auto-save files.
1006 @end menu
1008 @node Auto Save Files
1009 @subsection Auto-Save Files
1011   Auto-saving does not normally save in the files that you visited,
1012 because it can be very undesirable to save a change that you did not
1013 want to make permanent.  Instead, auto-saving is done in a different
1014 file called the @dfn{auto-save file}, and the visited file is changed
1015 only when you request saving explicitly (such as with @kbd{C-x C-s}).
1017   Normally, the auto-save file name is made by appending @samp{#} to the
1018 front and rear of the visited file name.  Thus, a buffer visiting file
1019 @file{foo.c} is auto-saved in a file @file{#foo.c#}.  Most buffers that
1020 are not visiting files are auto-saved only if you request it explicitly;
1021 when they are auto-saved, the auto-save file name is made by appending
1022 @samp{#} to the front and rear of buffer name, then
1023 adding digits and letters at the end for uniqueness.  For
1024 example, the @file{*mail*} buffer in which you compose messages to be
1025 sent might be auto-saved in a file named @file{#*mail*#704juu}.  Auto-save file
1026 names are made this way unless you reprogram parts of Emacs to do
1027 something different (the functions @code{make-auto-save-file-name} and
1028 @code{auto-save-file-name-p}).  The file name to be used for auto-saving
1029 in a buffer is calculated when auto-saving is turned on in that buffer.
1031 @cindex auto-save for remote files
1032 @vindex auto-save-file-name-transforms
1033   The variable @code{auto-save-file-name-transforms} allows a degree
1034 of control over the auto-save file name.  It lets you specify a series
1035 of regular expressions and replacements to transform the auto save
1036 file name.  The default value puts the auto-save files for remote
1037 files (@pxref{Remote Files}) into the temporary file directory on the
1038 local machine.
1040   When you delete a substantial part of the text in a large buffer, auto
1041 save turns off temporarily in that buffer.  This is because if you
1042 deleted the text unintentionally, you might find the auto-save file more
1043 useful if it contains the deleted text.  To reenable auto-saving after
1044 this happens, save the buffer with @kbd{C-x C-s}, or use @kbd{C-u 1 M-x
1045 auto-save-mode}.
1047 @vindex auto-save-visited-mode
1048   If you want auto-saving to be done in the visited file rather than
1049 in a separate auto-save file, enable the global minor mode
1050 @code{auto-save-visited-mode}.  In this mode, auto-saving is identical
1051 to explicit saving.  Note that this mode is orthogonal to the
1052 @code{auto-save} mode described above; you can enable both at the same
1053 time.  However, if @code{auto-save} mode is active in some buffer and
1054 the obsolete @code{auto-save-visited-file-name} variable is set to a
1055 non-@code{nil} value, that buffer won't be affected by
1056 @code{auto-save-visited-mode}.
1058   You can use the variable @code{auto-save-visited-interval} to
1059 customize the interval between auto-save operations in
1060 @code{auto-save-visited-mode}; by default it's five seconds.
1061 @code{auto-save-interval} and @code{auto-save-timeout} have no effect
1062 on @code{auto-save-visited-mode}.  @xref{Auto Save Control}, for
1063 details on these variables.
1065 @vindex delete-auto-save-files
1066   A buffer's auto-save file is deleted when you save the buffer in its
1067 visited file.  (You can inhibit this by setting the variable
1068 @code{delete-auto-save-files} to @code{nil}.)  Changing the visited
1069 file name with @kbd{C-x C-w} or @code{set-visited-file-name} renames
1070 any auto-save file to go with the new visited name.
1072 @node Auto Save Control
1073 @subsection Controlling Auto-Saving
1075 @vindex auto-save-default
1076 @findex auto-save-mode
1077   Each time you visit a file, auto-saving is turned on for that file's
1078 buffer if the variable @code{auto-save-default} is non-@code{nil} (but
1079 not in batch mode; @pxref{Initial Options}).  The default for this
1080 variable is @code{t}, so auto-saving is the usual practice for
1081 file-visiting buffers.  To toggle auto-saving in the current buffer,
1082 type @kbd{M-x auto-save-mode}.  Auto Save mode acts as a buffer-local
1083 minor mode (@pxref{Minor Modes}).
1085 @vindex auto-save-interval
1086   Emacs auto-saves periodically based on how many characters you have
1087 typed since the last auto-save.  The variable
1088 @code{auto-save-interval} specifies how many characters there are
1089 between auto-saves.  By default, it is 300.  Emacs doesn't accept
1090 values that are too small: if you customize @code{auto-save-interval}
1091 to a value less than 20, Emacs will behave as if the value is 20.
1093 @vindex auto-save-timeout
1094   Auto-saving also takes place when you stop typing for a while.  By
1095 default, it does this after 30 seconds of idleness (at this time,
1096 Emacs may also perform garbage collection; @pxref{Garbage
1097 Collection,,, elisp, The Emacs Lisp Reference Manual}).  To change
1098 this interval, customize the variable @code{auto-save-timeout}.  The
1099 actual time period is longer if the current buffer is long; this is a
1100 heuristic which aims to keep out of your way when you are editing long
1101 buffers, in which auto-save takes an appreciable amount of time.
1102 Auto-saving during idle periods accomplishes two things: first, it
1103 makes sure all your work is saved if you go away from the terminal for
1104 a while; second, it may avoid some auto-saving while you are actually
1105 typing.
1107 @vindex auto-save-visited-interval
1108   When @code{auto-save-visited-mode} is enabled, Emacs will auto-save
1109 file-visiting buffers after five seconds of idle time.  You can
1110 customize the variable @code{auto-save-visited-interval} to change the
1111 idle time interval.
1113   Emacs also does auto-saving whenever it gets a fatal error.  This
1114 includes killing the Emacs job with a shell command such as @samp{kill
1115 %emacs}, or disconnecting a phone line or network connection.
1117 @findex do-auto-save
1118   You can perform an auto-save explicitly with the command @kbd{M-x
1119 do-auto-save}.
1121 @node Recover
1122 @subsection Recovering Data from Auto-Saves
1124 @findex recover-file
1125   You can use the contents of an auto-save file to recover from a loss
1126 of data with the command @kbd{M-x recover-file @key{RET} @var{file}
1127 @key{RET}}.  This visits @var{file} and then (after your confirmation)
1128 restores the contents from its auto-save file @file{#@var{file}#}.
1129 You can then save with @kbd{C-x C-s} to put the recovered text into
1130 @var{file} itself.  For example, to recover file @file{foo.c} from its
1131 auto-save file @file{#foo.c#}, do:
1133 @example
1134 M-x recover-file @key{RET} foo.c @key{RET}
1135 yes @key{RET}
1136 C-x C-s
1137 @end example
1139   Before asking for confirmation, @kbd{M-x recover-file} displays a
1140 directory listing describing the specified file and the auto-save file,
1141 so you can compare their sizes and dates.  If the auto-save file
1142 is older, @kbd{M-x recover-file} does not offer to read it.
1144 @findex recover-session
1145   If Emacs or the computer crashes, you can recover all the files you
1146 were editing from their auto save files with the command @kbd{M-x
1147 recover-session}.  This first shows you a list of recorded interrupted
1148 sessions.  Move point to the one you choose, and type @kbd{C-c C-c}.
1150   Then @code{recover-session} asks about each of the files that were
1151 being edited during that session, asking whether to recover that file.
1152 If you answer @kbd{y}, it calls @code{recover-file}, which works in its
1153 normal fashion.  It shows the dates of the original file and its
1154 auto-save file, and asks once again whether to recover that file.
1156   When @code{recover-session} is done, the files you've chosen to
1157 recover are present in Emacs buffers.  You should then save them.  Only
1158 this---saving them---updates the files themselves.
1160 @vindex auto-save-list-file-prefix
1161   Emacs records information about interrupted sessions in files named
1162 @file{.saves-@var{pid}-@var{hostname}} in the directory
1163 @file{~/.emacs.d/auto-save-list/}.  This directory is determined by
1164 the variable @code{auto-save-list-file-prefix}.  If you set
1165 @code{auto-save-list-file-prefix} to @code{nil}, sessions are not
1166 recorded for recovery.
1168 @node File Aliases
1169 @section File Name Aliases
1170 @cindex symbolic links (visiting)
1171 @cindex hard links (visiting)
1173   Symbolic links and hard links both make it possible for several file
1174 names to refer to the same file.  Hard links are alternate names that
1175 refer directly to the file; all the names are equally valid, and no one
1176 of them is preferred.  By contrast, a symbolic link is a kind of defined
1177 alias: when @file{foo} is a symbolic link to @file{bar}, you can use
1178 either name to refer to the file, but @file{bar} is the real name, while
1179 @file{foo} is just an alias.  More complex cases occur when symbolic
1180 links point to directories.
1182 @vindex find-file-existing-other-name
1183 @vindex find-file-suppress-same-file-warnings
1184   Normally, if you visit a file which Emacs is already visiting under
1185 a different name, Emacs displays a message in the echo area and uses
1186 the existing buffer visiting that file.  This can happen on systems
1187 that support hard or symbolic links, or if you use a long file name on
1188 a system that truncates long file names, or on a case-insensitive file
1189 system.  You can suppress the message by setting the variable
1190 @code{find-file-suppress-same-file-warnings} to a non-@code{nil}
1191 value.  You can disable this feature entirely by setting the variable
1192 @code{find-file-existing-other-name} to @code{nil}: then if you visit
1193 the same file under two different names, you get a separate buffer for
1194 each file name.
1196 @vindex find-file-visit-truename
1197 @cindex truenames of files
1198 @cindex file truenames
1199   If the variable @code{find-file-visit-truename} is non-@code{nil},
1200 then the file name recorded for a buffer is the file's @dfn{truename}
1201 (made by replacing all symbolic links with their target names), rather
1202 than the name you specify.  Setting @code{find-file-visit-truename} also
1203 implies the effect of @code{find-file-existing-other-name}.
1205 @cindex directory name abbreviation
1206 @vindex directory-abbrev-alist
1207   Sometimes, a directory is ordinarily accessed through a symbolic
1208 link, and you may want Emacs to preferentially show its linked
1209 name.  To do this, customize @code{directory-abbrev-alist}.  Each
1210 element in this list should have the form @code{(@var{from}
1211 . @var{to})}, which means to replace @var{from} with @var{to} whenever
1212 @var{from} appears in a directory name.  The @var{from} string is a
1213 regular expression (@pxref{Regexps}).  It is matched against directory
1214 names anchored at the first character, and should start with @samp{\`}
1215 (to support directory names with embedded newlines, which would defeat
1216 @samp{^}).  The @var{to} string should be an ordinary absolute
1217 directory name pointing to the same directory.  Do not use @samp{~} to
1218 stand for a home directory in the @var{to} string; Emacs performs
1219 these substitutions separately.  Here's an example, from a system on
1220 which @file{/home/fsf} is normally accessed through a symbolic link
1221 named @file{/fsf}:
1223 @example
1224 (("\\`/home/fsf" . "/fsf"))
1225 @end example
1227 @node Directories
1228 @section File Directories
1230 @cindex file directory
1231 @cindex directory listing
1232   The file system groups files into @dfn{directories}.  A @dfn{directory
1233 listing} is a list of all the files in a directory.  Emacs provides
1234 commands to create and delete directories, and to make directory
1235 listings in brief format (file names only) and verbose format (sizes,
1236 dates, and authors included).  Emacs also includes a directory browser
1237 feature called Dired; see @ref{Dired}.
1239 @table @kbd
1240 @item C-x C-d @var{dir-or-pattern} @key{RET}
1241 Display a brief directory listing (@code{list-directory}).
1242 @item C-u C-x C-d @var{dir-or-pattern} @key{RET}
1243 Display a verbose directory listing.
1244 @item M-x make-directory @key{RET} @var{dirname} @key{RET}
1245 Create a new directory named @var{dirname}.
1246 @item M-x delete-directory @key{RET} @var{dirname} @key{RET}
1247 Delete the directory named @var{dirname}.  If it isn't empty,
1248 you will be asked whether you want to delete it recursively.
1249 @end table
1251 @findex list-directory
1252 @kindex C-x C-d
1253   The command to display a directory listing is @kbd{C-x C-d}
1254 (@code{list-directory}).  It reads using the minibuffer a file name
1255 which is either a directory to be listed or a wildcard-containing
1256 pattern for the files to be listed.  For example,
1258 @example
1259 C-x C-d /u2/emacs/etc @key{RET}
1260 @end example
1262 @noindent
1263 lists all the files in directory @file{/u2/emacs/etc}.  Here is an
1264 example of specifying a file name pattern:
1266 @example
1267 C-x C-d /u2/emacs/src/*.c @key{RET}
1268 @end example
1270   Normally, @kbd{C-x C-d} displays a brief directory listing containing
1271 just file names.  A numeric argument (regardless of value) tells it to
1272 make a verbose listing including sizes, dates, and owners (like
1273 @samp{ls -l}).
1275 @vindex list-directory-brief-switches
1276 @vindex list-directory-verbose-switches
1277   The text of a directory listing is mostly obtained by running
1278 @code{ls} in an inferior process.  Two Emacs variables control the
1279 switches passed to @code{ls}: @code{list-directory-brief-switches} is
1280 a string giving the switches to use in brief listings (@code{"-CF"} by
1281 default), and @code{list-directory-verbose-switches} is a string
1282 giving the switches to use in a verbose listing (@code{"-l"} by
1283 default).
1285   In verbose directory listings, Emacs adds information about the
1286 amount of free space on the disk that contains the directory.
1288   The command @kbd{M-x delete-directory} prompts for a directory's name
1289 using the minibuffer, and deletes the directory if it is empty.  If
1290 the directory is not empty, you will be asked whether you want to
1291 delete it recursively.  On systems that have a ``Trash'' (or ``Recycle
1292 Bin'') feature, you can make this command move the specified directory
1293 to the Trash instead of deleting it outright, by changing the variable
1294 @code{delete-by-moving-to-trash} to @code{t}.  @xref{Misc File Ops},
1295 for more information about using the Trash.
1297 @node Comparing Files
1298 @section Comparing Files
1299 @cindex comparing files
1301 @findex diff
1302 @vindex diff-switches
1303   The command @kbd{M-x diff} prompts for two file names, using the
1304 minibuffer, and displays the differences between the two files in a
1305 buffer named @file{*diff*}.  This works by running the @command{diff}
1306 program, using options taken from the variable @code{diff-switches}.
1307 The value of @code{diff-switches} should be a string; the default is
1308 @code{"-u"} to specify a unified context diff.
1309 @c Note that the actual name of the info file is diffutils.info,
1310 @c but it adds a dir entry for diff too.
1311 @c On older systems, only "info diff" works, not "info diffutils".
1312 @xref{Top,, Diff, diffutils, Comparing and Merging Files}, for more
1313 information about the @command{diff} program.
1315   The output of the @code{diff} command is shown using a major mode
1316 called Diff mode.  @xref{Diff Mode}.
1318 @findex diff-backup
1319   The command @kbd{M-x diff-backup} compares a specified file with its
1320 most recent backup.  If you specify the name of a backup file,
1321 @code{diff-backup} compares it with the source file that it is a
1322 backup of.  In all other respects, this behaves like @kbd{M-x diff}.
1324 @findex diff-buffer-with-file
1325   The command @kbd{M-x diff-buffer-with-file} compares a specified
1326 buffer with its corresponding file.  This shows you what changes you
1327 would make to the file if you save the buffer.
1329 @findex compare-windows
1330   The command @kbd{M-x compare-windows} compares the text in the
1331 current window with that in the window that was the selected window
1332 before you selected the current one.  (For more information about
1333 windows in Emacs, @ref{Windows}.)  Comparison starts at point in each
1334 window, after pushing each initial point value on the mark ring in its
1335 respective buffer.  Then it moves point forward in each window, one
1336 character at a time, until it reaches characters that don't match.
1337 Then the command exits.
1339   If point in the two windows is followed by non-matching text when
1340 the command starts, @kbd{M-x compare-windows} tries heuristically to
1341 advance up to matching text in the two windows, and then exits.  So if
1342 you use @kbd{M-x compare-windows} repeatedly, each time it either
1343 skips one matching range or finds the start of another.
1345 @vindex compare-ignore-case
1346 @vindex compare-ignore-whitespace
1347   With a numeric argument, @code{compare-windows} ignores changes in
1348 whitespace.  If the variable @code{compare-ignore-case} is
1349 non-@code{nil}, the comparison ignores differences in case as well.
1350 If the variable @code{compare-ignore-whitespace} is non-@code{nil},
1351 @code{compare-windows} normally ignores changes in whitespace, and a
1352 prefix argument turns that off.
1354 @cindex Smerge mode
1355 @findex smerge-mode
1356 @cindex failed merges
1357 @cindex merges, failed
1358 @cindex comparing 3 files (@code{diff3})
1359   You can use @kbd{M-x smerge-mode} to turn on Smerge mode, a minor
1360 mode for editing output from the @command{diff3} program.  This is
1361 typically the result of a failed merge from a version control system
1362 update outside VC, due to conflicting changes to a file.  Smerge
1363 mode provides commands to resolve conflicts by selecting specific
1364 changes.
1366 @iftex
1367 @xref{Emerge,,, emacs-xtra, Specialized Emacs Features},
1368 @end iftex
1369 @ifnottex
1370 @xref{Emerge},
1371 @end ifnottex
1372 for the Emerge facility, which provides a powerful interface for
1373 merging files.
1375 @node Diff Mode
1376 @section Diff Mode
1377 @cindex Diff mode
1378 @findex diff-mode
1379 @cindex patches, editing
1381   Diff mode is a major mode used for the output of @kbd{M-x diff} and
1382 other similar commands.  This kind of output is called a @dfn{patch},
1383 because it can be passed to the @command{patch} command to
1384 automatically apply the specified changes.  To select Diff mode
1385 manually, type @kbd{M-x diff-mode}.
1387 @cindex hunk, diff
1388   The changes specified in a patch are grouped into @dfn{hunks}, which
1389 are contiguous chunks of text that contain one or more changed lines.
1390 Hunks can also include unchanged lines to provide context for the
1391 changes.  Each hunk is preceded by a @dfn{hunk header}, which
1392 specifies the old and new line numbers at which the hunk occurs.  Diff
1393 mode highlights each hunk header, to distinguish it from the actual
1394 contents of the hunk.
1396 @vindex diff-update-on-the-fly
1397   You can edit a Diff mode buffer like any other buffer.  (If it is
1398 read-only, you need to make it writable first.  @xref{Misc Buffer}.)
1399 Whenever you change a hunk, Diff mode attempts to automatically
1400 correct the line numbers in the hunk headers, to ensure that the patch
1401 remains correct.  To disable automatic line number correction,
1402 change the variable @code{diff-update-on-the-fly} to @code{nil}.
1404   Diff mode treats each hunk as an error message, similar to
1405 Compilation mode.  Thus, you can use commands such as @kbd{C-x `} to
1406 visit the corresponding source locations.  @xref{Compilation Mode}.
1408   In addition, Diff mode provides the following commands to navigate,
1409 manipulate and apply parts of patches:
1411 @table @kbd
1412 @item M-n
1413 @findex diff-hunk-next
1414 Move to the next hunk-start (@code{diff-hunk-next}).
1416 @findex diff-auto-refine-mode
1417 @cindex mode, Diff Auto-Refine
1418 @cindex Diff Auto-Refine mode
1419 This command has a side effect: it @dfn{refines} the hunk you move to,
1420 highlighting its changes with better granularity.  To disable this
1421 feature, type @kbd{M-x diff-auto-refine-mode} to toggle off the minor
1422 mode Diff Auto-Refine mode.  To disable Diff Auto Refine mode by
1423 default, add this to your init file (@pxref{Hooks}):
1425 @example
1426 (add-hook 'diff-mode-hook
1427           (lambda () (diff-auto-refine-mode -1)))
1428 @end example
1430 @item M-p
1431 @findex diff-hunk-prev
1432 Move to the previous hunk-start (@code{diff-hunk-prev}).  Like
1433 @kbd{M-n}, this has the side-effect of refining the hunk you move to,
1434 unless you disable Diff Auto-Refine mode.
1436 @item M-@}
1437 @findex diff-file-next
1438 Move to the next file-start, in a multi-file patch
1439 (@code{diff-file-next}).
1441 @item M-@{
1442 @findex diff-file-prev
1443 Move to the previous file-start, in a multi-file patch
1444 (@code{diff-file-prev}).
1446 @item M-k
1447 @findex diff-hunk-kill
1448 Kill the hunk at point (@code{diff-hunk-kill}).
1450 @item M-K
1451 @findex diff-file-kill
1452 In a multi-file patch, kill the current file part.
1453 (@code{diff-file-kill}).
1455 @item C-c C-a
1456 @findex diff-apply-hunk
1457 @cindex patches, applying
1458 Apply this hunk to its target file (@code{diff-apply-hunk}).  With a
1459 prefix argument of @kbd{C-u}, revert this hunk.
1461 @item C-c C-b
1462 @findex diff-refine-hunk
1463 Highlight the changes of the hunk at point with a finer granularity
1464 (@code{diff-refine-hunk}).  This allows you to see exactly which parts
1465 of each changed line were actually changed.
1467 @item C-c C-c
1468 @findex diff-goto-source
1469 Go to the source file and line corresponding to this hunk
1470 (@code{diff-goto-source}).
1472 @item C-c C-e
1473 @findex diff-ediff-patch
1474 Start an Ediff session with the patch (@code{diff-ediff-patch}).
1475 @xref{Top, Ediff, Ediff, ediff, The Ediff Manual}.
1477 @item C-c C-n
1478 @findex diff-restrict-view
1479 Restrict the view to the current hunk (@code{diff-restrict-view}).
1480 @xref{Narrowing}.  With a prefix argument of @kbd{C-u}, restrict the
1481 view to the current file of a multiple-file patch.  To widen again,
1482 use @kbd{C-x n w} (@code{widen}).
1484 @item C-c C-r
1485 @findex diff-reverse-direction
1486 Reverse the direction of comparison for the entire buffer
1487 (@code{diff-reverse-direction}).
1489 @item C-c C-s
1490 @findex diff-split-hunk
1491 Split the hunk at point (@code{diff-split-hunk}).  This is for
1492 manually editing patches, and only works with the @dfn{unified diff
1493 format} produced by the @option{-u} or @option{--unified} options to
1494 the @command{diff} program.  If you need to split a hunk in the
1495 @dfn{context diff format} produced by the @option{-c} or
1496 @option{--context} options to @command{diff}, first convert the buffer
1497 to the unified diff format with @kbd{C-c C-u}.
1499 @item C-c C-d
1500 @findex diff-unified->context
1501 Convert the entire buffer to the @dfn{context diff format}
1502 (@code{diff-unified->context}).  With a prefix argument, convert only
1503 the text within the region.
1505 @item C-c C-u
1506 @findex diff-context->unified
1507 Convert the entire buffer to unified diff format
1508 (@code{diff-context->unified}).  With a prefix argument, convert
1509 unified format to context format.  When the mark is active, convert
1510 only the text within the region.
1512 @item C-c C-w
1513 @findex diff-ignore-whitespace-hunk
1514 Re-diff the current hunk, disregarding changes in whitespace
1515 (@code{diff-ignore-whitespace-hunk}).
1517 @item C-x 4 A
1518 @findex diff-add-change-log-entries-other-window
1519 @findex add-change-log-entry-other-window@r{, in Diff mode}
1520 Generate a ChangeLog entry, like @kbd{C-x 4 a} does (@pxref{Change
1521 Log}), for each one of the hunks
1522 (@code{diff-add-change-log-entries-other-window}).  This creates a
1523 skeleton of the log of changes that you can later fill with the actual
1524 descriptions of the changes.  @kbd{C-x 4 a} itself in Diff mode
1525 operates on behalf of the current hunk's file, but gets the function
1526 name from the patch itself.  This is useful for making log entries for
1527 functions that are deleted by the patch.
1528 @end table
1530 @c Trailing whitespace is NOT shown by default.
1531 @c Emacs's dir-locals file enables this (for some reason).
1532 @cindex trailing whitespace, in patches
1533 @findex diff-delete-trailing-whitespace
1534   Patches sometimes include trailing whitespace on modified lines, as
1535 an unintentional and undesired change.  There are two ways to deal
1536 with this problem.  Firstly, if you enable Whitespace mode in a Diff
1537 buffer (@pxref{Useless Whitespace}), it automatically highlights
1538 trailing whitespace in modified lines.  Secondly, you can use the
1539 command @kbd{M-x diff-delete-trailing-whitespace}, which searches for
1540 trailing whitespace in the lines modified by the patch, and removes
1541 that whitespace in both the patch and the patched source file(s).
1542 This command does not save the modifications that it makes, so you can
1543 decide whether to save the changes (the list of modified files is
1544 displayed in the echo area).  With a prefix argument, it tries to
1545 modify the original source files rather than the patched source files.
1547 @node Copying and Naming
1548 @section Copying, Naming and Renaming Files
1550   Emacs has several commands for copying, naming, and renaming files.
1551 All of them read two file names @var{old} and @var{new} using the
1552 minibuffer, and then copy or adjust a file's name accordingly; they do
1553 not accept wildcard file names.
1555 In all these commands, if the argument @var{new} is just a directory
1556 name, the real new name is in that directory, with the same
1557 non-directory component as @var{old}.  For example, the command
1558 @w{@kbd{M-x rename-file @key{RET} ~/foo @key{RET} /tmp/ @key{RET}}}
1559 renames @file{~/foo} to @file{/tmp/foo}.  On GNU and other POSIX-like
1560 systems, directory names end in @samp{/}.  @xref{Directory Names,,,
1561 elisp, the Emacs Lisp Reference Manual}.
1563 All these commands ask for confirmation when the new file name already
1564 exists.
1566 @findex copy-file
1567 @cindex copying files
1568   @kbd{M-x copy-file} copies the contents of the file @var{old} to the
1569 file @var{new}.
1571 @findex copy-directory
1572   @kbd{M-x copy-directory} copies directories, similar to the
1573 @command{cp -r} shell command.  If @var{new} is a directory name, it
1574 creates a copy of the @var{old} directory and puts it in @var{new}.
1575 Otherwise it copies all the contents of @var{old} into a new directory
1576 named @var{new}.
1578 @cindex renaming files
1579 @findex rename-file
1580   @kbd{M-x rename-file} renames file @var{old} as @var{new}.  If the
1581 file name @var{new} already exists, you must confirm with @kbd{yes} or
1582 renaming is not done; this is because renaming causes the old meaning
1583 of the name @var{new} to be lost.  If @var{old} and @var{new} are on
1584 different file systems, the file @var{old} is copied and deleted.
1586 @ifnottex
1587   If a file is under version control (@pxref{Version Control}), you
1588 should rename it using @w{@kbd{M-x vc-rename-file}} instead of
1589 @w{@kbd{M-x rename-file}}.  @xref{VC Delete/Rename}.
1590 @end ifnottex
1592 @findex add-name-to-file
1593 @cindex hard links (creation)
1594   @kbd{M-x add-name-to-file} adds an additional name to an existing
1595 file without removing the old name.  The new name is created as a hard
1596 link to the existing file.  The new name must belong on the same file
1597 system that the file is on.  On MS-Windows, this command works only if
1598 the file resides in an NTFS file system.  On MS-DOS, and some remote
1599 system types, it works by copying the file.
1601 @findex make-symbolic-link
1602 @cindex symbolic links (creation)
1603   @kbd{M-x make-symbolic-link} creates a symbolic link named
1604 @var{new}, which points at @var{target}.  The effect is that future
1605 attempts to open file @var{new} will refer to whatever file is named
1606 @var{target} at the time the opening is done, or will get an error if
1607 the name @var{target} is nonexistent at that time.  This command does
1608 not expand the argument @var{target}, so that it allows you to specify
1609 a relative name as the target of the link.  However, this command
1610 does expand leading @samp{~} in @var{target} so that you can easily
1611 specify home directories, and strips leading @samp{/:} so that you can
1612 specify relative names beginning with literal @samp{~} or @samp{/:}.
1613 @xref{Quoted File Names}.  On MS-Windows, this command works only on
1614 MS Windows Vista and later.  When @var{new} is remote,
1615 it works depending on the system type.
1617 @node Misc File Ops
1618 @section Miscellaneous File Operations
1620   Emacs has commands for performing many other operations on files.
1621 All operate on one file; they do not accept wildcard file names.
1623 @findex delete-file
1624 @cindex deletion (of files)
1625   @kbd{M-x delete-file} prompts for a file and deletes it.  If you are
1626 deleting many files in one directory, it may be more convenient to use
1627 Dired rather than @code{delete-file}.  @xref{Dired Deletion}.
1629 @cindex trash
1630 @cindex recycle bin
1631   @kbd{M-x move-file-to-trash} moves a file into the system
1632 @dfn{Trash} (or @dfn{Recycle Bin}).  This is a facility available on
1633 most operating systems; files that are moved into the Trash can be
1634 brought back later if you change your mind.
1636 @vindex delete-by-moving-to-trash
1637   By default, Emacs deletion commands do @emph{not} use the Trash.  To
1638 use the Trash (when it is available) for common deletion commands,
1639 change the variable @code{delete-by-moving-to-trash} to @code{t}.
1640 This affects the commands @kbd{M-x delete-file} and @kbd{M-x
1641 delete-directory} (@pxref{Directories}), as well as the deletion
1642 commands in Dired (@pxref{Dired Deletion}).  Supplying a prefix
1643 argument to @kbd{M-x delete-file} or @kbd{M-x delete-directory} makes
1644 them delete outright, instead of using the Trash, regardless of
1645 @code{delete-by-moving-to-trash}.
1647 @ifnottex
1648   If a file is under version control (@pxref{Version Control}), you
1649 should delete it using @kbd{M-x vc-delete-file} instead of @kbd{M-x
1650 delete-file}.  @xref{VC Delete/Rename}.
1651 @end ifnottex
1653 @kindex C-x i
1654 @findex insert-file
1655   @kbd{M-x insert-file} (also @kbd{C-x i}) inserts a copy of the
1656 contents of the specified file into the current buffer at point,
1657 leaving point unchanged before the contents.  The position after the
1658 inserted contents is added to the mark ring, without activating the
1659 mark (@pxref{Mark Ring}).
1661 @findex insert-file-literally
1662   @kbd{M-x insert-file-literally} is like @kbd{M-x insert-file},
1663 except the file is inserted literally: it is treated as a sequence
1664 of @acronym{ASCII} characters with no special encoding or conversion,
1665 similar to the @kbd{M-x find-file-literally} command
1666 (@pxref{Visiting}).
1668 @findex write-region
1669   @kbd{M-x write-region} is the inverse of @kbd{M-x insert-file}; it
1670 copies the contents of the region into the specified file.  @kbd{M-x
1671 append-to-file} adds the text of the region to the end of the
1672 specified file.  @xref{Accumulating Text}.  The variable
1673 @code{write-region-inhibit-fsync} applies to these commands, as well
1674 as saving files; see @ref{Customize Save}.
1676 @findex set-file-modes
1677 @cindex file modes
1678 @cindex file permissions
1679   @kbd{M-x set-file-modes} reads a file name followed by a @dfn{file
1680 mode}, and applies that file mode to the specified file.  File modes,
1681 also called @dfn{file permissions}, determine whether a file can be
1682 read, written to, or executed, and by whom.  This command reads file
1683 modes using the same symbolic or octal format accepted by the
1684 @command{chmod} command; for instance, @samp{u+x} means to add
1685 execution permission for the user who owns the file.  It has no effect
1686 on operating systems that do not support file modes.  @code{chmod} is a
1687 convenience alias for this function.
1689 @node Compressed Files
1690 @section Accessing Compressed Files
1691 @cindex compression
1692 @cindex uncompression
1693 @cindex Auto Compression mode
1694 @cindex mode, Auto Compression
1695 @pindex gzip
1697   Emacs automatically uncompresses compressed files when you visit
1698 them, and automatically recompresses them if you alter them and save
1699 them.  Emacs recognizes compressed files by their file names.  File
1700 names ending in @samp{.gz} indicate a file compressed with
1701 @code{gzip}.  Other endings indicate other compression programs.
1703   Automatic uncompression and compression apply to all the operations in
1704 which Emacs uses the contents of a file.  This includes visiting it,
1705 saving it, inserting its contents into a buffer, loading it, and byte
1706 compiling it.
1708 @findex auto-compression-mode
1709 @vindex auto-compression-mode
1710   To disable this feature, type the command @kbd{M-x
1711 auto-compression-mode}.  You can disable it permanently by
1712 customizing the variable @code{auto-compression-mode}.
1714 @node File Archives
1715 @section File Archives
1716 @cindex mode, tar
1717 @cindex Tar mode
1718 @cindex file archives
1720   A file whose name ends in @samp{.tar} is normally an @dfn{archive}
1721 made by the @code{tar} program.  Emacs views these files in a special
1722 mode called Tar mode which provides a Dired-like list of the contents
1723 (@pxref{Dired}).  You can move around through the list just as you
1724 would in Dired, and visit the subfiles contained in the archive.
1725 However, not all Dired commands are available in Tar mode.
1727   If Auto Compression mode is enabled (@pxref{Compressed Files}), then
1728 Tar mode is used also for compressed archives---files with extensions
1729 @samp{.tgz}, @code{.tar.Z} and @code{.tar.gz}.
1731   The keys @kbd{e}, @kbd{f} and @key{RET} all extract a component file
1732 into its own buffer.  You can edit it there, and if you save the
1733 buffer, the edited version will replace the version in the Tar buffer.
1734 Clicking with the mouse on the file name in the Tar buffer does
1735 likewise.  @kbd{v} extracts a file into a buffer in View mode
1736 (@pxref{View Mode}).  @kbd{o} extracts the file and displays it in
1737 another window, so you could edit the file and operate on the archive
1738 simultaneously.
1740   The @kbd{I} key adds a new (regular) file to the archive.  The file
1741 is initially empty, but can readily be edited using the commands
1742 above.  The command inserts the new file before the current one, so
1743 that using it on the topmost line of the Tar buffer makes the new file
1744 the first one in the archive, and using it at the end of the buffer
1745 makes it the last one.
1747   @kbd{d} marks a file for deletion when you later use @kbd{x}, and
1748 @kbd{u} unmarks a file, as in Dired.  @kbd{C} copies a file from the
1749 archive to disk and @kbd{R} renames a file within the archive.
1750 @kbd{g} reverts the buffer from the archive on disk.  The keys
1751 @kbd{M}, @kbd{G}, and @kbd{O} change the file's permission bits,
1752 group, and owner, respectively.
1754   Saving the Tar buffer writes a new version of the archive to disk with
1755 the changes you made to the components.
1757   You don't need the @code{tar} program to use Tar mode---Emacs reads
1758 the archives directly.  However, accessing compressed archives
1759 requires the appropriate uncompression program.
1761 @cindex Archive mode
1762 @cindex mode, archive
1763 @cindex @code{arc}
1764 @cindex @code{jar}
1765 @cindex @code{rar}
1766 @cindex @code{zip}
1767 @cindex @code{lzh}
1768 @cindex @code{zoo}
1769 @cindex @code{7z}
1770 @pindex arc
1771 @pindex jar
1772 @pindex zip
1773 @pindex rar
1774 @pindex lzh
1775 @pindex zoo
1776 @pindex 7z
1777 @cindex Java class archives
1778 @cindex unzip archives
1779   A separate but similar Archive mode is used for @code{arc},
1780 @code{jar}, @code{lzh}, @code{zip}, @code{rar}, @code{7z}, and
1781 @code{zoo} archives, as well as @code{exe} files that are
1782 self-extracting executables.
1784   The key bindings of Archive mode are similar to those in Tar mode,
1785 with the addition of the @kbd{m} key which marks a file for subsequent
1786 operations, and @kbd{M-@key{DEL}} which unmarks all the marked files.
1787 Also, the @kbd{a} key toggles the display of detailed file
1788 information, for those archive types where it won't fit in a single
1789 line.  Operations such as renaming a subfile, or changing its mode or
1790 owner, are supported only for some of the archive formats.
1792   Unlike Tar mode, Archive mode runs the archiving programs to unpack
1793 and repack archives.  However, you don't need these programs to look
1794 at the archive table of contents, only to extract or manipulate the
1795 subfiles in the archive.  Details of the program names and their
1796 options can be set in the @samp{Archive} Customize group
1797 (@pxref{Customization Groups}).
1799 @node Remote Files
1800 @section Remote Files
1802 @cindex Tramp
1803 @cindex FTP
1804 @cindex remote file access
1805   You can refer to files on other machines using a special file name
1806 syntax:
1808 @example
1809 @group
1810 /@var{method}:@var{host}:@var{filename}
1811 /@var{method}:@var{user}@@@var{host}:@var{filename}
1812 /@var{method}:@var{user}@@@var{host}#@var{port}:@var{filename}
1813 @end group
1814 @end example
1816 @noindent
1817 To carry out this request, Emacs uses a remote-login program such as
1818 @command{ssh}.
1819 You must always specify in the file name which method to use---for
1820 example, @file{/ssh:@var{user}@@@var{host}:@var{filename}} uses
1821 @command{ssh}.  When you specify the pseudo method @var{-} in the file
1822 name, Emacs chooses the method as follows:
1824 @enumerate
1825 @item
1826 If the host name starts with @samp{ftp.} (with dot), Emacs uses FTP.
1827 @item
1828 If the user name is @samp{ftp} or @samp{anonymous}, Emacs uses FTP.
1829 @item
1830 If the variable @code{tramp-default-method} is set to @samp{ftp},
1831 Emacs uses FTP.
1832 @item
1833 If @command{ssh-agent} is running, Emacs uses @command{scp}.
1834 @item
1835 Otherwise, Emacs uses @command{ssh}.
1836 @end enumerate
1838 @cindex disabling remote files
1839 @noindent
1840 You can entirely turn off the remote file name feature by setting the
1841 variable @code{tramp-mode} to @code{nil}.  You can turn off the
1842 feature in individual cases by quoting the file name with @samp{/:}
1843 (@pxref{Quoted File Names}).
1845 @cindex ange-ftp
1846   Remote file access through FTP is handled by the Ange-FTP package, which
1847 is documented in the following.  Remote file access through the other
1848 methods is handled by the Tramp package, which has its own manual.
1849 @xref{Top, The Tramp Manual,, tramp, The Tramp Manual}.
1851 @vindex ange-ftp-default-user
1852 @cindex user name for remote file access
1853   When the Ange-FTP package is used, Emacs logs in through FTP using
1854 the name @var{user}, if that is specified in the remote file name.  If
1855 @var{user} is unspecified, Emacs logs in using your user name on the
1856 local system; but if you set the variable @code{ange-ftp-default-user}
1857 to a string, that string is used instead.  When logging in, Emacs may
1858 also ask for a password.
1860 @cindex backups for remote files
1861 @vindex ange-ftp-make-backup-files
1862   For performance reasons, Emacs does not make backup files for files
1863 accessed via FTP by default.  To make it do so, change the variable
1864 @code{ange-ftp-make-backup-files} to a non-@code{nil} value.
1866   By default, auto-save files for remote files are made in the
1867 temporary file directory on the local machine, as specified by the
1868 variable @code{auto-save-file-name-transforms}.  @xref{Auto Save
1869 Files}.
1871 @cindex anonymous FTP
1872 @vindex ange-ftp-generate-anonymous-password
1873   To visit files accessible by anonymous FTP, you use special user
1874 names @samp{anonymous} or @samp{ftp}.  Passwords for these user names
1875 are handled specially.  The variable
1876 @code{ange-ftp-generate-anonymous-password} controls what happens: if
1877 the value of this variable is a string, then that string is used as
1878 the password; if non-@code{nil} (the default), then the value of
1879 @code{user-mail-address} is used; if @code{nil}, then Emacs prompts
1880 you for a password as usual (@pxref{Passwords}).
1882 @cindex firewall, and accessing remote files
1883 @cindex gateway, and remote file access with @code{ange-ftp}
1884 @vindex ange-ftp-smart-gateway
1885 @vindex ange-ftp-gateway-host
1886   Sometimes you may be unable to access files on a remote machine
1887 because a @dfn{firewall} in between blocks the connection for security
1888 reasons.  If you can log in on a @dfn{gateway} machine from which the
1889 target files @emph{are} accessible, and whose FTP server supports
1890 gatewaying features, you can still use remote file names; all you have
1891 to do is specify the name of the gateway machine by setting the
1892 variable @code{ange-ftp-gateway-host}, and set
1893 @code{ange-ftp-smart-gateway} to @code{t}.  Otherwise you may be able
1894 to make remote file names work, but the procedure is complex.  You can
1895 read the instructions by typing @kbd{M-x finder-commentary @key{RET}
1896 ange-ftp @key{RET}}.
1898 @node Quoted File Names
1899 @section Quoted File Names
1901 @cindex quoting file names
1902 @cindex file names, quote special characters
1903   You can @dfn{quote} an absolute file name to prevent special
1904 characters and syntax in it from having their special effects.
1905 The way to do this is to add @samp{/:} at the beginning.
1907   For example, you can quote a local file name which appears remote, to
1908 prevent it from being treated as a remote file name.  Thus, if you have
1909 a directory named @file{/foo:} and a file named @file{bar} in it, you
1910 can refer to that file in Emacs as @samp{/:/foo:/bar}.
1912   If you want to quote only special characters in the local part of a
1913 remote file name, you can quote just the local part.
1914 @samp{/ssh:baz:/:/foo:/bar} refers to the file @file{bar} of directory
1915 @file{/foo:} on the host @file{baz}.
1917   @samp{/:} can also prevent @samp{~} from being treated as a special
1918 character for a user's home directory.  For example, @file{/:/tmp/~hack}
1919 refers to a file whose name is @file{~hack} in directory @file{/tmp}.
1921   Quoting with @samp{/:} is also a way to enter in the minibuffer a
1922 file name that contains @samp{$}.  In order for this to work, the
1923 @samp{/:} must be at the beginning of the minibuffer contents.  (You
1924 can also double each @samp{$}; see @ref{File Names with $}.)
1926   You can also quote wildcard characters with @samp{/:}, for visiting.
1927 For example, @file{/:/tmp/foo*bar} visits the file
1928 @file{/tmp/foo*bar}.
1930   Another method of getting the same result is to enter
1931 @file{/tmp/foo[*]bar}, which is a wildcard specification that matches
1932 only @file{/tmp/foo*bar}.  However, in many cases there is no need to
1933 quote the wildcard characters because even unquoted they give the
1934 right result.  For example, if the only file name in @file{/tmp} that
1935 starts with @samp{foo} and ends with @samp{bar} is @file{foo*bar},
1936 then specifying @file{/tmp/foo*bar} will visit only
1937 @file{/tmp/foo*bar}.
1939 @node File Name Cache
1940 @section File Name Cache
1942 @cindex file name caching
1943 @cindex cache of file names
1944 @pindex find
1945 @kindex C-TAB
1946 @findex file-cache-minibuffer-complete
1947   You can use the @dfn{file name cache} to make it easy to locate a
1948 file by name, without having to remember exactly where it is located.
1949 When typing a file name in the minibuffer, @kbd{C-@key{TAB}}
1950 (@code{file-cache-minibuffer-complete}) completes it using the file
1951 name cache.  If you repeat @kbd{C-@key{TAB}}, that cycles through the
1952 possible completions of what you had originally typed.  (However, note
1953 that the @kbd{C-@key{TAB}} character cannot be typed on most text
1954 terminals.)
1956   The file name cache does not fill up automatically.  Instead, you
1957 load file names into the cache using these commands:
1959 @findex file-cache-add-directory
1960 @table @kbd
1961 @item M-x file-cache-add-directory @key{RET} @var{directory} @key{RET}
1962 Add each file name in @var{directory} to the file name cache.
1963 @item M-x file-cache-add-directory-using-find @key{RET} @var{directory} @key{RET}
1964 Add each file name in @var{directory} and all of its nested
1965 subdirectories to the file name cache.
1966 @item M-x file-cache-add-directory-using-locate @key{RET} @var{directory} @key{RET}
1967 Add each file name in @var{directory} and all of its nested
1968 subdirectories to the file name cache, using @command{locate} to find
1969 them all.
1970 @item M-x file-cache-add-directory-list @key{RET} @var{variable} @key{RET}
1971 Add each file name in each directory listed in @var{variable} to the
1972 file name cache.  @var{variable} should be a Lisp variable whose value
1973 is a list of directories, like @code{load-path}.
1974 @item M-x file-cache-clear-cache @key{RET}
1975 Clear the cache; that is, remove all file names from it.
1976 @end table
1978   The file name cache is not persistent: it is kept and maintained
1979 only for the duration of the Emacs session.  You can view the contents
1980 of the cache with the @code{file-cache-display} command.
1982 @node File Conveniences
1983 @section Convenience Features for Finding Files
1985   In this section, we introduce some convenient facilities for finding
1986 recently-opened files, reading file names from a buffer, and viewing
1987 image files.
1989 @findex recentf-mode
1990 @vindex recentf-mode
1991 @findex recentf-save-list
1992 @findex recentf-edit-list
1993   If you enable Recentf mode, with @kbd{M-x recentf-mode}, the
1994 @samp{File} menu includes a submenu containing a list of recently
1995 opened files.  @kbd{M-x recentf-save-list} saves the current
1996 @code{recent-file-list} to a file, and @kbd{M-x recentf-edit-list}
1997 edits it.
1999 @c FIXME partial-completion-mode (complete.el) is obsolete.
2000   The @kbd{M-x ffap} command generalizes @code{find-file} with more
2001 powerful heuristic defaults (@pxref{FFAP}), often based on the text at
2002 point.  Partial Completion mode offers other features extending
2003 @code{find-file}, which can be used with @code{ffap}.
2004 @xref{Completion Options}.
2006 @findex image-mode
2007 @findex image-toggle-display
2008 @findex image-next-file
2009 @findex image-previous-file
2010 @cindex images, viewing
2011   Visiting image files automatically selects Image mode.  In this
2012 major mode, you can type @kbd{C-c C-c} (@code{image-toggle-display})
2013 to toggle between displaying the file as an image in the Emacs buffer,
2014 and displaying its underlying text (or raw byte) representation.
2015 Additionally you can type @kbd{C-c C-x} (@code{image-toggle-hex-display})
2016 to toggle between displaying the file as an image in the Emacs buffer,
2017 and displaying it in hex representation.
2018 Displaying the file as an image works only if Emacs is compiled with
2019 support for displaying such images.  If the displayed image is wider
2020 or taller than the frame, the usual point motion keys (@kbd{C-f},
2021 @kbd{C-p}, and so forth) cause different parts of the image to be
2022 displayed.  You can press @kbd{n} (@code{image-next-file}) and @kbd{p}
2023 (@code{image-previous-file}) to visit the next image file and the
2024 previous image file in the same directory, respectively.
2026 @findex image-toggle-animation
2027 @findex image-next-frame
2028 @findex image-previous-frame
2029 @findex image-goto-frame
2030 @findex image-increase-speed
2031 @findex image-decrease-speed
2032 @findex image-reset-speed
2033 @findex image-reverse-speed
2034 @vindex image-animate-loop
2035 @cindex image animation
2036 @cindex animated images
2037   If the image can be animated, the command @key{RET}
2038 (@code{image-toggle-animation}) starts or stops the animation.
2039 Animation plays once, unless the option @code{image-animate-loop} is
2040 non-@code{nil}.  With @kbd{f} (@code{image-next-frame}) and @kbd{b}
2041 (@code{image-previous-frame}) you can step through the individual
2042 frames.  Both commands accept a numeric prefix to step through several
2043 frames at once.  You can go to a specific frame with @kbd{F}
2044 (@code{image-goto-frame}).  Frames are indexed from 1.  Typing @kbd{a
2045 +} (@code{image-increase-speed}) increases the speed of the animation,
2046 @kbd{a -} (@code{image-decrease-speed}) decreases it, and @kbd{a r}
2047 (@code{image-reverse-speed}) reverses it.  The command @kbd{a 0}
2048 (@code{image-reset-speed}) resets the speed to the original value.
2050 @cindex ImageMagick support
2051 @vindex imagemagick-enabled-types
2052 @vindex imagemagick-types-inhibit
2053   If Emacs was compiled with support for the ImageMagick library, it
2054 can use ImageMagick to render a wide variety of images.  The variable
2055 @code{imagemagick-enabled-types} lists the image types that Emacs may
2056 render using ImageMagick; each element in the list should be an
2057 internal ImageMagick name for an image type, as a symbol or an
2058 equivalent string (e.g., @code{BMP} for @file{.bmp} images).  To
2059 enable ImageMagick for all possible image types, change
2060 @code{imagemagick-enabled-types} to @code{t}.  The variable
2061 @code{imagemagick-types-inhibit} lists the image types which should
2062 never be rendered using ImageMagick, regardless of the value of
2063 @code{imagemagick-enabled-types} (the default list includes types like
2064 @code{C} and @code{HTML}, which ImageMagick can render as an image
2065 but Emacs should not).  To disable ImageMagick entirely, change
2066 @code{imagemagick-types-inhibit} to @code{t}.
2068 @findex thumbs-mode
2069 @findex mode, thumbs
2070   The Image-Dired package can also be used to view images as
2071 thumbnails.  @xref{Image-Dired}.
2073 @node Filesets
2074 @section Filesets
2075 @cindex filesets
2076 @cindex sets of files
2078 @findex filesets-init
2079   If you regularly edit a certain group of files, you can define them
2080 as a @dfn{fileset}.  This lets you perform certain operations, such as
2081 visiting, @code{query-replace}, and shell commands on all the files at
2082 once.  To make use of filesets, you must first add the expression
2083 @code{(filesets-init)} to your init file (@pxref{Init File}).  This
2084 adds a @samp{Filesets} sub-menu to the menu bar's @samp{File} menu.
2086 @findex filesets-add-buffer
2087 @findex filesets-remove-buffer
2088   The simplest way to define a fileset is by adding files to it one at
2089 a time.  To add a file to fileset @var{name}, visit the file and type
2090 @kbd{M-x filesets-add-buffer @key{RET} @var{name} @key{RET}}.  If
2091 there is no fileset @var{name}, this creates a new one, which
2092 initially contains only the current file.  The command @kbd{M-x
2093 filesets-remove-buffer} removes the current file from a fileset.
2095   You can also edit the list of filesets directly, with @kbd{M-x
2096 filesets-edit} (or by choosing @samp{Edit Filesets} from the
2097 @samp{Filesets} menu).  The editing is performed in a Customize buffer
2098 (@pxref{Easy Customization}).  Normally, a fileset is a simple list of
2099 files, but you can also define a fileset as a regular expression
2100 matching file names.  Some examples of these more complicated filesets
2101 are shown in the Customize buffer.  Remember to select @samp{Save for
2102 future sessions} if you want to use the same filesets in future Emacs
2103 sessions.
2105   You can use the command @kbd{M-x filesets-open} to visit all the
2106 files in a fileset, and @kbd{M-x filesets-close} to close them.  Use
2107 @kbd{M-x filesets-run-cmd} to run a shell command on all the files in
2108 a fileset.  These commands are also available from the @samp{Filesets}
2109 menu, where each existing fileset is represented by a submenu.
2111    @xref{Version Control}, for a different concept of filesets:
2112 groups of files bundled together for version control operations.
2113 Filesets of that type are unnamed, and do not persist across Emacs
2114 sessions.