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