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