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