]> code.delx.au - gnu-emacs/blob - man/files.texi
*** empty log message ***
[gnu-emacs] / man / files.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985, 1986, 1987, 1993, 1994, 1995, 1997, 1999, 2000,
3 @c 2001, 2002, 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
4 @c See file emacs.texi for copying conditions.
5 @node Files, Buffers, Keyboard Macros, Top
6 @chapter File Handling
7 @cindex files
8
9 The operating system stores data permanently in named @dfn{files}, so
10 most of the text you edit with Emacs comes from a file and is ultimately
11 stored in a file.
12
13 To edit a file, you must tell Emacs to read the file and prepare a
14 buffer containing a copy of the file's text. This is called
15 @dfn{visiting} the file. Editing commands apply directly to text in the
16 buffer; that is, to the copy inside Emacs. Your changes appear in the
17 file itself only when you @dfn{save} the buffer back into the file.
18
19 In addition to visiting and saving files, Emacs can delete, copy,
20 rename, and append to files, keep multiple versions of them, and operate
21 on file directories.
22
23 @menu
24 * File Names:: How to type and edit file-name arguments.
25 * Visiting:: Visiting a file prepares Emacs to edit the file.
26 * Saving:: Saving makes your changes permanent.
27 * Reverting:: Reverting cancels all the changes not saved.
28 * Auto Save:: Auto Save periodically protects against loss of data.
29 * File Aliases:: Handling multiple names for one file.
30 * Version Control:: Version control systems (RCS, CVS and SCCS).
31 * Directories:: Creating, deleting, and listing file directories.
32 * Comparing Files:: Finding where two files differ.
33 * Diff Mode:: Mode for editing file differences.
34 * Misc File Ops:: Other things you can do on files.
35 * Compressed Files:: Accessing compressed files.
36 * File Archives:: Operating on tar, zip, jar etc. archive files.
37 * Remote Files:: Accessing files on other sites.
38 * Quoted File Names:: Quoting special characters in file names.
39 * File Name Cache:: Completion against a list of files you often use.
40 * File Conveniences:: Convenience Features for Finding Files.
41 * Filesets:: Handling sets of files.
42 @end menu
43
44 @node File Names
45 @section File Names
46 @cindex file names
47
48 Most Emacs commands that operate on a file require you to specify the
49 file name. (Saving and reverting are exceptions; the buffer knows which
50 file name to use for them.) You enter the file name using the
51 minibuffer (@pxref{Minibuffer}). @dfn{Completion} is available
52 (@pxref{Completion}) to make it easier to specify long file names. When
53 completing file names, Emacs ignores those whose file-name extensions
54 appear in the variable @code{completion-ignored-extensions}; see
55 @ref{Completion Options}.
56
57 For most operations, there is a @dfn{default file name} which is used
58 if you type just @key{RET} to enter an empty argument. Normally the
59 default file name is the name of the file visited in the current buffer;
60 this makes it easy to operate on that file with any of the Emacs file
61 commands.
62
63 @vindex default-directory
64 Each buffer has a default directory which is normally the same as the
65 directory of the file visited in that buffer. When you enter a file
66 name without a directory, the default directory is used. If you specify
67 a directory in a relative fashion, with a name that does not start with
68 a slash, it is interpreted with respect to the default directory. The
69 default directory is kept in the variable @code{default-directory},
70 which has a separate value in every buffer.
71
72 @findex cd
73 @findex pwd
74 The command @kbd{M-x pwd} displays the current buffer's default
75 directory, and the command @kbd{M-x cd} sets it (to a value read using
76 the minibuffer). A buffer's default directory changes only when the
77 @code{cd} command is used. A file-visiting buffer's default directory
78 is initialized to the directory of the file it visits. If you create
79 a buffer with @kbd{C-x b}, its default directory is copied from that
80 of the buffer that was current at the time.
81
82 For example, if the default file name is @file{/u/rms/gnu/gnu.tasks}
83 then the default directory is normally @file{/u/rms/gnu/}. If you
84 type just @samp{foo}, which does not specify a directory, it is short
85 for @file{/u/rms/gnu/foo}. @samp{../.login} would stand for
86 @file{/u/rms/.login}. @samp{new/foo} would stand for the file name
87 @file{/u/rms/gnu/new/foo}.
88
89 @vindex insert-default-directory
90 The default directory actually appears in the minibuffer when the
91 minibuffer becomes active to read a file name. This serves two
92 purposes: it @emph{shows} you what the default is, so that you can type
93 a relative file name and know with certainty what it will mean, and it
94 allows you to @emph{edit} the default to specify a different directory.
95 This insertion of the default directory is inhibited if the variable
96 @code{insert-default-directory} is set to @code{nil}.
97
98 Note that it is legitimate to type an absolute file name after you
99 enter the minibuffer, ignoring the presence of the default directory
100 name as part of the text. The final minibuffer contents may look
101 invalid, but that is not so. For example, if the minibuffer starts out
102 with @samp{/usr/tmp/} and you add @samp{/x1/rms/foo}, you get
103 @samp{/usr/tmp//x1/rms/foo}; but Emacs ignores everything through the
104 first slash in the double slash; the result is @samp{/x1/rms/foo}.
105 @xref{Minibuffer File}.
106
107 @cindex home directory shorthand
108 You can use @file{~/} in a file name to mean your home directory,
109 or @file{~@var{user-id}/} to mean the home directory of a user whose
110 login name is @code{user-id}@footnote{
111 On MS-Windows and MS-DOS systems, where a user doesn't have a home
112 directory, Emacs substitutes @file{~/} with the value of the
113 environment variable @code{HOME}; see @ref{General Variables}. The
114 @file{~@var{user-id}/} construct is supported on those systems only
115 for the current user, i.e., only if @var{user-id} is the current
116 user's login name.}.
117
118 @cindex environment variables in file names
119 @cindex expansion of environment variables
120 @cindex @code{$} in file names
121 @anchor{File Names with $}@samp{$} in a file name is used to
122 substitute an environment variable. The environment variable name
123 consists of all the alphanumeric characters after the @samp{$};
124 alternatively, it can be enclosed in braces after the @samp{$}. For
125 example, if you have used the shell command @command{export
126 FOO=rms/hacks} to set up an environment variable named @env{FOO}, then
127 you can use @file{/u/$FOO/test.c} or @file{/u/$@{FOO@}/test.c} as an
128 abbreviation for @file{/u/rms/hacks/test.c}. If the environment
129 variable is not defined, no substitution occurs: @file{/u/$notdefined}
130 stands for itself (assuming the environment variable @env{notdefined}
131 is not defined).
132
133 Note that shell commands to set environment variables affect Emacs
134 only when done before Emacs is started.
135
136 To access a file with @samp{$} in its name, if the @samp{$} causes
137 expansion, type @samp{$$}. This pair is converted to a single
138 @samp{$} at the same time as variable substitution is performed for a
139 single @samp{$}. Alternatively, quote the whole file name with
140 @samp{/:} (@pxref{Quoted File Names}). File names which begin with a
141 literal @samp{~} should also be quoted with @samp{/:}.
142
143 @findex substitute-in-file-name
144 The Lisp function that performs the @samp{$}-substitution is called
145 @code{substitute-in-file-name}. The substitution is performed only on
146 file names read as such using the minibuffer.
147
148 You can include non-@acronym{ASCII} characters in file names if you set the
149 variable @code{file-name-coding-system} to a non-@code{nil} value.
150 @xref{File Name Coding}.
151
152 @node Visiting
153 @section Visiting Files
154 @cindex visiting files
155
156 @table @kbd
157 @item C-x C-f
158 Visit a file (@code{find-file}).
159 @item C-x C-r
160 Visit a file for viewing, without allowing changes to it
161 (@code{find-file-read-only}).
162 @item C-x C-v
163 Visit a different file instead of the one visited last
164 (@code{find-alternate-file}).
165 @item C-x 4 f
166 Visit a file, in another window (@code{find-file-other-window}). Don't
167 alter what is displayed in the selected window.
168 @item C-x 5 f
169 Visit a file, in a new frame (@code{find-file-other-frame}). Don't
170 alter what is displayed in the selected frame.
171 @item M-x find-file-literally
172 Visit a file with no conversion of the contents.
173 @end table
174
175 @cindex files, visiting and saving
176 @cindex saving files
177 @dfn{Visiting} a file means copying its contents into an Emacs
178 buffer so you can edit them. Emacs makes a new buffer for each file
179 that you visit. We often say that this buffer ``is visiting'' that
180 file, or that the buffer's ``visited file'' is that file. Emacs
181 constructs the buffer name from the file name by throwing away the
182 directory, keeping just the name proper. For example, a file named
183 @file{/usr/rms/emacs.tex} would get a buffer named @samp{emacs.tex}.
184 If there is already a buffer with that name, Emacs constructs a unique
185 name---the normal method is to append @samp{<2>}, @samp{<3>}, and so
186 on, but you can select other methods (@pxref{Uniquify}).
187
188 Each window's mode line shows the name of the buffer that is being displayed
189 in that window, so you can always tell what buffer you are editing.
190
191 The changes you make with editing commands are made in the Emacs
192 buffer. They do not take effect in the file that you visited, or any
193 permanent place, until you @dfn{save} the buffer. Saving the buffer
194 means that Emacs writes the current contents of the buffer into its
195 visited file. @xref{Saving}.
196
197 @cindex modified (buffer)
198 If a buffer contains changes that have not been saved, we say the
199 buffer is @dfn{modified}. This is important because it implies that
200 some changes will be lost if the buffer is not saved. The mode line
201 displays two stars near the left margin to indicate that the buffer is
202 modified.
203
204 @kindex C-x C-f
205 @findex find-file
206 To visit a file, use the command @kbd{C-x C-f} (@code{find-file}). Follow
207 the command with the name of the file you wish to visit, terminated by a
208 @key{RET}.
209
210 The file name is read using the minibuffer (@pxref{Minibuffer}), with
211 defaulting and completion in the standard manner (@pxref{File Names}).
212 While in the minibuffer, you can abort @kbd{C-x C-f} by typing
213 @kbd{C-g}. File-name completion ignores certain filenames; for more
214 about this, see @ref{Completion Options}.
215
216 Your confirmation that @kbd{C-x C-f} has completed successfully is
217 the appearance of new text on the screen and a new buffer name in the
218 mode line. If the specified file does not exist and you could not
219 create it, or exists but you can't read it, then you get an error,
220 with an error message displayed in the echo area.
221
222 If you visit a file that is already in Emacs, @kbd{C-x C-f} does not make
223 another copy. It selects the existing buffer containing that file.
224 However, before doing so, it checks whether the file itself has changed
225 since you visited or saved it last. If the file has changed, Emacs offers
226 to reread it.
227
228 @vindex large-file-warning-threshold
229 @cindex maximum buffer size exceeded, error message
230 If you try to visit a file larger than
231 @code{large-file-warning-threshold} (the default is 10000000, which is
232 about 10 megabytes), Emacs will ask you for confirmation first. You
233 can answer @kbd{y} to proceed with visiting the file. Note, however,
234 that Emacs cannot visit files that are larger than the maximum Emacs
235 buffer size, which is around 256 megabytes on 32-bit machines
236 (@pxref{Buffers}). If you try, Emacs will display an error message
237 saying that the maximum buffer size has been exceeded.
238
239 @cindex file selection dialog
240 On graphical displays there are two additional methods for
241 visiting files. Firstly, when Emacs is built with a suitable GUI
242 toolkit, commands invoked with the mouse (by clicking on the menu bar
243 or tool bar) use the toolkit's standard File Selection dialog instead
244 of prompting for the file name in the minibuffer. On Unix and
245 GNU/Linux platforms, Emacs does that when built with GTK, LessTif, and
246 Motif toolkits; on MS-Windows and Mac, the GUI version does that by default.
247 For information on how to customize this, see @ref{Dialog Boxes}.
248
249 Secondly, Emacs supports ``drag and drop''; dropping a file into an
250 ordinary Emacs window visits the file using that window. However,
251 dropping a file into a window displaying a Dired buffer moves or
252 copies the file into the displayed directory. For details, see
253 @ref{Drag and Drop}, and @ref{Misc Dired Features}.
254
255 @cindex creating files
256 What if you want to create a new file? Just visit it. Emacs displays
257 @samp{(New file)} in the echo area, but in other respects behaves as if
258 you had visited an existing empty file. If you make any changes and
259 save them, the file is created.
260
261 Emacs recognizes from the contents of a file which convention it uses
262 to separate lines---newline (used on GNU/Linux and on Unix),
263 carriage-return linefeed (used on Microsoft systems), or just
264 carriage-return (used on the Macintosh)---and automatically converts the
265 contents to the normal Emacs convention, which is that the newline
266 character separates lines. This is a part of the general feature of
267 coding system conversion (@pxref{Coding Systems}), and makes it possible
268 to edit files imported from different operating systems with
269 equal convenience. If you change the text and save the file, Emacs
270 performs the inverse conversion, changing newlines back into
271 carriage-return linefeed or just carriage-return if appropriate.
272
273 @vindex find-file-run-dired
274 If the file you specify is actually a directory, @kbd{C-x C-f} invokes
275 Dired, the Emacs directory browser, so that you can ``edit'' the contents
276 of the directory (@pxref{Dired}). Dired is a convenient way to view, delete,
277 or operate on the files in the directory. However, if the variable
278 @code{find-file-run-dired} is @code{nil}, then it is an error to try
279 to visit a directory.
280
281 Files which are actually collections of other files, or @dfn{file
282 archives}, are visited in special modes which invoke a Dired-like
283 environment to allow operations on archive members. @xref{File
284 Archives}, for more about these features.
285
286 @cindex wildcard characters in file names
287 @vindex find-file-wildcards
288 If the file name you specify contains shell-style wildcard
289 characters, Emacs visits all the files that match it. Wildcards
290 include @samp{?}, @samp{*}, and @samp{[@dots{}]} sequences. To enter
291 the wild card @samp{?} in a file name in the minibuffer, you need to
292 type @kbd{C-q ?}. @xref{Quoted File Names}, for information on how to
293 visit a file whose name actually contains wildcard characters. You
294 can disable the wildcard feature by customizing
295 @code{find-file-wildcards}.
296
297 If you visit a file that the operating system won't let you modify,
298 or that is marked read-only, Emacs makes the buffer read-only too, so
299 that you won't go ahead and make changes that you'll have trouble
300 saving afterward. You can make the buffer writable with @kbd{C-x C-q}
301 (@code{toggle-read-only}). @xref{Misc Buffer}.
302
303 @kindex C-x C-r
304 @findex find-file-read-only
305 If you want to visit a file as read-only in order to protect
306 yourself from entering changes accidentally, visit it with the command
307 @kbd{C-x C-r} (@code{find-file-read-only}) instead of @kbd{C-x C-f}.
308
309 @kindex C-x C-v
310 @findex find-alternate-file
311 If you visit a nonexistent file unintentionally (because you typed the
312 wrong file name), use the @kbd{C-x C-v} command
313 (@code{find-alternate-file}) to visit the file you really wanted.
314 @kbd{C-x C-v} is similar to @kbd{C-x C-f}, but it kills the current
315 buffer (after first offering to save it if it is modified). When
316 @kbd{C-x C-v} reads the file name to visit, it inserts the entire
317 default file name in the buffer, with point just after the directory
318 part; this is convenient if you made a slight error in typing the name.
319
320 @kindex C-x 4 f
321 @findex find-file-other-window
322 @kbd{C-x 4 f} (@code{find-file-other-window}) is like @kbd{C-x C-f}
323 except that the buffer containing the specified file is selected in another
324 window. The window that was selected before @kbd{C-x 4 f} continues to
325 show the same buffer it was already showing. If this command is used when
326 only one window is being displayed, that window is split in two, with one
327 window showing the same buffer as before, and the other one showing the
328 newly requested file. @xref{Windows}.
329
330 @kindex C-x 5 f
331 @findex find-file-other-frame
332 @kbd{C-x 5 f} (@code{find-file-other-frame}) is similar, but opens a
333 new frame, or makes visible any existing frame showing the file you
334 seek. This feature is available only when you are using a window
335 system. @xref{Frames}.
336
337 @findex find-file-literally
338 If you wish to edit a file as a sequence of @acronym{ASCII} characters with no special
339 encoding or conversion, use the @kbd{M-x find-file-literally} command.
340 It visits a file, like @kbd{C-x C-f}, but does not do format conversion
341 (@pxref{Formatted Text}), character code conversion (@pxref{Coding
342 Systems}), or automatic uncompression (@pxref{Compressed Files}), and
343 does not add a final newline because of @code{require-final-newline}.
344 If you already have visited the same file in the usual (non-literal)
345 manner, this command asks you whether to visit it literally instead.
346
347 @vindex find-file-hook
348 @vindex find-file-not-found-functions
349 Two special hook variables allow extensions to modify the operation of
350 visiting files. Visiting a file that does not exist runs the functions
351 in the list @code{find-file-not-found-functions}; this variable holds a list
352 of functions, and the functions are called one by one (with no
353 arguments) until one of them returns non-@code{nil}. This is not a
354 normal hook, and the name ends in @samp{-functions} rather than @samp{-hook}
355 to indicate that fact.
356
357 Successful visiting of any file, whether existing or not, calls the
358 functions in the list @code{find-file-hook}, with no arguments.
359 This variable is a normal hook. In the case of a nonexistent file, the
360 @code{find-file-not-found-functions} are run first. @xref{Hooks}.
361
362 There are several ways to specify automatically the major mode for
363 editing the file (@pxref{Choosing Modes}), and to specify local
364 variables defined for that file (@pxref{File Variables}).
365
366 @node Saving
367 @section Saving Files
368
369 @dfn{Saving} a buffer in Emacs means writing its contents back into the file
370 that was visited in the buffer.
371
372 @menu
373 * Save Commands:: Commands for saving files.
374 * Backup:: How Emacs saves the old version of your file.
375 * Customize Save:: Customizing the saving of files.
376 * Interlocking:: How Emacs protects against simultaneous editing
377 of one file by two users.
378 * Shadowing: File Shadowing. Copying files to "shadows" automatically.
379 * Time Stamps:: Emacs can update time stamps on saved files.
380 @end menu
381
382 @node Save Commands
383 @subsection Commands for Saving Files
384
385 These are the commands that relate to saving and writing files.
386
387 @table @kbd
388 @item C-x C-s
389 Save the current buffer in its visited file on disk (@code{save-buffer}).
390 @item C-x s
391 Save any or all buffers in their visited files (@code{save-some-buffers}).
392 @item M-~
393 Forget that the current buffer has been changed (@code{not-modified}).
394 With prefix argument (@kbd{C-u}), mark the current buffer as changed.
395 @item C-x C-w
396 Save the current buffer with a specified file name (@code{write-file}).
397 @item M-x set-visited-file-name
398 Change the file name under which the current buffer will be saved.
399 @end table
400
401 @kindex C-x C-s
402 @findex save-buffer
403 When you wish to save the file and make your changes permanent, type
404 @kbd{C-x C-s} (@code{save-buffer}). After saving is finished, @kbd{C-x C-s}
405 displays a message like this:
406
407 @example
408 Wrote /u/rms/gnu/gnu.tasks
409 @end example
410
411 @noindent
412 If the selected buffer is not modified (no changes have been made in it
413 since the buffer was created or last saved), saving is not really done,
414 because it would have no effect. Instead, @kbd{C-x C-s} displays a message
415 like this in the echo area:
416
417 @example
418 (No changes need to be saved)
419 @end example
420
421 @kindex C-x s
422 @findex save-some-buffers
423 The command @kbd{C-x s} (@code{save-some-buffers}) offers to save any
424 or all modified buffers. It asks you what to do with each buffer. The
425 possible responses are analogous to those of @code{query-replace}:
426
427 @table @kbd
428 @item y
429 Save this buffer and ask about the rest of the buffers.
430 @item n
431 Don't save this buffer, but ask about the rest of the buffers.
432 @item !
433 Save this buffer and all the rest with no more questions.
434 @c following generates acceptable underfull hbox
435 @item @key{RET}
436 Terminate @code{save-some-buffers} without any more saving.
437 @item .
438 Save this buffer, then exit @code{save-some-buffers} without even asking
439 about other buffers.
440 @item C-r
441 View the buffer that you are currently being asked about. When you exit
442 View mode, you get back to @code{save-some-buffers}, which asks the
443 question again.
444 @item d
445 Diff the buffer against its corresponding file, so you can see
446 what changes you would be saving.
447 @item C-h
448 Display a help message about these options.
449 @end table
450
451 @kbd{C-x C-c}, the key sequence to exit Emacs, invokes
452 @code{save-some-buffers} and therefore asks the same questions.
453
454 @kindex M-~
455 @findex not-modified
456 If you have changed a buffer but you do not want to save the changes,
457 you should take some action to prevent it. Otherwise, each time you use
458 @kbd{C-x s} or @kbd{C-x C-c}, you are liable to save this buffer by
459 mistake. One thing you can do is type @kbd{M-~} (@code{not-modified}),
460 which clears out the indication that the buffer is modified. If you do
461 this, none of the save commands will believe that the buffer needs to be
462 saved. (@samp{~} is often used as a mathematical symbol for `not'; thus
463 @kbd{M-~} is `not', metafied.) You could also use
464 @code{set-visited-file-name} (see below) to mark the buffer as visiting
465 a different file name, one which is not in use for anything important.
466 Alternatively, you can cancel all the changes made since the file was
467 visited or saved, by reading the text from the file again. This is
468 called @dfn{reverting}. @xref{Reverting}. (You could also undo all the
469 changes by repeating the undo command @kbd{C-x u} until you have undone
470 all the changes; but reverting is easier.) You can also kill the buffer.
471
472 @findex set-visited-file-name
473 @kbd{M-x set-visited-file-name} alters the name of the file that the
474 current buffer is visiting. It reads the new file name using the
475 minibuffer. Then it marks the buffer as visiting that file name, and
476 changes the buffer name correspondingly. @code{set-visited-file-name}
477 does not save the buffer in the newly visited file; it just alters the
478 records inside Emacs in case you do save later. It also marks the
479 buffer 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
484 If you wish to mark the buffer as visiting a different file and save it
485 right away, use @kbd{C-x C-w} (@code{write-file}). It is
486 equivalent to @code{set-visited-file-name} followed by @kbd{C-x C-s}
487 (except that @kbd{C-x C-w} asks for confirmation if the file exists).
488 @kbd{C-x C-s} used on a buffer that is not visiting a file has the
489 same effect as @kbd{C-x C-w}; that is, it reads a file name, marks the
490 buffer as visiting that file, and saves it there. The default file name in
491 a buffer that is not visiting a file is made by combining the buffer name
492 with the buffer's default directory (@pxref{File Names}).
493
494 If the new file name implies a major mode, then @kbd{C-x C-w} switches
495 to 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
499 version on disk does not match what Emacs last read or wrote, Emacs
500 notifies you of this fact, because it probably indicates a problem caused
501 by 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
511 record of what the file used to contain. Thus, saving a file from Emacs
512 throws away the old contents of the file---or it would, except that
513 Emacs carefully copies the old contents to another file, called the
514 @dfn{backup} file, before actually saving.
515
516 For most files, the variable @code{make-backup-files} determines
517 whether to make backup files. On most operating systems, its default
518 value is @code{t}, so that Emacs does write backup files.
519
520 For files managed by a version control system (@pxref{Version
521 Control}), the variable @code{vc-make-backup-files} determines whether
522 to make backup files. By default it is @code{nil}, since backup files
523 are redundant when you store all the previous versions in a version
524 control system. @xref{General VC Options,,,emacs-xtra, Specialized
525 Emacs Features}.
526
527 At your option, Emacs can keep either a single backup for each file,
528 or make a series of numbered backup files for each file that you edit.
529
530 @vindex backup-enable-predicate
531 @vindex temporary-file-directory
532 @vindex small-temporary-file-directory
533 The default value of the @code{backup-enable-predicate} variable
534 prevents backup files being written for files in the directories used
535 for temporary files, specified by @code{temporary-file-directory} or
536 @code{small-temporary-file-directory}.
537
538 Emacs makes a backup for a file only the first time the file is saved
539 from one buffer. No matter how many times you save a file, its backup file
540 continues to contain the contents from before the file was visited.
541 Normally this means that the backup file contains the contents from before
542 the current editing session; however, if you kill the buffer and then visit
543 the file again, a new backup file will be made by the next save.
544
545 You can also explicitly request making another backup file from a
546 buffer even though it has already been saved at least once. If you save
547 the buffer with @kbd{C-u C-x C-s}, the version thus saved will be made
548 into a backup file if you save the buffer again. @kbd{C-u C-u C-x C-s}
549 saves the buffer, but first makes the previous file contents into a new
550 backup file. @kbd{C-u C-u C-u C-x C-s} does both things: it makes a
551 backup from the previous contents, and arranges to make another from the
552 newly saved contents if you save again.
553
554 @menu
555 * One or Many: Numbered Backups. Whether to make one backup file or many.
556 * Names: Backup Names. How backup files are named.
557 * Deletion: Backup Deletion. Emacs deletes excess numbered backups.
558 * Copying: Backup Copying. Backups can be made by copying or renaming.
559 @end menu
560
561 @node Numbered Backups
562 @subsubsection Numbered Backups
563
564 @vindex version-control
565 The choice of single backup file or multiple numbered backup files
566 is controlled by the variable @code{version-control}. Its possible
567 values are:
568
569 @table @code
570 @item t
571 Make numbered backups.
572 @item nil
573 Make numbered backups for files that have numbered backups already.
574 Otherwise, make single backups.
575 @item never
576 Never make numbered backups; always make single backups.
577 @end table
578
579 @noindent
580 The usual way to set this variable is globally, through your
581 @file{.emacs} file or the customization buffer. However, you can set
582 @code{version-control} locally in an individual buffer to control the
583 making of backups for that buffer's file. For example, Rmail mode
584 locally sets @code{version-control} to @code{never} to make sure that
585 there is only one backup for an Rmail file. @xref{Locals}.
586
587 @cindex @env{VERSION_CONTROL} environment variable
588 If you set the environment variable @env{VERSION_CONTROL}, to tell
589 various GNU utilities what to do with backup files, Emacs also obeys the
590 environment variable by setting the Lisp variable @code{version-control}
591 accordingly at startup. If the environment variable's value is @samp{t}
592 or @samp{numbered}, then @code{version-control} becomes @code{t}; if the
593 value is @samp{nil} or @samp{existing}, then @code{version-control}
594 becomes @code{nil}; if it is @samp{never} or @samp{simple}, then
595 @code{version-control} becomes @code{never}.
596
597 @node Backup Names
598 @subsubsection Single or Numbered Backups
599
600 When Emacs makes a single backup file, its name is normally
601 constructed by appending @samp{~} to the file name being edited; thus,
602 the backup file for @file{eval.c} would be @file{eval.c~}.
603
604 @vindex make-backup-file-name-function
605 @vindex backup-directory-alist
606 You can change this behavior by defining the variable
607 @code{make-backup-file-name-function} to a suitable function.
608 Alternatively you can customize the variable
609 @code{backup-directory-alist} to specify that files matching certain
610 patterns should be backed up in specific directories.
611
612 A typical use is to add an element @code{("." . @var{dir})} to make
613 all backups in the directory with absolute name @var{dir}; Emacs
614 modifies the backup file names to avoid clashes between files with the
615 same names originating in different directories. Alternatively,
616 adding, say, @code{("." . ".~")} would make backups in the invisible
617 subdirectory @file{.~} of the original file's directory. Emacs
618 creates the directory, if necessary, to make the backup.
619
620 If access control stops Emacs from writing backup files under the usual
621 names, it writes the backup file as @file{%backup%~} in your home
622 directory. Only one such file can exist, so only the most recently
623 made such backup is available.
624
625 If you choose to have a series of numbered backup files, backup file
626 names contain @samp{.~}, the number, and another @samp{~} after the
627 original file name. Thus, the backup files of @file{eval.c} would be
628 called @file{eval.c.~1~}, @file{eval.c.~2~}, and so on, all the way
629 through names like @file{eval.c.~259~} and beyond. The variable
630 @code{backup-directory-alist} applies to numbered backups just as
631 usual.
632
633 @node Backup Deletion
634 @subsubsection Automatic Deletion of Backups
635
636 To prevent excessive consumption of disk space, Emacs can delete numbered
637 backup versions automatically. Generally Emacs keeps the first few backups
638 and the latest few backups, deleting any in between. This happens every
639 time a new backup is made.
640
641 @vindex kept-old-versions
642 @vindex kept-new-versions
643 The two variables @code{kept-old-versions} and
644 @code{kept-new-versions} control this deletion. Their values are,
645 respectively, the number of oldest (lowest-numbered) backups to keep
646 and the number of newest (highest-numbered) ones to keep, each time a
647 new backup is made. The backups in the middle (excluding those oldest
648 and newest) are the excess middle versions---those backups are
649 deleted. These variables' values are used when it is time to delete
650 excess versions, just after a new backup version is made; the newly
651 made backup is included in the count in @code{kept-new-versions}. By
652 default, both variables are 2.
653
654 @vindex delete-old-versions
655 If @code{delete-old-versions} is @code{t}, Emacs deletes the excess
656 backup files silently. If it is @code{nil}, the default, Emacs asks
657 you whether it should delete the excess backup versions. If it has
658 any other value, then Emacs never automatically deletes backups.
659
660 Dired's @kbd{.} (Period) command can also be used to delete old versions.
661 @xref{Dired Deletion}.
662
663 @node Backup Copying
664 @subsubsection Copying vs.@: Renaming
665
666 Backup files can be made by copying the old file or by renaming it.
667 This makes a difference when the old file has multiple names (hard
668 links). If the old file is renamed into the backup file, then the
669 alternate names become names for the backup file. If the old file is
670 copied instead, then the alternate names remain names for the file
671 that you are editing, and the contents accessed by those names will be
672 the new contents.
673
674 The method of making a backup file may also affect the file's owner
675 and group. If copying is used, these do not change. If renaming is used,
676 you become the file's owner, and the file's group becomes the default
677 (different operating systems have different defaults for the group).
678
679 Having the owner change is usually a good idea, because then the owner
680 always shows who last edited the file. Also, the owners of the backups
681 show who produced those versions. Occasionally there is a file whose
682 owner should not change; it is a good idea for such files to contain
683 local variable lists to set @code{backup-by-copying-when-mismatch}
684 locally (@pxref{File Variables}).
685
686 @vindex backup-by-copying
687 @vindex backup-by-copying-when-linked
688 @vindex backup-by-copying-when-mismatch
689 @vindex backup-by-copying-when-privileged-mismatch
690 @cindex file ownership, and backup
691 @cindex backup, and user-id
692 The choice of renaming or copying is controlled by four variables.
693 Renaming is the default choice. If the variable
694 @code{backup-by-copying} is non-@code{nil}, copying is used. Otherwise,
695 if the variable @code{backup-by-copying-when-linked} is non-@code{nil},
696 then copying is used for files that have multiple names, but renaming
697 may still be used when the file being edited has only one name. If the
698 variable @code{backup-by-copying-when-mismatch} is non-@code{nil}, then
699 copying is used if renaming would cause the file's owner or group to
700 change. @code{backup-by-copying-when-mismatch} is @code{t} by default
701 if you start Emacs as the superuser. The fourth variable,
702 @code{backup-by-copying-when-privileged-mismatch}, gives the highest
703 numeric user-id for which @code{backup-by-copying-when-mismatch} will be
704 forced on. This is useful when low-numbered user-ids are assigned to
705 special system users, such as @code{root}, @code{bin}, @code{daemon},
706 etc., which must maintain ownership of files.
707
708 When a file is managed with a version control system (@pxref{Version
709 Control}), Emacs does not normally make backups in the usual way for
710 that file. But check-in and check-out are similar in some ways to
711 making backups. One unfortunate similarity is that these operations
712 typically break hard links, disconnecting the file name you visited from
713 any alternate names for the same file. This has nothing to do with
714 Emacs---the version control system does it.
715
716 @node Customize Save
717 @subsection Customizing Saving of Files
718
719 @vindex require-final-newline
720 If the value of the variable @code{require-final-newline} is
721 @code{t}, saving or writing a file silently puts a newline at the end
722 if there isn't already one there. If the value is @code{visit}, Emacs
723 adds a newline at the end of any file that doesn't have one, just
724 after it visits the file. (This marks the buffer as modified, and you
725 can undo it.) If the value is @code{visit-save}, that means to add
726 newlines both on visiting and on saving. If the value is @code{nil},
727 Emacs leaves the end of the file unchanged; if it's neither @code{nil}
728 nor @code{t}, Emacs asks you whether to add a newline. The default is
729 @code{nil}.
730
731 @vindex mode-require-final-newline
732 Many major modes are designed for specific kinds of files that are
733 always supposed to end in newlines. These major modes set the
734 variable @code{require-final-newline} according to
735 @code{mode-require-final-newline}. By setting the latter variable,
736 you can control how these modes handle final newlines.
737
738 @vindex write-region-inhibit-fsync
739 When Emacs saves a file, it invokes the @code{fsync} system call to
740 force the data immediately out to disk. This is important for safety
741 if the system crashes or in case of power outage. However, it can be
742 disruptive on laptops using power saving, because it requires the disk
743 to spin up each time you save a file. Setting
744 @code{write-region-inhibit-fsync} to a non-@code{nil} value disables
745 this synchronization. Be careful---this means increased risk of data
746 loss.
747
748 @node Interlocking
749 @subsection Protection against Simultaneous Editing
750
751 @cindex file dates
752 @cindex simultaneous editing
753 Simultaneous editing occurs when two users visit the same file, both
754 make changes, and then both save them. If nobody were informed that
755 this was happening, whichever user saved first would later find that his
756 changes were lost.
757
758 On some systems, Emacs notices immediately when the second user starts
759 to change the file, and issues an immediate warning. On all systems,
760 Emacs checks when you save the file, and warns if you are about to
761 overwrite another user's changes. You can prevent loss of the other
762 user's work by taking the proper corrective action instead of saving the
763 file.
764
765 @findex ask-user-about-lock
766 @cindex locking files
767 When you make the first modification in an Emacs buffer that is
768 visiting a file, Emacs records that the file is @dfn{locked} by you.
769 (It does this by creating a symbolic link in the same directory with a
770 different name.) Emacs removes the lock when you save the changes. The
771 idea is that the file is locked whenever an Emacs buffer visiting it has
772 unsaved changes.
773
774 @cindex collision
775 If you begin to modify the buffer while the visited file is locked by
776 someone else, this constitutes a @dfn{collision}. When Emacs detects a
777 collision, it asks you what to do, by calling the Lisp function
778 @code{ask-user-about-lock}. You can redefine this function for the sake
779 of customization. The standard definition of this function asks you a
780 question and accepts three possible answers:
781
782 @table @kbd
783 @item s
784 Steal the lock. Whoever was already changing the file loses the lock,
785 and you gain the lock.
786 @item p
787 Proceed. Go ahead and edit the file despite its being locked by someone else.
788 @item q
789 Quit. This causes an error (@code{file-locked}), and the buffer
790 contents remain unchanged---the modification you were trying to make
791 does not actually take place.
792 @end table
793
794 Note that locking works on the basis of a file name; if a file has
795 multiple names, Emacs does not realize that the two names are the same file
796 and cannot prevent two users from editing it simultaneously under different
797 names. However, basing locking on names means that Emacs can interlock the
798 editing of new files that will not really exist until they are saved.
799
800 Some systems are not configured to allow Emacs to make locks, and
801 there are cases where lock files cannot be written. In these cases,
802 Emacs cannot detect trouble in advance, but it still can detect the
803 collision when you try to save a file and overwrite someone else's
804 changes.
805
806 If Emacs or the operating system crashes, this may leave behind lock
807 files which are stale, so you may occasionally get warnings about
808 spurious collisions. When you determine that the collision is spurious,
809 just use @kbd{p} to tell Emacs to go ahead anyway.
810
811 Every time Emacs saves a buffer, it first checks the last-modification
812 date of the existing file on disk to verify that it has not changed since the
813 file was last visited or saved. If the date does not match, it implies
814 that changes were made in the file in some other way, and these changes are
815 about to be lost if Emacs actually does save. To prevent this, Emacs
816 displays a warning message and asks for confirmation before saving.
817 Occasionally you will know why the file was changed and know that it does
818 not matter; then you can answer @kbd{yes} and proceed. Otherwise, you should
819 cancel the save with @kbd{C-g} and investigate the situation.
820
821 The first thing you should do when notified that simultaneous editing
822 has 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
824 should attempt to contact him to warn him not to continue editing.
825 Often the next step is to save the contents of your Emacs buffer under a
826 different 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
836 Set up file shadowing.
837 @item M-x shadow-define-literal-group
838 Declare a single file to be shared between sites.
839 @item M-x shadow-define-regexp-group
840 Make 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}
842 Define a shadow file cluster @var{name}.
843 @item M-x shadow-copy-files
844 Copy all pending shadow files.
845 @item M-x shadow-cancel
846 Cancel the instruction to shadow some files.
847 @end table
848
849 You can arrange to keep identical @dfn{shadow} copies of certain files
850 in more than one place---possibly on different machines. To do this,
851 first you must set up a @dfn{shadow file group}, which is a set of
852 identically-named files shared between a list of sites. The file
853 group is permanent and applies to further Emacs sessions as well as
854 the current one. Once the group is set up, every time you exit Emacs,
855 it will copy the file you edited to the other files in its group. You
856 can also do the copying without exiting Emacs, by typing @kbd{M-x
857 shadow-copy-files}.
858
859 To set up a shadow file group, use @kbd{M-x
860 shadow-define-literal-group} or @kbd{M-x shadow-define-regexp-group}.
861 See their documentation strings for further information.
862
863 Before copying a file to its shadows, Emacs asks for confirmation.
864 You can answer ``no'' to bypass copying of this file, this time. If
865 you 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
868 A @dfn{shadow cluster} is a group of hosts that share directories, so
869 that copying to or from one of them is sufficient to update the file
870 on all of them. Each shadow cluster has a name, and specifies the
871 network address of a primary host (the one we copy files to), and a
872 regular expression that matches the host names of all the other hosts
873 in the cluster. You can define a shadow cluster with @kbd{M-x
874 shadow-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
882 You can arrange to put a time stamp in a file, so that it will be updated
883 automatically each time you edit and save the file. The time stamp
884 has to be in the first eight lines of the file, and you should
885 insert it like this:
886
887 @example
888 Time-stamp: <>
889 @end example
890
891 @noindent
892 or like this:
893
894 @example
895 Time-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
901 the time stamp, inserting the current date and time when you save the
902 file. You can also use the command @kbd{M-x time-stamp} to update the
903 time stamp manually. For other customizations, see the Custom group
904 @code{time-stamp}. Note that non-numeric fields in the time stamp are
905 formatted 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
914 about them, you can get rid of them by reading in the previous version
915 of the file. To do this, use @kbd{M-x revert-buffer}, which operates on
916 the current buffer. Since reverting a buffer unintentionally could lose
917 a 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
920 the file was edited only slightly, you will be at approximately the
921 same piece of text after reverting as before. However, if you have made
922 drastic changes, point may wind up in a totally different piece of text.
923
924 Reverting marks the buffer as ``not modified'' until another change is
925 made.
926
927 Some kinds of buffers whose contents reflect data bases other than files,
928 such as Dired buffers, can also be reverted. For them, reverting means
929 recalculating their contents from the appropriate data base. Buffers
930 created explicitly with @kbd{C-x b} cannot be reverted; @code{revert-buffer}
931 reports an error when asked to do so.
932
933 @vindex revert-without-query
934 When you edit a file that changes automatically and frequently---for
935 example, a log of output from a process that continues to run---it may be
936 useful for Emacs to revert the file without querying you, whenever you
937 visit the file again with @kbd{C-x C-f}.
938
939 To request this behavior, set the variable @code{revert-without-query}
940 to a list of regular expressions. When a file name matches one of these
941 regular expressions, @code{find-file} and @code{revert-buffer} will
942 revert it automatically if it has changed---provided the buffer itself
943 is not modified. (If you have edited the text, it would be wrong to
944 discard your changes.)
945
946 @cindex Global Auto-Revert mode
947 @cindex mode, Global Auto-Revert
948 @cindex Auto-Revert mode
949 @cindex mode, Auto-Revert
950 @findex global-auto-revert-mode
951 @findex auto-revert-mode
952 @findex auto-revert-tail-mode
953
954 You may find it useful to have Emacs revert files automatically when
955 they change. Three minor modes are available to do this.
956
957 @kbd{M-x global-auto-revert-mode} enables Global Auto-Revert mode,
958 which periodically checks all file buffers and reverts when the
959 corresponding file has changed. @kbd{M-x auto-revert-mode} enables a
960 local version, Auto-Revert mode, which applies only to the current
961 buffer.
962
963 You can use Auto-Revert mode to ``tail'' a file such as a system
964 log, so that changes made to that file by other programs are
965 continuously displayed. To do this, just move the point to the end of
966 the buffer, and it will stay there as the file contents change.
967 However, if you are sure that the file will only change by growing at
968 the end, use Auto-Revert Tail mode instead
969 (@code{auto-revert-tail-mode}). It is more efficient for this.
970
971 @vindex auto-revert-interval
972 The variable @code{auto-revert-interval} controls how often to check
973 for a changed file. Since checking a remote file is too slow, these
974 modes do not check or revert remote files.
975
976 @xref{VC Mode Line}, for Auto Revert peculiarities in buffers that
977 visit files under version control.
978
979 @node Auto Save
980 @section Auto-Saving: Protection Against Disasters
981 @cindex Auto Save mode
982 @cindex mode, Auto Save
983 @cindex crashes
984
985 Emacs saves all the visited files from time to time (based on counting
986 your keystrokes) without being asked. This is called @dfn{auto-saving}.
987 It prevents you from losing more than a limited amount of work if the
988 system crashes.
989
990 When Emacs determines that it is time for auto-saving, it considers
991 each buffer, and each is auto-saved if auto-saving is enabled for it
992 and it has been changed since the last time it was auto-saved. The
993 message @samp{Auto-saving...} is displayed in the echo area during
994 auto-saving, if any files are actually auto-saved. Errors occurring
995 during auto-saving are caught so that they do not interfere with the
996 execution of commands you have been typing.
997
998 @menu
999 * Files: Auto Save Files. The file where auto-saved changes are
1000 actually made until you save the file.
1001 * Control: Auto Save Control. Controlling when and how often to auto-save.
1002 * Recover:: Recovering text from auto-save files.
1003 @end menu
1004
1005 @node Auto Save Files
1006 @subsection Auto-Save Files
1007
1008 Auto-saving does not normally save in the files that you visited, because
1009 it can be very undesirable to save a program that is in an inconsistent
1010 state when you have made half of a planned change. Instead, auto-saving
1011 is done in a different file called the @dfn{auto-save file}, and the
1012 visited file is changed only when you request saving explicitly (such as
1013 with @kbd{C-x C-s}).
1014
1015 Normally, the auto-save file name is made by appending @samp{#} to the
1016 front and rear of the visited file name. Thus, a buffer visiting file
1017 @file{foo.c} is auto-saved in a file @file{#foo.c#}. Most buffers that
1018 are not visiting files are auto-saved only if you request it explicitly;
1019 when they are auto-saved, the auto-save file name is made by appending
1020 @samp{#} to the front and rear of buffer name, then
1021 adding digits and letters at the end for uniqueness. For
1022 example, the @samp{*mail*} buffer in which you compose messages to be
1023 sent might be auto-saved in a file named @file{#*mail*#704juu}. Auto-save file
1024 names are made this way unless you reprogram parts of Emacs to do
1025 something different (the functions @code{make-auto-save-file-name} and
1026 @code{auto-save-file-name-p}). The file name to be used for auto-saving
1027 in a buffer is calculated when auto-saving is turned on in that buffer.
1028
1029 @cindex auto-save for remote files
1030 @vindex auto-save-file-name-transforms
1031 The variable @code{auto-save-file-name-transforms} allows a degree
1032 of control over the auto-save file name. It lets you specify a series
1033 of regular expressions and replacements to transform the auto save
1034 file name. The default value puts the auto-save files for remote
1035 files (@pxref{Remote Files}) into the temporary file directory on the
1036 local machine.
1037
1038 When you delete a substantial part of the text in a large buffer, auto
1039 save turns off temporarily in that buffer. This is because if you
1040 deleted the text unintentionally, you might find the auto-save file more
1041 useful if it contains the deleted text. To reenable auto-saving after
1042 this happens, save the buffer with @kbd{C-x C-s}, or use @kbd{C-u 1 M-x
1043 auto-save-mode}.
1044
1045 @vindex auto-save-visited-file-name
1046 If you want auto-saving to be done in the visited file rather than
1047 in a separate auto-save file, set the variable
1048 @code{auto-save-visited-file-name} to a non-@code{nil} value. In this
1049 mode, there is no real difference between auto-saving and explicit
1050 saving.
1051
1052 @vindex delete-auto-save-files
1053 A buffer's auto-save file is deleted when you save the buffer in its
1054 visited file. (You can inhibit this by setting the variable
1055 @code{delete-auto-save-files} to @code{nil}.) Changing the visited
1056 file name with @kbd{C-x C-w} or @code{set-visited-file-name} renames
1057 any auto-save file to go with the new visited name.
1058
1059 @node Auto Save Control
1060 @subsection Controlling Auto-Saving
1061
1062 @vindex auto-save-default
1063 @findex auto-save-mode
1064 Each time you visit a file, auto-saving is turned on for that file's
1065 buffer if the variable @code{auto-save-default} is non-@code{nil} (but not
1066 in batch mode; @pxref{Entering Emacs}). The default for this variable is
1067 @code{t}, so auto-saving is the usual practice for file-visiting buffers.
1068 Auto-saving can be turned on or off for any existing buffer with the
1069 command @kbd{M-x auto-save-mode}. Like other minor mode commands, @kbd{M-x
1070 auto-save-mode} turns auto-saving on with a positive argument, off with a
1071 zero or negative argument; with no argument, it toggles.
1072
1073 @vindex auto-save-interval
1074 Emacs does auto-saving periodically based on counting how many characters
1075 you have typed since the last time auto-saving was done. The variable
1076 @code{auto-save-interval} specifies how many characters there are between
1077 auto-saves. By default, it is 300. Emacs doesn't accept values that are
1078 too small: if you customize @code{auto-save-interval} to a value less
1079 than 20, Emacs will behave as if the value is 20.
1080
1081 @vindex auto-save-timeout
1082 Auto-saving also takes place when you stop typing for a while. The
1083 variable @code{auto-save-timeout} says how many seconds Emacs should
1084 wait before it does an auto save (and perhaps also a garbage
1085 collection). (The actual time period is longer if the current buffer is
1086 long; this is a heuristic which aims to keep out of your way when you
1087 are editing long buffers, in which auto-save takes an appreciable amount
1088 of time.) Auto-saving during idle periods accomplishes two things:
1089 first, it makes sure all your work is saved if you go away from the
1090 terminal for a while; second, it may avoid some auto-saving while you
1091 are actually typing.
1092
1093 Emacs also does auto-saving whenever it gets a fatal error. This
1094 includes killing the Emacs job with a shell command such as @samp{kill
1095 %emacs}, or disconnecting a phone line or network connection.
1096
1097 @findex do-auto-save
1098 You can request an auto-save explicitly with the command @kbd{M-x
1099 do-auto-save}.
1100
1101 @node Recover
1102 @subsection Recovering Data from Auto-Saves
1103
1104 @findex recover-file
1105 You can use the contents of an auto-save file to recover from a loss
1106 of data with the command @kbd{M-x recover-file @key{RET} @var{file}
1107 @key{RET}}. This visits @var{file} and then (after your confirmation)
1108 restores the contents from its auto-save file @file{#@var{file}#}.
1109 You can then save with @kbd{C-x C-s} to put the recovered text into
1110 @var{file} itself. For example, to recover file @file{foo.c} from its
1111 auto-save file @file{#foo.c#}, do:@refill
1112
1113 @example
1114 M-x recover-file @key{RET} foo.c @key{RET}
1115 yes @key{RET}
1116 C-x C-s
1117 @end example
1118
1119 Before asking for confirmation, @kbd{M-x recover-file} displays a
1120 directory listing describing the specified file and the auto-save file,
1121 so you can compare their sizes and dates. If the auto-save file
1122 is older, @kbd{M-x recover-file} does not offer to read it.
1123
1124 @findex recover-session
1125 If Emacs or the computer crashes, you can recover all the files you
1126 were editing from their auto save files with the command @kbd{M-x
1127 recover-session}. This first shows you a list of recorded interrupted
1128 sessions. Move point to the one you choose, and type @kbd{C-c C-c}.
1129
1130 Then @code{recover-session} asks about each of the files that were
1131 being edited during that session, asking whether to recover that file.
1132 If you answer @kbd{y}, it calls @code{recover-file}, which works in its
1133 normal fashion. It shows the dates of the original file and its
1134 auto-save file, and asks once again whether to recover that file.
1135
1136 When @code{recover-session} is done, the files you've chosen to
1137 recover are present in Emacs buffers. You should then save them. Only
1138 this---saving them---updates the files themselves.
1139
1140 @vindex auto-save-list-file-prefix
1141 Emacs records interrupted sessions for later recovery in files named
1142 @file{~/.emacs.d/auto-save-list/.saves-@var{pid}-@var{hostname}}. All
1143 of this name except @file{@var{pid}-@var{hostname}} comes from the
1144 value of @code{auto-save-list-file-prefix}. You can record sessions
1145 in a different place by customizing that variable. If you set
1146 @code{auto-save-list-file-prefix} to @code{nil} in your @file{.emacs}
1147 file, sessions are not recorded for recovery.
1148
1149 @node File Aliases
1150 @section File Name Aliases
1151 @cindex symbolic links (visiting)
1152 @cindex hard links (visiting)
1153
1154 Symbolic links and hard links both make it possible for several file
1155 names to refer to the same file. Hard links are alternate names that
1156 refer directly to the file; all the names are equally valid, and no one
1157 of them is preferred. By contrast, a symbolic link is a kind of defined
1158 alias: when @file{foo} is a symbolic link to @file{bar}, you can use
1159 either name to refer to the file, but @file{bar} is the real name, while
1160 @file{foo} is just an alias. More complex cases occur when symbolic
1161 links point to directories.
1162
1163 @vindex find-file-existing-other-name
1164 @vindex find-file-suppress-same-file-warnings
1165
1166 Normally, if you visit a file which Emacs is already visiting under
1167 a different name, Emacs displays a message in the echo area and uses
1168 the existing buffer visiting that file. This can happen on systems
1169 that support hard or symbolic links, or if you use a long file name on
1170 a system that truncates long file names, or on a case-insensitive file
1171 system. You can suppress the message by setting the variable
1172 @code{find-file-suppress-same-file-warnings} to a non-@code{nil}
1173 value. You can disable this feature entirely by setting the variable
1174 @code{find-file-existing-other-name} to @code{nil}: then if you visit
1175 the same file under two different names, you get a separate buffer for
1176 each file name.
1177
1178 @vindex find-file-visit-truename
1179 @cindex truenames of files
1180 @cindex file truenames
1181 If the variable @code{find-file-visit-truename} is non-@code{nil},
1182 then the file name recorded for a buffer is the file's @dfn{truename}
1183 (made by replacing all symbolic links with their target names), rather
1184 than the name you specify. Setting @code{find-file-visit-truename} also
1185 implies the effect of @code{find-file-existing-other-name}.
1186
1187 @node Version Control
1188 @section Version Control
1189 @cindex version control
1190
1191 @dfn{Version control systems} are packages that can record multiple
1192 versions of a source file, usually storing the unchanged parts of the
1193 file just once. Version control systems also record history information
1194 such as the creation time of each version, who created it, and a
1195 description of what was changed in that version.
1196
1197 The Emacs version control interface is called VC. Its commands work
1198 with different version control systems---currently, it supports CVS,
1199 GNU Arch, RCS, Meta-CVS, Subversion, and SCCS. Of these, the GNU
1200 project distributes CVS, GNU Arch, and RCS; we recommend that you use
1201 either CVS or GNU Arch for your projects, and RCS for individual
1202 files. We also have free software to replace SCCS, known as CSSC; if
1203 you are using SCCS and don't want to make the incompatible change to
1204 RCS or CVS, you can switch to CSSC.
1205
1206 VC is enabled by default in Emacs. To disable it, set the
1207 customizable variable @code{vc-handled-backends} to @code{nil}
1208 (@pxref{Customizing VC,,,emacs-xtra, Specialized Emacs Features}).
1209
1210 @menu
1211 * Introduction to VC:: How version control works in general.
1212 * VC Mode Line:: How the mode line shows version control status.
1213 * Basic VC Editing:: How to edit a file under version control.
1214 * Old Versions:: Examining and comparing old versions.
1215 * Secondary VC Commands:: The commands used a little less frequently.
1216 * Branches:: Multiple lines of development.
1217 @end menu
1218
1219 @node Introduction to VC
1220 @subsection Introduction to Version Control
1221
1222 VC allows you to use a version control system from within Emacs,
1223 integrating the version control operations smoothly with editing. VC
1224 provides a uniform interface to version control, so that regardless of
1225 which version control system is in use, you can use it the same way.
1226
1227 This section provides a general overview of version control, and
1228 describes the version control systems that VC supports. You can skip
1229 this section if you are already familiar with the version control system
1230 you want to use.
1231
1232 @menu
1233 * Version Systems:: Supported version control back-end systems.
1234 * VC Concepts:: Words and concepts related to version control.
1235 * Types of Log File:: The per-file VC log in contrast to the ChangeLog.
1236 @end menu
1237
1238 @node Version Systems
1239 @subsubsection Supported Version Control Systems
1240
1241 @cindex back end (version control)
1242 VC currently works with six different version control systems or
1243 ``back ends'': CVS, GNU Arch, RCS, Meta-CVS, Subversion, and SCCS.
1244
1245 @cindex CVS
1246 CVS is a free version control system that is used for the majority
1247 of free software projects today. It allows concurrent multi-user
1248 development either locally or over the network. Some of its
1249 shortcomings, corrected by newer systems such as GNU Arch, are that it
1250 lacks atomic commits or support for renaming files. VC supports all
1251 basic editing operations under CVS, but for some less common tasks you
1252 still need to call CVS from the command line. Note also that before
1253 using CVS you must set up a repository, which is a subject too complex
1254 to treat here.
1255
1256 @cindex GNU Arch
1257 @cindex Arch
1258 GNU Arch is a new version control system that is designed for
1259 distributed work. It differs in many ways from old well-known
1260 systems, such as CVS and RCS. It supports different transports for
1261 interoperating between users, offline operations, and it has good
1262 branching and merging features. It also supports atomic commits, and
1263 history of file renaming and moving. VC does not support all
1264 operations provided by GNU Arch, so you must sometimes invoke it from
1265 the command line, or use a specialized module.
1266
1267 @cindex RCS
1268 RCS is the free version control system around which VC was initially
1269 built. The VC commands are therefore conceptually closest to RCS.
1270 Almost everything you can do with RCS can be done through VC. You
1271 cannot use RCS over the network though, and it only works at the level
1272 of individual files, rather than projects. You should use it if you
1273 want a simple, yet reliable tool for handling individual files.
1274
1275 @cindex SVN
1276 @cindex Subversion
1277 Subversion is a free version control system designed to be similar
1278 to CVS but without CVS's problems. Subversion supports atomic commits,
1279 and versions directories, symbolic links, meta-data, renames, copies,
1280 and deletes. It can be used via http or via its own protocol.
1281
1282 @cindex MCVS
1283 @cindex Meta-CVS
1284 Meta-CVS is another attempt to solve problems arising in CVS. It
1285 supports directory structure versioning, improved branching and
1286 merging, and use of symbolic links and meta-data in repositories.
1287
1288 @cindex SCCS
1289 SCCS is a proprietary but widely used version control system. In
1290 terms of capabilities, it is the weakest of the six that VC supports.
1291 VC compensates for certain features missing in SCCS (snapshots, for
1292 example) by implementing them itself, but some other VC features, such
1293 as multiple branches, are not available with SCCS. Since SCCS is
1294 non-free, not respecting its users freedom, you should not use it;
1295 use its free replacement CSSC instead. But you should use CSSC only
1296 if for some reason you cannot use RCS, or one of the higher-level
1297 systems such as CVS or GNU Arch.
1298
1299 In the following, we discuss mainly RCS, SCCS and CVS. Nearly
1300 everything said about CVS applies to GNU Arch, Subversion and Meta-CVS
1301 as well.
1302
1303 @node VC Concepts
1304 @subsubsection Concepts of Version Control
1305
1306 @cindex master file
1307 @cindex registered file
1308 When a file is under version control, we also say that it is
1309 @dfn{registered} in the version control system. Each registered file
1310 has a corresponding @dfn{master file} which represents the file's
1311 present state plus its change history---enough to reconstruct the
1312 current version or any earlier version. Usually the master file also
1313 records a @dfn{log entry} for each version, describing in words what was
1314 changed in that version.
1315
1316 @cindex work file
1317 @cindex checking out files
1318 The file that is maintained under version control is sometimes called
1319 the @dfn{work file} corresponding to its master file. You edit the work
1320 file and make changes in it, as you would with an ordinary file. (With
1321 SCCS and RCS, you must @dfn{lock} the file before you start to edit it.)
1322 After you are done with a set of changes, you @dfn{check the file in},
1323 which records the changes in the master file, along with a log entry for
1324 them.
1325
1326 With CVS, there are usually multiple work files corresponding to a
1327 single master file---often each user has his own copy. It is also
1328 possible to use RCS in this way, but this is not the usual way to use
1329 RCS.
1330
1331 @cindex locking and version control
1332 A version control system typically has some mechanism to coordinate
1333 between users who want to change the same file. One method is
1334 @dfn{locking} (analogous to the locking that Emacs uses to detect
1335 simultaneous editing of a file, but distinct from it). The other method
1336 is to merge your changes with other people's changes when you check them
1337 in.
1338
1339 With version control locking, work files are normally read-only so
1340 that you cannot change them. You ask the version control system to make
1341 a work file writable for you by locking it; only one user can do
1342 this at any given time. When you check in your changes, that unlocks
1343 the file, making the work file read-only again. This allows other users
1344 to lock the file to make further changes. SCCS always uses locking, and
1345 RCS normally does.
1346
1347 The other alternative for RCS is to let each user modify the work file
1348 at any time. In this mode, locking is not required, but it is
1349 permitted; check-in is still the way to record a new version.
1350
1351 CVS normally allows each user to modify his own copy of the work file
1352 at any time, but requires merging with changes from other users at
1353 check-in time. However, CVS can also be set up to require locking.
1354 (@pxref{CVS Options,,,emacs-xtra, Specialized Emacs Features}).
1355
1356 @node Types of Log File
1357 @subsubsection Types of Log File
1358 @cindex types of log file
1359 @cindex log File, types of
1360 @cindex version control log
1361
1362 Projects that use a revision control system can have @emph{two}
1363 types of log for changes. One is the per-file log maintained by the
1364 revision control system: each time you check in a change, you must
1365 fill out a @dfn{log entry} for the change (@pxref{Log Buffer}). This
1366 kind of log is called the @dfn{version control log}, also the
1367 @dfn{revision control log}, @dfn{RCS log}, or @dfn{CVS log}.
1368
1369 The other kind of log is the file @file{ChangeLog} (@pxref{Change
1370 Log}). It provides a chronological record of all changes to a large
1371 portion of a program---typically one directory and its subdirectories.
1372 A small program would use one @file{ChangeLog} file; a large program
1373 may well merit a @file{ChangeLog} file in each major directory.
1374 @xref{Change Log}.
1375
1376 A project maintained with version control can use just the per-file
1377 log, or it can use both kinds of logs. It can handle some files one
1378 way and some files the other way. Each project has its policy, which
1379 you should follow.
1380
1381 When the policy is to use both, you typically want to write an entry
1382 for each change just once, then put it into both logs. You can write
1383 the entry in @file{ChangeLog}, then copy it to the log buffer when you
1384 check in the change. Or you can write the entry in the log buffer
1385 while checking in the change, and later use the @kbd{C-x v a} command
1386 to copy it to @file{ChangeLog} (@pxref{Change Logs and
1387 VC,,,emacs-xtra, Specialized Emacs Features}).
1388
1389 @node VC Mode Line
1390 @subsection Version Control and the Mode Line
1391
1392 When you visit a file that is under version control, Emacs indicates
1393 this on the mode line. For example, @samp{RCS-1.3} says that RCS is
1394 used for that file, and the current version is 1.3.
1395
1396 The character between the back-end name and the version number
1397 indicates the version control status of the file. @samp{-} means that
1398 the work file is not locked (if locking is in use), or not modified (if
1399 locking is not in use). @samp{:} indicates that the file is locked, or
1400 that it is modified. If the file is locked by some other user (for
1401 instance, @samp{jim}), that is displayed as @samp{RCS:jim:1.3}.
1402
1403 @vindex auto-revert-check-vc-info
1404 When Auto Revert mode (@pxref{Reverting}) reverts a buffer that is
1405 under version control, it updates the version control information in
1406 the mode line. However, Auto Revert mode may not properly update this
1407 information if the version control status changes without changes to
1408 the work file, from outside the current Emacs session. If you set
1409 @code{auto-revert-check-vc-info} to @code{t}, Auto Revert mode updates
1410 the version control status information every
1411 @code{auto-revert-interval} seconds, even if the work file itself is
1412 unchanged. The resulting CPU usage depends on the version control
1413 system, but is usually not excessive.
1414
1415 @node Basic VC Editing
1416 @subsection Basic Editing under Version Control
1417
1418 The principal VC command is an all-purpose command that performs
1419 either locking or check-in, depending on the situation.
1420
1421 @table @kbd
1422 @itemx C-x v v
1423 Perform the next logical version control operation on this file.
1424 @end table
1425
1426 @findex vc-next-action
1427 @kindex C-x v v
1428 The precise action of this command depends on the state of the file,
1429 and whether the version control system uses locking or not. SCCS and
1430 RCS normally use locking; CVS normally does not use locking.
1431
1432 @findex vc-toggle-read-only
1433 @kindex C-x C-q @r{(Version Control)}
1434 As a special convenience that is particularly useful for files with
1435 locking, you can let Emacs check a file in or out whenever you change
1436 its read-only flag. This means, for example, that you cannot
1437 accidentally edit a file without properly checking it out first. To
1438 achieve this, bind the key @kbd{C-x C-q} to @kbd{vc-toggle-read-only}
1439 in your @file{~/.emacs} file. (@xref{Init Rebinding}.)
1440
1441 @menu
1442 * VC with Locking:: RCS in its default mode, SCCS, and optionally CVS.
1443 * Without Locking:: Without locking: default mode for CVS.
1444 * Advanced C-x v v:: Advanced features available with a prefix argument.
1445 * Log Buffer:: Features available in log entry buffers.
1446 @end menu
1447
1448 @node VC with Locking
1449 @subsubsection Basic Version Control with Locking
1450
1451 If locking is used for the file (as with SCCS, and RCS in its default
1452 mode), @kbd{C-x v v} can either lock a file or check it in:
1453
1454 @itemize @bullet
1455 @item
1456 If the file is not locked, @kbd{C-x v v} locks it, and
1457 makes it writable so that you can change it.
1458
1459 @item
1460 If the file is locked by you, and contains changes, @kbd{C-x v v} checks
1461 in the changes. In order to do this, it first reads the log entry
1462 for the new version. @xref{Log Buffer}.
1463
1464 @item
1465 If the file is locked by you, but you have not changed it since you
1466 locked it, @kbd{C-x v v} releases the lock and makes the file read-only
1467 again.
1468
1469 @item
1470 If the file is locked by some other user, @kbd{C-x v v} asks you whether
1471 you want to ``steal the lock'' from that user. If you say yes, the file
1472 becomes locked by you, but a message is sent to the person who had
1473 formerly locked the file, to inform him of what has happened.
1474 @end itemize
1475
1476 These rules also apply when you use CVS in locking mode, except
1477 that there is no such thing as stealing a lock.
1478
1479 @node Without Locking
1480 @subsubsection Basic Version Control without Locking
1481
1482 When there is no locking---the default for CVS---work files are always
1483 writable; you do not need to do anything before you begin to edit a
1484 file. The status indicator on the mode line is @samp{-} if the file is
1485 unmodified; it flips to @samp{:} as soon as you save any changes in the
1486 work file.
1487
1488 Here is what @kbd{C-x v v} does when using CVS:
1489
1490 @itemize @bullet
1491 @item
1492 If some other user has checked in changes into the master file, Emacs
1493 asks you whether you want to merge those changes into your own work
1494 file. You must do this before you can check in your own changes. (To
1495 pick up any recent changes from the master file @emph{without} trying
1496 to commit your own changes, type @kbd{C-x v m @key{RET}}.)
1497 @xref{Merging}.
1498
1499 @item
1500 If there are no new changes in the master file, but you have made
1501 modifications in your work file, @kbd{C-x v v} checks in your changes.
1502 In order to do this, it first reads the log entry for the new version.
1503 @xref{Log Buffer}.
1504
1505 @item
1506 If the file is not modified, the @kbd{C-x v v} does nothing.
1507 @end itemize
1508
1509 These rules also apply when you use RCS in the mode that does not
1510 require locking, except that automatic merging of changes from the
1511 master file is not implemented. Unfortunately, this means that nothing
1512 informs you if another user has checked in changes in the same file
1513 since you began editing it, and when this happens, his changes will be
1514 effectively removed when you check in your version (though they will
1515 remain in the master file, so they will not be entirely lost). You must
1516 therefore verify that the current version is unchanged, before you
1517 check in your changes. We hope to eliminate this risk and provide
1518 automatic merging with RCS in a future Emacs version.
1519
1520 In addition, locking is possible with RCS even in this mode, although
1521 it is not required; @kbd{C-x v v} with an unmodified file locks the
1522 file, just as it does with RCS in its normal (locking) mode.
1523
1524 @node Advanced C-x v v
1525 @subsubsection Advanced Control in @kbd{C-x v v}
1526
1527 @cindex version number to check in/out
1528 When you give a prefix argument to @code{vc-next-action} (@kbd{C-u
1529 C-x v v}), it still performs the next logical version control
1530 operation, but accepts additional arguments to specify precisely how
1531 to do the operation.
1532
1533 @itemize @bullet
1534 @item
1535 If the file is modified (or locked), you can specify the version
1536 number to use for the new version that you check in. This is one way
1537 to create a new branch (@pxref{Branches}).
1538
1539 @item
1540 If the file is not modified (and unlocked), you can specify the
1541 version to select; this lets you start working from an older version,
1542 or on another branch. If you do not enter any version, that takes you
1543 to the highest version on the current branch; therefore @kbd{C-u C-x
1544 v v @key{RET}} is a convenient way to get the latest version of a file from
1545 the repository.
1546
1547 @item
1548 @cindex specific version control system
1549 Instead of the version number, you can also specify the name of a
1550 version control system. This is useful when one file is being managed
1551 with two version control systems at the same time (@pxref{Local
1552 Version Control,,,emacs-xtra, Specialized Emacs Features}).
1553 @end itemize
1554
1555 @node Log Buffer
1556 @subsubsection Features of the Log Entry Buffer
1557
1558 When you check in changes, @kbd{C-x v v} first reads a log entry. It
1559 pops up a buffer called @samp{*VC-Log*} for you to enter the log entry.
1560
1561 Sometimes the @samp{*VC-Log*} buffer contains default text when you enter it,
1562 typically the last log message entered. If it does, mark and point
1563 are set around the entire contents of the buffer so that it is easy to
1564 kill the contents of the buffer with @kbd{C-w}.
1565
1566 @findex log-edit-insert-changelog
1567 If you work by writing entries in the @file{ChangeLog}
1568 (@pxref{Change Log}) and then commit the change under revision
1569 control, you can generate the Log Edit text from the ChangeLog using
1570 @kbd{C-c C-a} (@kbd{log-edit-insert-changelog}). This looks for
1571 entries for the file(s) concerned in the top entry in the ChangeLog
1572 and uses those paragraphs as the log text. This text is only inserted
1573 if the top entry was made under your user name on the current date.
1574 @xref{Change Logs and VC,,,emacs-xtra, Specialized Emacs Features},
1575 for the opposite way of working---generating ChangeLog entries from
1576 the revision control log.
1577
1578 In the @samp{*VC-Log*} buffer, @kbd{C-c C-f} (@kbd{M-x
1579 log-edit-show-files}) shows the list of files to be committed in case
1580 you need to check that. (This can be a list of more than one file if
1581 you use VC Dired mode or PCL-CVS. @xref{VC Dired Mode,,,emacs-xtra,
1582 Specialized Emacs Features}, and @ref{Top, , About PCL-CVS, pcl-cvs,
1583 PCL-CVS --- The Emacs Front-End to CVS}.)
1584
1585 When you have finished editing the log message, type @kbd{C-c C-c} to
1586 exit the buffer and commit the change.
1587
1588 To abort check-in, just @strong{don't} type @kbd{C-c C-c} in that
1589 buffer. You can switch buffers and do other editing. As long as you
1590 don't try to check in another file, the entry you were editing remains
1591 in the @samp{*VC-Log*} buffer, and you can go back to that buffer at any
1592 time to complete the check-in.
1593
1594 If you change several source files for the same reason, it is often
1595 convenient to specify the same log entry for many of the files. To do
1596 this, use the history of previous log entries. The commands @kbd{M-n},
1597 @kbd{M-p}, @kbd{M-s} and @kbd{M-r} for doing this work just like the
1598 minibuffer history commands (except that these versions are used outside
1599 the minibuffer).
1600
1601 @vindex vc-log-mode-hook
1602 Each time you check in a file, the log entry buffer is put into VC Log
1603 mode, which involves running two hooks: @code{text-mode-hook} and
1604 @code{vc-log-mode-hook}. @xref{Hooks}.
1605
1606 @node Old Versions
1607 @subsection Examining And Comparing Old Versions
1608
1609 One of the convenient features of version control is the ability
1610 to examine any version of a file, or compare two versions.
1611
1612 @table @kbd
1613 @item C-x v ~ @var{version} @key{RET}
1614 Examine version @var{version} of the visited file, in a buffer of its
1615 own.
1616
1617 @item C-x v =
1618 Compare the current buffer contents with the master version from which
1619 you started editing.
1620
1621 @item C-u C-x v = @var{file} @key{RET} @var{oldvers} @key{RET} @var{newvers} @key{RET}
1622 Compare the specified two versions of @var{file}.
1623
1624 @item C-x v g
1625 Display the file with per-line version information and using colors.
1626 @end table
1627
1628 @findex vc-version-other-window
1629 @kindex C-x v ~
1630 To examine an old version in its entirety, visit the file and then type
1631 @kbd{C-x v ~ @var{version} @key{RET}} (@code{vc-version-other-window}).
1632 This puts the text of version @var{version} in a file named
1633 @file{@var{filename}.~@var{version}~}, and visits it in its own buffer
1634 in a separate window. (In RCS, you can also select an old version
1635 and create a branch from it. @xref{Branches}.)
1636
1637 @findex vc-diff
1638 @kindex C-x v =
1639 It is usually more convenient to compare two versions of the file,
1640 with the command @kbd{C-x v =} (@code{vc-diff}). Plain @kbd{C-x v =}
1641 compares the current buffer contents (saving them in the file if
1642 necessary) with the master version from which you started editing the
1643 file (this is not necessarily the latest version of the file).
1644 @kbd{C-u C-x v =}, with a numeric argument, reads a file name and two
1645 version numbers, then compares those versions of the specified file.
1646 Both forms display the output in a special buffer in another window.
1647
1648 You can specify a checked-in version by its number; an empty input
1649 specifies the current contents of the work file (which may be different
1650 from all the checked-in versions). You can also specify a snapshot name
1651 (@pxref{Snapshots,,,emacs-xtra, Specialized Emacs Features}) instead of one or both version numbers.
1652
1653 If you supply a directory name instead of the name of a registered
1654 file, this command compares the two specified versions of all registered
1655 files in that directory and its subdirectories.
1656
1657 @vindex vc-diff-switches
1658 @vindex vc-rcs-diff-switches
1659 @kbd{C-x v =} works by running a variant of the @code{diff} utility
1660 designed to work with the version control system in use. When you
1661 invoke @code{diff} this way, in addition to the options specified by
1662 @code{diff-switches} (@pxref{Comparing Files}), it receives those
1663 specified by @code{vc-diff-switches}, plus those specified for the
1664 specific back end by @code{vc-@var{backend}-diff-switches}. For
1665 instance, when the version control back end is RCS, @code{diff} uses
1666 the options in @code{vc-rcs-diff-switches}. The
1667 @samp{vc@dots{}diff-switches} variables are @code{nil} by default.
1668
1669 The buffer produced by @kbd{C-x v =} supports the commands of
1670 Compilation mode (@pxref{Compilation Mode}), such as @kbd{C-x `} and
1671 @kbd{C-c C-c}, in both the ``old'' and ``new'' text, and they always
1672 find the corresponding locations in the current work file. (Older
1673 versions are not, in general, present as files on your disk.)
1674
1675 @findex vc-annotate
1676 @kindex C-x v g
1677 For some back ends, you can display the file @dfn{annotated} with
1678 per-line version information and using colors to enhance the visual
1679 appearance, with the command @kbd{M-x vc-annotate}. It creates a new
1680 buffer (the ``annotate buffer'') displaying the file's text, with each
1681 part colored to show how old it is. Text colored red is new, blue means
1682 old, and intermediate colors indicate intermediate ages. By default,
1683 the color is scaled over the full range of ages, such that the oldest
1684 changes are blue, and the newest changes are red.
1685
1686 When you give a prefix argument to this command, it uses the
1687 minibuffer to read two arguments: which version number to display and
1688 annotate (instead of the current file contents), and the time span in
1689 days the color range should cover.
1690
1691 From the annotate buffer, these and other color scaling options are
1692 available from the @samp{VC-Annotate} menu. In this buffer, you can
1693 also use the following keys to browse the annotations of past revisions,
1694 view diffs, or view log entries:
1695
1696 @table @kbd
1697 @item P
1698 Annotate the previous revision, that is to say, the revision before
1699 the one currently annotated. A numeric prefix argument is a repeat
1700 count, so @kbd{C-u 10 P} would take you back 10 revisions.
1701
1702 @item N
1703 Annotate the next revision---the one after the revision currently
1704 annotated. A numeric prefix argument is a repeat count.
1705
1706 @item J
1707 Annotate the revision indicated by the current line.
1708
1709 @item A
1710 Annotate the revision before the one indicated by the current line.
1711 This is useful to see the state the file was in before the change on
1712 the current line was made.
1713
1714 @item D
1715 Display the diff between the current line's revision and the previous
1716 revision. This is useful to see what the current line's revision
1717 actually changed in the file.
1718
1719 @item L
1720 Show the log of the current line's revision. This is useful to see
1721 the author's description of the changes in the revision on the current
1722 line.
1723
1724 @item W
1725 Annotate the workfile version--the one you are editing. If you used
1726 @kbd{P} and @kbd{N} to browse to other revisions, use this key to
1727 return to your current version.
1728 @end table
1729
1730 @node Secondary VC Commands
1731 @subsection The Secondary Commands of VC
1732
1733 This section explains the secondary commands of VC; those that you might
1734 use once a day.
1735
1736 @menu
1737 * Registering:: Putting a file under version control.
1738 * VC Status:: Viewing the VC status of files.
1739 * VC Undo:: Canceling changes before or after check-in.
1740 @end menu
1741
1742 @node Registering
1743 @subsubsection Registering a File for Version Control
1744
1745 @kindex C-x v i
1746 @findex vc-register
1747 You can put any file under version control by simply visiting it, and
1748 then typing @w{@kbd{C-x v i}} (@code{vc-register}).
1749
1750 @table @kbd
1751 @item C-x v i
1752 Register the visited file for version control.
1753 @end table
1754
1755 To register the file, Emacs must choose which version control system
1756 to use for it. If the file's directory already contains files
1757 registered in a version control system, Emacs uses that system. If
1758 there is more than one system in use for a directory, Emacs uses the
1759 one that appears first in @code{vc-handled-backends}
1760 (@pxref{Customizing VC,,,emacs-xtra, Specialized Emacs Features}). On
1761 the other hand, if there are no files already registered, Emacs uses
1762 the first system from @code{vc-handled-backends} that could register
1763 the file (for example, you cannot register a file under CVS if its
1764 directory is not already part of a CVS tree); with the default value
1765 of @code{vc-handled-backends}, this means that Emacs uses RCS in this
1766 situation.
1767
1768 If locking is in use, @kbd{C-x v i} leaves the file unlocked and
1769 read-only. Type @kbd{C-x v v} if you wish to start editing it. After
1770 registering a file with CVS, you must subsequently commit the initial
1771 version by typing @kbd{C-x v v}. Until you do that, the version
1772 appears as @samp{@@@@} in the mode line.
1773
1774 @vindex vc-default-init-version
1775 @cindex initial version number to register
1776 The initial version number for a newly registered file is 1.1, by
1777 default. You can specify a different default by setting the variable
1778 @code{vc-default-init-version}, or you can give @kbd{C-x v i} a numeric
1779 argument; then it reads the initial version number for this particular
1780 file using the minibuffer.
1781
1782 @vindex vc-initial-comment
1783 If @code{vc-initial-comment} is non-@code{nil}, @kbd{C-x v i} reads an
1784 initial comment to describe the purpose of this source file. Reading
1785 the initial comment works like reading a log entry (@pxref{Log Buffer}).
1786
1787 @node VC Status
1788 @subsubsection VC Status Commands
1789
1790 @table @kbd
1791 @item C-x v l
1792 Display version control state and change history.
1793 @end table
1794
1795 @kindex C-x v l
1796 @findex vc-print-log
1797 To view the detailed version control status and history of a file,
1798 type @kbd{C-x v l} (@code{vc-print-log}). It displays the history of
1799 changes to the current file, including the text of the log entries. The
1800 output appears in a separate window. The point is centered at the
1801 revision of the file that is currently being visited.
1802
1803 In the change log buffer, you can use the following keys to move
1804 between the logs of revisions and of files, to view past revisions, and
1805 to view diffs:
1806
1807 @table @kbd
1808 @item p
1809 Move to the previous revision-item in the buffer. (Revision entries in the log
1810 buffer are usually in reverse-chronological order, so the previous
1811 revision-item usually corresponds to a newer revision.) A numeric
1812 prefix argument is a repeat count.
1813
1814 @item n
1815 Move to the next revision-item (which most often corresponds to the
1816 previous revision of the file). A numeric prefix argument is a repeat
1817 count.
1818
1819 @item P
1820 Move to the log of the previous file, when the logs of multiple files
1821 are in the log buffer (@pxref{VC Dired Mode,,,emacs-xtra, Specialized
1822 Emacs Features}). Otherwise, just move to the beginning of the log. A
1823 numeric prefix argument is a repeat count, so @kbd{C-u 10 P} would
1824 move backward 10 files.
1825
1826 @item N
1827 Move to the log of the next file, when the logs of multiple files are
1828 in the log buffer (@pxref{VC Dired Mode,,,emacs-xtra, Specialized
1829 Emacs Features}). It also takes a numeric prefix argument as a repeat
1830 count.
1831
1832 @item f
1833 Visit the revision indicated at the current line, like typing @kbd{C-x
1834 v ~} and specifying this revision's number (@pxref{Old Versions}).
1835
1836 @item d
1837 Display the diff (@pxref{Comparing Files}) between the revision
1838 indicated at the current line and the next earlier revision. This is
1839 useful to see what actually changed when the revision indicated on the
1840 current line was committed.
1841 @end table
1842
1843 @node VC Undo
1844 @subsubsection Undoing Version Control Actions
1845
1846 @table @kbd
1847 @item C-x v u
1848 Revert the buffer and the file to the version from which you started
1849 editing the file.
1850
1851 @item C-x v c
1852 Remove the last-entered change from the master for the visited file.
1853 This undoes your last check-in.
1854 @end table
1855
1856 @kindex C-x v u
1857 @findex vc-revert-buffer
1858 If you want to discard your current set of changes and revert to the
1859 version from which you started editing the file, use @kbd{C-x v u}
1860 (@code{vc-revert-buffer}). This leaves the file unlocked; if locking
1861 is in use, you must first lock the file again before you change it
1862 again. @kbd{C-x v u} requires confirmation, unless it sees that you
1863 haven't made any changes with respect to the master version.
1864
1865 @kbd{C-x v u} is also the command to unlock a file if you lock it and
1866 then decide not to change it.
1867
1868 @kindex C-x v c
1869 @findex vc-cancel-version
1870 To cancel a change that you already checked in, use @kbd{C-x v c}
1871 (@code{vc-cancel-version}). This command discards all record of the
1872 most recent checked-in version, but only if your work file corresponds
1873 to that version---you cannot use @kbd{C-x v c} to cancel a version
1874 that is not the latest on its branch. @kbd{C-x v c} also offers to
1875 revert your work file and buffer to the previous version (the one that
1876 precedes the version that is deleted).
1877
1878 If you answer @kbd{no}, VC keeps your changes in the buffer, and locks
1879 the file. The no-revert option is useful when you have checked in a
1880 change and then discover a trivial error in it; you can cancel the
1881 erroneous check-in, fix the error, and check the file in again.
1882
1883 When @kbd{C-x v c} does not revert the buffer, it unexpands all
1884 version control headers in the buffer instead (@pxref{Version
1885 Headers,,,emacs-xtra, Specialized Emacs Features}). This is because
1886 the buffer no longer corresponds to any existing version. If you
1887 check it in again, the check-in process will expand the headers
1888 properly for the new version number.
1889
1890 However, it is impossible to unexpand the RCS @samp{@w{$}Log$} header
1891 automatically. If you use that header feature, you have to unexpand it
1892 by hand---by deleting the entry for the version that you just canceled.
1893
1894 Be careful when invoking @kbd{C-x v c}, as it is easy to lose a lot of
1895 work with it. To help you be careful, this command always requires
1896 confirmation with @kbd{yes}. Note also that this command is disabled
1897 under CVS, because canceling versions is very dangerous and discouraged
1898 with CVS.
1899
1900 @node Branches
1901 @subsection Multiple Branches of a File
1902 @cindex branch (version control)
1903 @cindex trunk (version control)
1904
1905 One use of version control is to maintain multiple ``current''
1906 versions of a file. For example, you might have different versions of a
1907 program in which you are gradually adding various unfinished new
1908 features. Each such independent line of development is called a
1909 @dfn{branch}. VC allows you to create branches, switch between
1910 different branches, and merge changes from one branch to another.
1911 Please note, however, that branches are not supported for SCCS.
1912
1913 A file's main line of development is usually called the @dfn{trunk}.
1914 The versions on the trunk are normally numbered 1.1, 1.2, 1.3, etc. At
1915 any such version, you can start an independent branch. A branch
1916 starting at version 1.2 would have version number 1.2.1.1, and consecutive
1917 versions on this branch would have numbers 1.2.1.2, 1.2.1.3, 1.2.1.4,
1918 and so on. If there is a second branch also starting at version 1.2, it
1919 would consist of versions 1.2.2.1, 1.2.2.2, 1.2.2.3, etc.
1920
1921 @cindex head version
1922 If you omit the final component of a version number, that is called a
1923 @dfn{branch number}. It refers to the highest existing version on that
1924 branch---the @dfn{head version} of that branch. The branches in the
1925 example above have branch numbers 1.2.1 and 1.2.2.
1926
1927 @menu
1928 * Switching Branches:: How to get to another existing branch.
1929 * Creating Branches:: How to start a new branch.
1930 * Merging:: Transferring changes between branches.
1931 * Multi-User Branching:: Multiple users working at multiple branches
1932 in parallel.
1933 @end menu
1934
1935 @node Switching Branches
1936 @subsubsection Switching between Branches
1937
1938 To switch between branches, type @kbd{C-u C-x v v} and specify the
1939 version number you want to select. This version is then visited
1940 @emph{unlocked} (write-protected), so you can examine it before locking
1941 it. Switching branches in this way is allowed only when the file is not
1942 locked.
1943
1944 You can omit the minor version number, thus giving only the branch
1945 number; this takes you to the head version on the chosen branch. If you
1946 only type @key{RET}, Emacs goes to the highest version on the trunk.
1947
1948 After you have switched to any branch (including the main branch), you
1949 stay on it for subsequent VC commands, until you explicitly select some
1950 other branch.
1951
1952 @node Creating Branches
1953 @subsubsection Creating New Branches
1954
1955 To create a new branch from a head version (one that is the latest in
1956 the branch that contains it), first select that version if necessary,
1957 lock it with @kbd{C-x v v}, and make whatever changes you want. Then,
1958 when you check in the changes, use @kbd{C-u C-x v v}. This lets you
1959 specify the version number for the new version. You should specify a
1960 suitable branch number for a branch starting at the current version.
1961 For example, if the current version is 2.5, the branch number should be
1962 2.5.1, 2.5.2, and so on, depending on the number of existing branches at
1963 that point.
1964
1965 To create a new branch at an older version (one that is no longer the
1966 head of a branch), first select that version (@pxref{Switching
1967 Branches}), then lock it with @kbd{C-x v v}. You'll be asked to
1968 confirm, when you lock the old version, that you really mean to create a
1969 new branch---if you say no, you'll be offered a chance to lock the
1970 latest version instead.
1971
1972 Then make your changes and type @kbd{C-x v v} again to check in a new
1973 version. This automatically creates a new branch starting from the
1974 selected version. You need not specially request a new branch, because
1975 that's the only way to add a new version at a point that is not the head
1976 of a branch.
1977
1978 After the branch is created, you ``stay'' on it. That means that
1979 subsequent check-ins create new versions on that branch. To leave the
1980 branch, you must explicitly select a different version with @kbd{C-u C-x
1981 v v}. To transfer changes from one branch to another, use the merge
1982 command, described in the next section.
1983
1984 @node Merging
1985 @subsubsection Merging Branches
1986
1987 @cindex merging changes
1988 When you have finished the changes on a certain branch, you will
1989 often want to incorporate them into the file's main line of development
1990 (the trunk). This is not a trivial operation, because development might
1991 also have proceeded on the trunk, so that you must @dfn{merge} the
1992 changes into a file that has already been changed otherwise. VC allows
1993 you to do this (and other things) with the @code{vc-merge} command.
1994
1995 @table @kbd
1996 @item C-x v m (vc-merge)
1997 Merge changes into the work file.
1998 @end table
1999
2000 @kindex C-x v m
2001 @findex vc-merge
2002 @kbd{C-x v m} (@code{vc-merge}) takes a set of changes and merges it
2003 into the current version of the work file. It firsts asks you in the
2004 minibuffer where the changes should come from. If you just type
2005 @key{RET}, Emacs merges any changes that were made on the same branch
2006 since you checked the file out (we call this @dfn{merging the news}).
2007 This is the common way to pick up recent changes from the repository,
2008 regardless of whether you have already changed the file yourself.
2009
2010 You can also enter a branch number or a pair of version numbers in
2011 the minibuffer. Then @kbd{C-x v m} finds the changes from that
2012 branch, or the differences between the two versions you specified, and
2013 merges them into the current version of the current file.
2014
2015 As an example, suppose that you have finished a certain feature on
2016 branch 1.3.1. In the meantime, development on the trunk has proceeded
2017 to version 1.5. To merge the changes from the branch to the trunk,
2018 first go to the head version of the trunk, by typing @kbd{C-u C-x v v
2019 @key{RET}}. Version 1.5 is now current. If locking is used for the file,
2020 type @kbd{C-x v v} to lock version 1.5 so that you can change it. Next,
2021 type @kbd{C-x v m 1.3.1 @key{RET}}. This takes the entire set of changes on
2022 branch 1.3.1 (relative to version 1.3, where the branch started, up to
2023 the last version on the branch) and merges it into the current version
2024 of the work file. You can now check in the changed file, thus creating
2025 version 1.6 containing the changes from the branch.
2026
2027 It is possible to do further editing after merging the branch, before
2028 the next check-in. But it is usually wiser to check in the merged
2029 version, then lock it and make the further changes. This will keep
2030 a better record of the history of changes.
2031
2032 @cindex conflicts
2033 @cindex resolving conflicts
2034 When you merge changes into a file that has itself been modified, the
2035 changes might overlap. We call this situation a @dfn{conflict}, and
2036 reconciling the conflicting changes is called @dfn{resolving a
2037 conflict}.
2038
2039 Whenever conflicts occur during merging, VC detects them, tells you
2040 about them in the echo area, and asks whether you want help in merging.
2041 If you say yes, it starts an Ediff session (@pxref{Top,
2042 Ediff, Ediff, ediff, The Ediff Manual}).
2043
2044 If you say no, the conflicting changes are both inserted into the
2045 file, surrounded by @dfn{conflict markers}. The example below shows how
2046 a conflict region looks; the file is called @samp{name} and the current
2047 master file version with user B's changes in it is 1.11.
2048
2049 @c @w here is so CVS won't think this is a conflict.
2050 @smallexample
2051 @group
2052 @w{<}<<<<<< name
2053 @var{User A's version}
2054 =======
2055 @var{User B's version}
2056 @w{>}>>>>>> 1.11
2057 @end group
2058 @end smallexample
2059
2060 @cindex vc-resolve-conflicts
2061 Then you can resolve the conflicts by editing the file manually. Or
2062 you can type @code{M-x vc-resolve-conflicts} after visiting the file.
2063 This starts an Ediff session, as described above. Don't forget to
2064 check in the merged version afterwards.
2065
2066 @node Multi-User Branching
2067 @subsubsection Multi-User Branching
2068
2069 It is often useful for multiple developers to work simultaneously on
2070 different branches of a file. CVS allows this by default; for RCS, it
2071 is possible if you create multiple source directories. Each source
2072 directory should have a link named @file{RCS} which points to a common
2073 directory of RCS master files. Then each source directory can have its
2074 own choice of selected versions, but all share the same common RCS
2075 records.
2076
2077 This technique works reliably and automatically, provided that the
2078 source files contain RCS version headers (@pxref{Version
2079 Headers,,,emacs-xtra, Specialized Emacs Features}). The headers enable
2080 Emacs to be sure, at all times, which version number is present in the
2081 work file.
2082
2083 If the files do not have version headers, you must instead tell Emacs
2084 explicitly in each session which branch you are working on. To do this,
2085 first find the file, then type @kbd{C-u C-x v v} and specify the correct
2086 branch number. This ensures that Emacs knows which branch it is using
2087 during this particular editing session.
2088
2089 @node Directories
2090 @section File Directories
2091
2092 @cindex file directory
2093 @cindex directory listing
2094 The file system groups files into @dfn{directories}. A @dfn{directory
2095 listing} is a list of all the files in a directory. Emacs provides
2096 commands to create and delete directories, and to make directory
2097 listings in brief format (file names only) and verbose format (sizes,
2098 dates, and authors included). Emacs also includes a directory browser
2099 feature called Dired; see @ref{Dired}.
2100
2101 @table @kbd
2102 @item C-x C-d @var{dir-or-pattern} @key{RET}
2103 Display a brief directory listing (@code{list-directory}).
2104 @item C-u C-x C-d @var{dir-or-pattern} @key{RET}
2105 Display a verbose directory listing.
2106 @item M-x make-directory @key{RET} @var{dirname} @key{RET}
2107 Create a new directory named @var{dirname}.
2108 @item M-x delete-directory @key{RET} @var{dirname} @key{RET}
2109 Delete the directory named @var{dirname}. It must be empty,
2110 or you get an error.
2111 @end table
2112
2113 @findex list-directory
2114 @kindex C-x C-d
2115 The command to display a directory listing is @kbd{C-x C-d}
2116 (@code{list-directory}). It reads using the minibuffer a file name
2117 which is either a directory to be listed or a wildcard-containing
2118 pattern for the files to be listed. For example,
2119
2120 @example
2121 C-x C-d /u2/emacs/etc @key{RET}
2122 @end example
2123
2124 @noindent
2125 lists all the files in directory @file{/u2/emacs/etc}. Here is an
2126 example of specifying a file name pattern:
2127
2128 @example
2129 C-x C-d /u2/emacs/src/*.c @key{RET}
2130 @end example
2131
2132 Normally, @kbd{C-x C-d} displays a brief directory listing containing
2133 just file names. A numeric argument (regardless of value) tells it to
2134 make a verbose listing including sizes, dates, and owners (like
2135 @samp{ls -l}).
2136
2137 @vindex list-directory-brief-switches
2138 @vindex list-directory-verbose-switches
2139 The text of a directory listing is mostly obtained by running
2140 @code{ls} in an inferior process. Two Emacs variables control the
2141 switches passed to @code{ls}: @code{list-directory-brief-switches} is
2142 a string giving the switches to use in brief listings (@code{"-CF"} by
2143 default), and @code{list-directory-verbose-switches} is a string
2144 giving the switches to use in a verbose listing (@code{"-l"} by
2145 default).
2146
2147 @vindex directory-free-space-program
2148 @vindex directory-free-space-args
2149 In verbose directory listings, Emacs adds information about the
2150 amount of free space on the disk that contains the directory. To do
2151 this, it runs the program specified by
2152 @code{directory-free-space-program} with arguments
2153 @code{directory-free-space-args}.
2154
2155 @node Comparing Files
2156 @section Comparing Files
2157 @cindex comparing files
2158
2159 @findex diff
2160 @vindex diff-switches
2161 The command @kbd{M-x diff} compares two files, displaying the
2162 differences in an Emacs buffer named @samp{*diff*}. It works by
2163 running the @code{diff} program, using options taken from the variable
2164 @code{diff-switches}. The value of @code{diff-switches} should be a
2165 string; the default is @code{"-c"} to specify a context diff.
2166 @xref{Top,, Diff, diff, Comparing and Merging Files}, for more
2167 information about @command{diff} output formats.
2168
2169 @findex diff-backup
2170 The command @kbd{M-x diff-backup} compares a specified file with its most
2171 recent backup. If you specify the name of a backup file,
2172 @code{diff-backup} compares it with the source file that it is a backup
2173 of.
2174
2175 @findex compare-windows
2176 The command @kbd{M-x compare-windows} compares the text in the
2177 current window with that in the next window. (For more information
2178 about windows in Emacs, @ref{Windows}.) Comparison starts at point in
2179 each window, after pushing each initial point value on the mark ring
2180 in its respective buffer. Then it moves point forward in each window,
2181 one character at a time, until it reaches characters that don't match.
2182 Then the command exits.
2183
2184 If point in the two windows is followed by non-matching text when
2185 the command starts, @kbd{M-x compare-windows} tries heuristically to
2186 advance up to matching text in the two windows, and then exits. So if
2187 you use @kbd{M-x compare-windows} repeatedly, each time it either
2188 skips one matching range or finds the start of another.
2189
2190 @vindex compare-ignore-case
2191 @vindex compare-ignore-whitespace
2192 With a numeric argument, @code{compare-windows} ignores changes in
2193 whitespace. If the variable @code{compare-ignore-case} is
2194 non-@code{nil}, the comparison ignores differences in case as well.
2195 If the variable @code{compare-ignore-whitespace} is non-@code{nil},
2196 @code{compare-windows} normally ignores changes in whitespace, and a
2197 prefix argument turns that off.
2198
2199 @cindex Smerge mode
2200 @findex smerge-mode
2201 @cindex failed merges
2202 @cindex merges, failed
2203 @cindex comparing 3 files (@code{diff3})
2204 You can use @kbd{M-x smerge-mode} to turn on Smerge mode, a minor
2205 mode for editing output from the @command{diff3} program. This is
2206 typically the result of a failed merge from a version control system
2207 ``update'' outside VC, due to conflicting changes to a file. Smerge
2208 mode provides commands to resolve conflicts by selecting specific
2209 changes.
2210
2211 @inforef{Emerge,, emacs-xtra} for the Emerge facility, which
2212 provides a powerful interface for merging files.
2213
2214 @node Diff Mode
2215 @section Diff Mode
2216 @cindex Diff mode
2217 @findex diff-mode
2218 @cindex patches, editing
2219
2220 Diff mode is used for the output of @kbd{M-x diff}; it is also
2221 useful for editing patches and comparisons produced by the
2222 @command{diff} program. To select Diff mode manually, type @kbd{M-x
2223 diff-mode}.
2224
2225 One general feature of Diff mode is that manual edits to the patch
2226 automatically correct line numbers, including those in the hunk
2227 header, so that you can actually apply the edited patch. Diff mode
2228 also provides the following commands to navigate, manipulate and apply
2229 parts of patches:
2230
2231 @table @kbd
2232 @item M-n
2233 Move to the next hunk-start (@code{diff-hunk-next}).
2234
2235 @item M-p
2236 Move to the previous hunk-start (@code{diff-hunk-prev}).
2237
2238 @item M-@}
2239 Move to the next file-start, in a multi-file patch
2240 (@code{diff-file-next}).
2241
2242 @item M-@{
2243 Move to the previous file-start, in a multi-file patch
2244 (@code{diff-file-prev}).
2245
2246 @item M-k
2247 Kill the hunk at point (@code{diff-hunk-kill}).
2248
2249 @item M-K
2250 In a multi-file patch, kill the current file part.
2251 (@code{diff-file-kill}).
2252
2253 @item C-c C-a
2254 Apply this hunk to its target file (@code{diff-apply-hunk}). With a
2255 prefix argument of @kbd{C-u}, revert this hunk.
2256
2257 @item C-c C-c
2258 Go to the source corresponding to this hunk (@code{diff-goto-source}).
2259
2260 @item C-c C-e
2261 Start an Ediff session with the patch (@code{diff-ediff-patch}).
2262 @xref{Top, Ediff, Ediff, ediff, The Ediff Manual}.
2263
2264 @item C-c C-n
2265 Restrict the view to the current hunk (@code{diff-restrict-view}).
2266 @xref{Narrowing}. With a prefix argument of @kbd{C-u}, restrict the
2267 view to the current patch of a multiple file patch. To widen again,
2268 use @kbd{C-x n w}.
2269
2270 @item C-c C-r
2271 Reverse the direction of comparison for the entire buffer
2272 (@code{diff-reverse-direction}).
2273
2274 @item C-c C-s
2275 Split the hunk at point (@code{diff-split-hunk}). This is for
2276 manually editing patches, and only works with the unified diff format.
2277
2278 @item C-c C-u
2279 Convert the entire buffer to unified format
2280 (@code{diff-context->unified}). With a prefix argument, convert
2281 unified format to context format. In Transient Mark mode, when the
2282 mark is active, this command operates only on the region.
2283
2284 @item C-c C-w
2285 Refine the current hunk so that it disregards changes in whitespace
2286 (@code{diff-refine-hunk}).
2287 @end table
2288
2289 @kbd{C-x 4 a} in Diff mode operates on behalf of the target file,
2290 but gets the function name from the patch itself. @xref{Change Log}.
2291 This is useful for making log entries for functions that are deleted
2292 by the patch.
2293
2294 @node Misc File Ops
2295 @section Miscellaneous File Operations
2296
2297 Emacs has commands for performing many other operations on files.
2298 All operate on one file; they do not accept wildcard file names.
2299
2300 @findex view-file
2301 @cindex viewing
2302 @cindex View mode
2303 @cindex mode, View
2304 @kbd{M-x view-file} allows you to scan or read a file by sequential
2305 screenfuls. It reads a file name argument using the minibuffer. After
2306 reading the file into an Emacs buffer, @code{view-file} displays the
2307 beginning. You can then type @key{SPC} to scroll forward one windowful,
2308 or @key{DEL} to scroll backward. Various other commands are provided
2309 for moving around in the file, but none for changing it; type @kbd{?}
2310 while viewing for a list of them. They are mostly the same as normal
2311 Emacs cursor motion commands. To exit from viewing, type @kbd{q}.
2312 The commands for viewing are defined by a special minor mode called View
2313 mode.
2314
2315 A related command, @kbd{M-x view-buffer}, views a buffer already present
2316 in Emacs. @xref{Misc Buffer}.
2317
2318 @kindex C-x i
2319 @findex insert-file
2320 @kbd{M-x insert-file} (also @kbd{C-x i}) inserts a copy of the
2321 contents of the specified file into the current buffer at point,
2322 leaving point unchanged before the contents and the mark after them.
2323
2324 @findex write-region
2325 @kbd{M-x write-region} is the inverse of @kbd{M-x insert-file}; it
2326 copies the contents of the region into the specified file. @kbd{M-x
2327 append-to-file} adds the text of the region to the end of the
2328 specified file. @xref{Accumulating Text}. The variable
2329 @code{write-region-inhibit-fsync} applies to these commands, as well
2330 as saving files; see @ref{Customize Save}.
2331
2332 @findex delete-file
2333 @cindex deletion (of files)
2334 @kbd{M-x delete-file} deletes the specified file, like the @code{rm}
2335 command in the shell. If you are deleting many files in one directory, it
2336 may be more convenient to use Dired (@pxref{Dired}).
2337
2338 @findex rename-file
2339 @kbd{M-x rename-file} reads two file names @var{old} and @var{new} using
2340 the minibuffer, then renames file @var{old} as @var{new}. If the file name
2341 @var{new} already exists, you must confirm with @kbd{yes} or renaming is not
2342 done; this is because renaming causes the old meaning of the name @var{new}
2343 to be lost. If @var{old} and @var{new} are on different file systems, the
2344 file @var{old} is copied and deleted.
2345
2346 If the argument @var{new} is just a directory name, the real new
2347 name is in that directory, with the same non-directory component as
2348 @var{old}. For example, @kbd{M-x rename-file RET ~/foo RET /tmp RET}
2349 renames @file{~/foo} to @file{/tmp/foo}. The same rule applies to all
2350 the remaining commands in this section. All of them ask for
2351 confirmation when the new file name already exists, too.
2352
2353 @findex add-name-to-file
2354 @cindex hard links (creation)
2355 The similar command @kbd{M-x add-name-to-file} is used to add an
2356 additional name to an existing file without removing its old name.
2357 The new name is created as a ``hard link'' to the existing file.
2358 The new name must belong on the same file system that the file is on.
2359 On MS-Windows, this command works only if the file resides in an NTFS
2360 file system. On MS-DOS, it works by copying the file.
2361
2362 @findex copy-file
2363 @cindex copying files
2364 @kbd{M-x copy-file} reads the file @var{old} and writes a new file
2365 named @var{new} with the same contents.
2366
2367 @findex make-symbolic-link
2368 @cindex symbolic links (creation)
2369 @kbd{M-x make-symbolic-link} reads two file names @var{target} and
2370 @var{linkname}, then creates a symbolic link named @var{linkname},
2371 which points at @var{target}. The effect is that future attempts to
2372 open file @var{linkname} will refer to whatever file is named
2373 @var{target} at the time the opening is done, or will get an error if
2374 the name @var{target} is nonexistent at that time. This command does
2375 not expand the argument @var{target}, so that it allows you to specify
2376 a relative name as the target of the link.
2377
2378 Not all systems support symbolic links; on systems that don't
2379 support them, this command is not defined.
2380
2381 @node Compressed Files
2382 @section Accessing Compressed Files
2383 @cindex compression
2384 @cindex uncompression
2385 @cindex Auto Compression mode
2386 @cindex mode, Auto Compression
2387 @pindex gzip
2388
2389 Emacs automatically uncompresses compressed files when you visit
2390 them, and automatically recompresses them if you alter them and save
2391 them. Emacs recognizes compressed files by their file names. File
2392 names ending in @samp{.gz} indicate a file compressed with
2393 @code{gzip}. Other endings indicate other compression programs.
2394
2395 Automatic uncompression and compression apply to all the operations in
2396 which Emacs uses the contents of a file. This includes visiting it,
2397 saving it, inserting its contents into a buffer, loading it, and byte
2398 compiling it.
2399
2400 @findex auto-compression-mode
2401 @vindex auto-compression-mode
2402 To disable this feature, type the command @kbd{M-x
2403 auto-compression-mode}. You can disenable it permanently by
2404 customizing the variable @code{auto-compression-mode}.
2405
2406 @node File Archives
2407 @section File Archives
2408 @cindex mode, tar
2409 @cindex Tar mode
2410 @cindex file archives
2411
2412 A file whose name ends in @samp{.tar} is normally an @dfn{archive}
2413 made by the @code{tar} program. Emacs views these files in a special
2414 mode called Tar mode which provides a Dired-like list of the contents
2415 (@pxref{Dired}). You can move around through the list just as you
2416 would in Dired, and visit the subfiles contained in the archive.
2417 However, not all Dired commands are available in Tar mode.
2418
2419 If Auto Compression mode is enabled (@pxref{Compressed Files}), then
2420 Tar mode is used also for compressed archives---files with extensions
2421 @samp{.tgz}, @code{.tar.Z} and @code{.tar.gz}.
2422
2423 The keys @kbd{e}, @kbd{f} and @key{RET} all extract a component file
2424 into its own buffer. You can edit it there, and if you save the
2425 buffer, the edited version will replace the version in the Tar buffer.
2426 @kbd{v} extracts a file into a buffer in View mode. @kbd{o} extracts
2427 the file and displays it in another window, so you could edit the file
2428 and operate on the archive simultaneously. @kbd{d} marks a file for
2429 deletion when you later use @kbd{x}, and @kbd{u} unmarks a file, as in
2430 Dired. @kbd{C} copies a file from the archive to disk and @kbd{R}
2431 renames a file within the archive. @kbd{g} reverts the buffer from
2432 the archive on disk.
2433
2434 The keys @kbd{M}, @kbd{G}, and @kbd{O} change the file's permission
2435 bits, group, and owner, respectively.
2436
2437 If your display supports colors and the mouse, moving the mouse
2438 pointer across a file name highlights that file name, indicating that
2439 you can click on it. Clicking @kbd{Mouse-2} on the highlighted file
2440 name extracts the file into a buffer and displays that buffer.
2441
2442 Saving the Tar buffer writes a new version of the archive to disk with
2443 the changes you made to the components.
2444
2445 You don't need the @code{tar} program to use Tar mode---Emacs reads
2446 the archives directly. However, accessing compressed archives
2447 requires the appropriate uncompression program.
2448
2449 @cindex Archive mode
2450 @cindex mode, archive
2451 @cindex @code{arc}
2452 @cindex @code{jar}
2453 @cindex @code{zip}
2454 @cindex @code{lzh}
2455 @cindex @code{zoo}
2456 @pindex arc
2457 @pindex jar
2458 @pindex zip
2459 @pindex lzh
2460 @pindex zoo
2461 @cindex Java class archives
2462 @cindex unzip archives
2463 A separate but similar Archive mode is used for archives produced by
2464 the programs @code{arc}, @code{jar}, @code{lzh}, @code{zip}, and
2465 @code{zoo}, which have extensions corresponding to the program names.
2466
2467 The key bindings of Archive mode are similar to those in Tar mode,
2468 with the addition of the @kbd{m} key which marks a file for subsequent
2469 operations, and @kbd{M-@key{DEL}} which unmarks all the marked files.
2470 Also, the @kbd{a} key toggles the display of detailed file
2471 information, for those archive types where it won't fit in a single
2472 line. Operations such as renaming a subfile, or changing its mode or
2473 owner, are supported only for some of the archive formats.
2474
2475 Unlike Tar mode, Archive mode runs the archiving program to unpack
2476 and repack archives. Details of the program names and their options
2477 can be set in the @samp{Archive} Customize group. However, you don't
2478 need these programs to look at the archive table of contents, only to
2479 extract or manipulate the subfiles in the archive.
2480
2481 @node Remote Files
2482 @section Remote Files
2483
2484 @cindex Tramp
2485 @cindex FTP
2486 @cindex remote file access
2487 You can refer to files on other machines using a special file name
2488 syntax:
2489
2490 @example
2491 @group
2492 /@var{host}:@var{filename}
2493 /@var{user}@@@var{host}:@var{filename}
2494 /@var{user}@@@var{host}#@var{port}:@var{filename}
2495 /@var{method}:@var{user}@@@var{host}:@var{filename}
2496 /@var{method}:@var{user}@@@var{host}#@var{port}:@var{filename}
2497 @end group
2498 @end example
2499
2500 @noindent
2501 To carry out this request, Emacs uses either the FTP program or a
2502 remote-login program such as @command{ssh}, @command{rlogin}, or
2503 @command{telnet}. You can always specify in the file name which
2504 method to use---for example,
2505 @file{/ftp:@var{user}@@@var{host}:@var{filename}} uses FTP, whereas
2506 @file{/ssh:@var{user}@@@var{host}:@var{filename}} uses @command{ssh}.
2507 When you don't specify a method in the file name, Emacs chooses
2508 the method as follows:
2509
2510 @enumerate
2511 @item
2512 If the host name starts with @samp{ftp.} (with dot), then Emacs uses
2513 FTP.
2514 @item
2515 If the user name is @samp{ftp} or @samp{anonymous}, then Emacs uses
2516 FTP.
2517 @item
2518 Otherwise, Emacs uses @command{ssh}.
2519 @end enumerate
2520
2521 @noindent
2522 Remote file access through FTP is handled by the Ange-FTP package, which
2523 is documented in the following. Remote file access through the other
2524 methods is handled by the Tramp package, which has its own manual.
2525 @xref{Top, The Tramp Manual,, tramp, The Tramp Manual}.
2526
2527 When the Ange-FTP package is used, Emacs logs in through FTP using your
2528 user name or the name @var{user}. It may ask you for a password from
2529 time to time; this is used for logging in on @var{host}. The form using
2530 @var{port} allows you to access servers running on a non-default TCP
2531 port.
2532
2533 @cindex backups for remote files
2534 @vindex ange-ftp-make-backup-files
2535 If you want to disable backups for remote files, set the variable
2536 @code{ange-ftp-make-backup-files} to @code{nil}.
2537
2538 By default, the auto-save files (@pxref{Auto Save Files}) for remote
2539 files are made in the temporary file directory on the local machine.
2540 This is achieved using the variable @code{auto-save-file-name-transforms}.
2541
2542 @cindex ange-ftp
2543 @vindex ange-ftp-default-user
2544 @cindex user name for remote file access
2545 Normally, if you do not specify a user name in a remote file name,
2546 that means to use your own user name. But if you set the variable
2547 @code{ange-ftp-default-user} to a string, that string is used instead.
2548
2549 @cindex anonymous FTP
2550 @vindex ange-ftp-generate-anonymous-password
2551 To visit files accessible by anonymous FTP, you use special user
2552 names @samp{anonymous} or @samp{ftp}. Passwords for these user names
2553 are handled specially. The variable
2554 @code{ange-ftp-generate-anonymous-password} controls what happens: if
2555 the value of this variable is a string, then that string is used as
2556 the password; if non-@code{nil} (the default), then the value of
2557 @code{user-mail-address} is used; if @code{nil}, then Emacs prompts
2558 you for a password as usual.
2559
2560 @cindex firewall, and accessing remote files
2561 @cindex gateway, and remote file access with @code{ange-ftp}
2562 @vindex ange-ftp-smart-gateway
2563 @vindex ange-ftp-gateway-host
2564 Sometimes you may be unable to access files on a remote machine
2565 because a @dfn{firewall} in between blocks the connection for security
2566 reasons. If you can log in on a @dfn{gateway} machine from which the
2567 target files @emph{are} accessible, and whose FTP server supports
2568 gatewaying features, you can still use remote file names; all you have
2569 to do is specify the name of the gateway machine by setting the
2570 variable @code{ange-ftp-gateway-host}, and set
2571 @code{ange-ftp-smart-gateway} to @code{t}. Otherwise you may be able
2572 to make remote file names work, but the procedure is complex. You can
2573 read the instructions by typing @kbd{M-x finder-commentary @key{RET}
2574 ange-ftp @key{RET}}.
2575
2576 @vindex file-name-handler-alist
2577 @cindex disabling remote files
2578 You can entirely turn off the FTP file name feature by removing the
2579 entries @code{ange-ftp-completion-hook-function} and
2580 @code{ange-ftp-hook-function} from the variable
2581 @code{file-name-handler-alist}. You can turn off the feature in
2582 individual cases by quoting the file name with @samp{/:} (@pxref{Quoted
2583 File Names}).
2584
2585 @node Quoted File Names
2586 @section Quoted File Names
2587
2588 @cindex quoting file names
2589 @cindex file names, quote special characters
2590 You can @dfn{quote} an absolute file name to prevent special
2591 characters and syntax in it from having their special effects.
2592 The way to do this is to add @samp{/:} at the beginning.
2593
2594 For example, you can quote a local file name which appears remote, to
2595 prevent it from being treated as a remote file name. Thus, if you have
2596 a directory named @file{/foo:} and a file named @file{bar} in it, you
2597 can refer to that file in Emacs as @samp{/:/foo:/bar}.
2598
2599 @samp{/:} can also prevent @samp{~} from being treated as a special
2600 character for a user's home directory. For example, @file{/:/tmp/~hack}
2601 refers to a file whose name is @file{~hack} in directory @file{/tmp}.
2602
2603 Quoting with @samp{/:} is also a way to enter in the minibuffer a
2604 file name that contains @samp{$}. In order for this to work, the
2605 @samp{/:} must be at the beginning of the minibuffer contents. (You
2606 can also double each @samp{$}; see @ref{File Names with $}.)
2607
2608 You can also quote wildcard characters with @samp{/:}, for visiting.
2609 For example, @file{/:/tmp/foo*bar} visits the file
2610 @file{/tmp/foo*bar}.
2611
2612 Another method of getting the same result is to enter
2613 @file{/tmp/foo[*]bar}, which is a wildcard specification that matches
2614 only @file{/tmp/foo*bar}. However, in many cases there is no need to
2615 quote the wildcard characters because even unquoted they give the
2616 right result. For example, if the only file name in @file{/tmp} that
2617 starts with @samp{foo} and ends with @samp{bar} is @file{foo*bar},
2618 then specifying @file{/tmp/foo*bar} will visit only
2619 @file{/tmp/foo*bar}.
2620
2621 @node File Name Cache
2622 @section File Name Cache
2623
2624 @cindex file name caching
2625 @cindex cache of file names
2626 @pindex find
2627 @kindex C-@key{TAB}
2628 @findex file-cache-minibuffer-complete
2629 You can use the @dfn{file name cache} to make it easy to locate a
2630 file by name, without having to remember exactly where it is located.
2631 When typing a file name in the minibuffer, @kbd{C-@key{tab}}
2632 (@code{file-cache-minibuffer-complete}) completes it using the file
2633 name cache. If you repeat @kbd{C-@key{tab}}, that cycles through the
2634 possible completions of what you had originally typed. (However, note
2635 that the @kbd{C-@key{tab}} character cannot be typed on most text-only
2636 terminals.)
2637
2638 The file name cache does not fill up automatically. Instead, you
2639 load file names into the cache using these commands:
2640
2641 @findex file-cache-add-directory
2642 @table @kbd
2643 @item M-x file-cache-add-directory @key{RET} @var{directory} @key{RET}
2644 Add each file name in @var{directory} to the file name cache.
2645 @item M-x file-cache-add-directory-using-find @key{RET} @var{directory} @key{RET}
2646 Add each file name in @var{directory} and all of its nested
2647 subdirectories to the file name cache.
2648 @item M-x file-cache-add-directory-using-locate @key{RET} @var{directory} @key{RET}
2649 Add each file name in @var{directory} and all of its nested
2650 subdirectories to the file name cache, using @command{locate} to find
2651 them all.
2652 @item M-x file-cache-add-directory-list @key{RET} @var{variable} @key{RET}
2653 Add each file name in each directory listed in @var{variable}
2654 to the file name cache. @var{variable} should be a Lisp variable
2655 such as @code{load-path} or @code{exec-path}, whose value is a list
2656 of directory names.
2657 @item M-x file-cache-clear-cache @key{RET}
2658 Clear the cache; that is, remove all file names from it.
2659 @end table
2660
2661 The file name cache is not persistent: it is kept and maintained
2662 only for the duration of the Emacs session. You can view the contents
2663 of the cache with the @code{file-cache-display} command.
2664
2665 @node File Conveniences
2666 @section Convenience Features for Finding Files
2667
2668 In this section, we introduce some convenient facilities for finding
2669 recently-opened files, reading file names from a buffer, and viewing
2670 image files.
2671
2672 @findex recentf-mode
2673 @vindex recentf-mode
2674 @findex recentf-save-list
2675 @findex recentf-edit-list
2676 If you enable Recentf mode, with @kbd{M-x recentf-mode}, the
2677 @samp{File} menu includes a submenu containing a list of recently
2678 opened files. @kbd{M-x recentf-save-list} saves the current
2679 @code{recent-file-list} to a file, and @kbd{M-x recentf-edit-list}
2680 edits it.
2681
2682 The @kbd{M-x ffap} command generalizes @code{find-file} with more
2683 powerful heuristic defaults (@pxref{FFAP}), often based on the text at
2684 point. Partial Completion mode offers other features extending
2685 @code{find-file}, which can be used with @code{ffap}.
2686 @xref{Completion Options}.
2687
2688 @findex image-mode
2689 @findex image-toggle-display
2690 @cindex images, viewing
2691 Visiting image files automatically selects Image mode. This major
2692 mode allows you to toggle between displaying the file as an image in
2693 the Emacs buffer, and displaying its underlying text representation,
2694 using the command @kbd{C-c C-c} (@code{image-toggle-display}). This
2695 works only when Emacs can display the specific image type.
2696
2697 @findex thumbs-mode
2698 @findex mode, thumbs
2699 Thumbs mode is a major mode for viewing directories containing many
2700 image files. To use it, type @kbd{M-x thumbs} and specify the
2701 directory to view. The images in that directory will be displayed in
2702 a @samp{Thumbs} buffer as @dfn{thumbnails}; type @kbd{RET} on a
2703 thumbnail to view the full-size image. Thumbs mode requires the
2704 @file{convert} program, which is part of the ImageMagick software
2705 package.
2706
2707 @node Filesets
2708 @section Filesets
2709 @cindex filesets
2710
2711 @findex filesets-init
2712 If you regularly edit a certain group of files, you can define them
2713 as a @dfn{fileset}. This lets you perform certain operations, such as
2714 visiting, @code{query-replace}, and shell commands on all the files
2715 at once. To make use of filesets, you must first add the expression
2716 @code{(filesets-init)} to your @file{.emacs} file (@pxref{Init File}).
2717 This adds a @samp{Filesets} menu to the menu bar.
2718
2719 @findex filesets-add-buffer
2720 @findex filesets-remove-buffer
2721 The simplest way to define a fileset is by adding files to it one
2722 at a time. To add a file to fileset @var{name}, visit the file and
2723 type @kbd{M-x filesets-add-buffer @kbd{RET} @var{name} @kbd{RET}}. If
2724 there is no fileset @var{name}, this creates a new one, which
2725 initially creates only the current file. The command @kbd{M-x
2726 filesets-remove-buffer} removes the current file from a fileset.
2727
2728 You can also edit the list of filesets directly, with @kbd{M-x
2729 filesets-edit} (or by choosing @samp{Edit Filesets} from the
2730 @samp{Filesets} menu). The editing is performed in a Customize buffer
2731 (@pxref{Easy Customization}). Filesets need not be a simple list of
2732 files---you can also define filesets using regular expression matching
2733 file names. Some examples of these more complicated filesets are
2734 shown in the Customize buffer. Remember to select @samp{Save for
2735 future sessions} if you want to use the same filesets in future Emacs
2736 sessions.
2737
2738 You can use the command @kbd{M-x filesets-open} to visit all the
2739 files in a fileset, and @kbd{M-x filesets-close} to close them. Use
2740 @kbd{M-x filesets-run-cmd} to run a shell command on all the files in
2741 a fileset. These commands are also available from the @samp{Filesets}
2742 menu, where each existing fileset is represented by a submenu.
2743
2744 @ignore
2745 arch-tag: 768d32cb-e15a-4cc1-b7bf-62c00ee12250
2746 @end ignore