]> code.delx.au - gnu-emacs/blob - man/files.texi
Typo, copyright fix.
[gnu-emacs] / man / files.texi
1 @c This is part of the Emacs manual.
2 @c Copyright (C) 1985,86,87,93,94,95,97,99, 2000 Free Software Foundation, Inc.
3 @c See file emacs.texi for copying conditions.
4 @node Files, Buffers, Fixit, Top
5 @chapter File Handling
6 @cindex files
7
8 The operating system stores data permanently in named @dfn{files}. So
9 most of the text you edit with Emacs comes from a file and is ultimately
10 stored in a file.
11
12 To edit a file, you must tell Emacs to read the file and prepare a
13 buffer containing a copy of the file's text. This is called
14 @dfn{visiting} the file. Editing commands apply directly to text in the
15 buffer; that is, to the copy inside Emacs. Your changes appear in the
16 file itself only when you @dfn{save} the buffer back into the file.
17
18 In addition to visiting and saving files, Emacs can delete, copy,
19 rename, and append to files, keep multiple versions of them, and operate
20 on file directories.
21
22 @menu
23 * File Names:: How to type and edit file-name arguments.
24 * Visiting:: Visiting a file prepares Emacs to edit the file.
25 * Saving:: Saving makes your changes permanent.
26 * Reverting:: Reverting cancels all the changes not saved.
27 * Auto Save:: Auto Save periodically protects against loss of data.
28 * File Aliases:: Handling multiple names for one file.
29 * Version Control:: Version control systems (RCS, CVS and SCCS).
30 * Directories:: Creating, deleting, and listing file directories.
31 * Comparing Files:: Finding where two files differ.
32 * Misc File Ops:: Other things you can do on files.
33 * Compressed Files:: Accessing compressed files.
34 * File Archives:: Operating on tar, zip, jar etc. archive files.
35 * Remote Files:: Accessing files on other sites.
36 * Quoted File Names:: Quoting special characters in file names.
37 @end menu
38
39 @node File Names
40 @section File Names
41 @cindex file names
42
43 Most Emacs commands that operate on a file require you to specify the
44 file name. (Saving and reverting are exceptions; the buffer knows which
45 file name to use for them.) You enter the file name using the
46 minibuffer (@pxref{Minibuffer}). @dfn{Completion} is available, to make
47 it easier to specify long file names. @xref{Completion}.
48
49 For most operations, there is a @dfn{default file name} which is used
50 if you type just @key{RET} to enter an empty argument. Normally the
51 default file name is the name of the file visited in the current buffer;
52 this makes it easy to operate on that file with any of the Emacs file
53 commands.
54
55 @vindex default-directory
56 Each buffer has a default directory, normally the same as the
57 directory of the file visited in that buffer. When you enter a file
58 name without a directory, the default directory is used. If you specify
59 a directory in a relative fashion, with a name that does not start with
60 a slash, it is interpreted with respect to the default directory. The
61 default directory is kept in the variable @code{default-directory},
62 which has a separate value in every buffer.
63
64 For example, if the default file name is @file{/u/rms/gnu/gnu.tasks} then
65 the default directory is @file{/u/rms/gnu/}. If you type just @samp{foo},
66 which does not specify a directory, it is short for @file{/u/rms/gnu/foo}.
67 @samp{../.login} would stand for @file{/u/rms/.login}. @samp{new/foo}
68 would stand for the file name @file{/u/rms/gnu/new/foo}.
69
70 @findex cd
71 @findex pwd
72 The command @kbd{M-x pwd} prints the current buffer's default
73 directory, and the command @kbd{M-x cd} sets it (to a value read using
74 the minibuffer). A buffer's default directory changes only when the
75 @code{cd} command is used. A file-visiting buffer's default directory
76 is initialized to the directory of the file that is visited there. If
77 you create a buffer with @kbd{C-x b}, its default directory is copied
78 from that of the buffer that was current at the time.
79
80 @vindex insert-default-directory
81 The default directory actually appears in the minibuffer when the
82 minibuffer becomes active to read a file name. This serves two
83 purposes: it @emph{shows} you what the default is, so that you can type
84 a relative file name and know with certainty what it will mean, and it
85 allows you to @emph{edit} the default to specify a different directory.
86 This insertion of the default directory is inhibited if the variable
87 @code{insert-default-directory} is set to @code{nil}.
88
89 Note that it is legitimate to type an absolute file name after you
90 enter the minibuffer, ignoring the presence of the default directory
91 name as part of the text. The final minibuffer contents may look
92 invalid, but that is not so. For example, if the minibuffer starts out
93 with @samp{/usr/tmp/} and you add @samp{/x1/rms/foo}, you get
94 @samp{/usr/tmp//x1/rms/foo}; but Emacs ignores everything through the
95 first slash in the double slash; the result is @samp{/x1/rms/foo}.
96 @xref{Minibuffer File}.
97
98 @samp{$} in a file name is used to substitute environment variables.
99 For example, if you have used the shell command @samp{export
100 FOO=rms/hacks} to set up an environment variable named @code{FOO}, then
101 you can use @file{/u/$FOO/test.c} or @file{/u/$@{FOO@}/test.c} as an
102 abbreviation for @file{/u/rms/hacks/test.c}. The environment variable
103 name consists of all the alphanumeric characters after the @samp{$};
104 alternatively, it may be enclosed in braces after the @samp{$}. Note
105 that shell commands to set environment variables affect Emacs only if
106 done before Emacs is started.
107
108 To access a file with @samp{$} in its name, type @samp{$$}. This pair
109 is converted to a single @samp{$} at the same time as variable
110 substitution is performed for single @samp{$}. Alternatively, quote the
111 whole file name with @samp{/:} (@pxref{Quoted File Names}).
112
113 @findex substitute-in-file-name
114 The Lisp function that performs the substitution is called
115 @code{substitute-in-file-name}. The substitution is performed only on
116 file names read as such using the minibuffer.
117
118 You can include non-ASCII characters in file names if you set the
119 variable @code{file-name-coding-system} to a non-@code{nil} value.
120 @xref{Specify Coding}.
121
122 @node Visiting
123 @section Visiting Files
124 @cindex visiting files
125
126 @c WideCommands
127 @table @kbd
128 @item C-x C-f
129 Visit a file (@code{find-file}).
130 @item C-x C-r
131 Visit a file for viewing, without allowing changes to it
132 (@code{find-file-read-only}).
133 @item C-x C-v
134 Visit a different file instead of the one visited last
135 (@code{find-alternate-file}).
136 @item C-x 4 f
137 Visit a file, in another window (@code{find-file-other-window}). Don't
138 alter what is displayed in the selected window.
139 @item C-x 5 f
140 Visit a file, in a new frame (@code{find-file-other-frame}). Don't
141 alter what is displayed in the selected frame.
142 @item M-x find-file-literally
143 Visit a file with no conversion of the contents.
144 @end table
145
146 @cindex files, visiting and saving
147 @cindex visiting files
148 @cindex saving files
149 @dfn{Visiting} a file means copying its contents into an Emacs buffer
150 so you can edit them. Emacs makes a new buffer for each file that you
151 visit. We say that this buffer is visiting the file that it was created
152 to hold. Emacs constructs the buffer name from the file name by
153 throwing away the directory, keeping just the name proper. For example,
154 a file named @file{/usr/rms/emacs.tex} would get a buffer named
155 @samp{emacs.tex}. If there is already a buffer with that name, a unique
156 name is constructed by appending @samp{<2>}, @samp{<3>}, or so on, using
157 the lowest number that makes a name that is not already in use.
158
159 Each window's mode line shows the name of the buffer that is being displayed
160 in that window, so you can always tell what buffer you are editing.
161
162 The changes you make with editing commands are made in the Emacs
163 buffer. They do not take effect in the file that you visited, or any
164 place permanent, until you @dfn{save} the buffer. Saving the buffer
165 means that Emacs writes the current contents of the buffer into its
166 visited file. @xref{Saving}.
167
168 @cindex modified (buffer)
169 If a buffer contains changes that have not been saved, we say the
170 buffer is @dfn{modified}. This is important because it implies that
171 some changes will be lost if the buffer is not saved. The mode line
172 displays two stars near the left margin to indicate that the buffer is
173 modified.
174
175 @kindex C-x C-f
176 @findex find-file
177 To visit a file, use the command @kbd{C-x C-f} (@code{find-file}). Follow
178 the command with the name of the file you wish to visit, terminated by a
179 @key{RET}.
180
181 The file name is read using the minibuffer (@pxref{Minibuffer}), with
182 defaulting and completion in the standard manner (@pxref{File Names}).
183 While in the minibuffer, you can abort @kbd{C-x C-f} by typing @kbd{C-g}.
184
185 Your confirmation that @kbd{C-x C-f} has completed successfully is the
186 appearance of new text on the screen and a new buffer name in the mode
187 line. If the specified file does not exist and could not be created, or
188 cannot be read, then you get an error, with an error message displayed
189 in the echo area.
190
191 If you visit a file that is already in Emacs, @kbd{C-x C-f} does not make
192 another copy. It selects the existing buffer containing that file.
193 However, before doing so, it checks that the file itself has not changed
194 since you visited or saved it last. If the file has changed, a warning
195 message is printed. @xref{Interlocking,,Simultaneous Editing}.
196
197 @cindex creating files
198 What if you want to create a new file? Just visit it. Emacs prints
199 @samp{(New File)} in the echo area, but in other respects behaves as if
200 you had visited an existing empty file. If you make any changes and
201 save them, the file is created.
202
203 Emacs recognizes from the contents of a file which convention it uses
204 to separate lines---newline (used on GNU/Linux and on Unix),
205 carriage-return linefeed (used on Microsoft systems), or just
206 carriage-return (used on the Macintosh)---and automatically converts the
207 contents to the normal Emacs convention, which is that the newline
208 character separates lines. This is a part of the general feature of
209 coding system conversion (@pxref{Coding Systems}), and makes it possible
210 to edit files imported from various different operating systems with
211 equal convenience. If you change the text and save the file, Emacs
212 performs the inverse conversion, changing newlines back into
213 carriage-return linefeed or just carriage-return if appropriate.
214
215 @vindex find-file-run-dired
216 If the file you specify is actually a directory, @kbd{C-x C-f} invokes
217 Dired, the Emacs directory browser, so that you can ``edit'' the contents
218 of the directory (@pxref{Dired}). Dired is a convenient way to delete,
219 look at, or operate on the files in the directory. However, if the
220 variable @code{find-file-run-dired} is @code{nil}, then it is an error
221 to try to visit a directory.
222
223 If the file name you specify contains wildcard characters, Emacs
224 visits all the files that match it. @xref{Quoted File Names}, if you
225 want to visit a file whose name actually contains wildcard characters.
226
227 If you visit a file that the operating system won't let you modify,
228 Emacs makes the buffer read-only, so that you won't go ahead and make
229 changes that you'll have trouble saving afterward. You can make the
230 buffer writable with @kbd{C-x C-q} (@code{vc-toggle-read-only}).
231 @xref{Misc Buffer}.
232
233 @kindex C-x C-r
234 @findex find-file-read-only
235 Occasionally you might want to visit a file as read-only in order to
236 protect yourself from entering changes accidentally; do so by visiting
237 the file with the command @kbd{C-x C-r} (@code{find-file-read-only}).
238
239 @kindex C-x C-v
240 @findex find-alternate-file
241 If you visit a nonexistent file unintentionally (because you typed the
242 wrong file name), use the @kbd{C-x C-v} command
243 (@code{find-alternate-file}) to visit the file you really wanted.
244 @kbd{C-x C-v} is similar to @kbd{C-x C-f}, but it kills the current
245 buffer (after first offering to save it if it is modified). When it
246 reads the file name to visit, it inserts the entire default file name in
247 the buffer, with point just after the directory part; this is convenient
248 if you made a slight error in typing the name.
249
250 If you find a file which exists but cannot be read, @kbd{C-x C-f}
251 signals an error.
252
253 @kindex C-x 4 f
254 @findex find-file-other-window
255 @kbd{C-x 4 f} (@code{find-file-other-window}) is like @kbd{C-x C-f}
256 except that the buffer containing the specified file is selected in another
257 window. The window that was selected before @kbd{C-x 4 f} continues to
258 show the same buffer it was already showing. If this command is used when
259 only one window is being displayed, that window is split in two, with one
260 window showing the same buffer as before, and the other one showing the
261 newly requested file. @xref{Windows}.
262
263 @kindex C-x 5 f
264 @findex find-file-other-frame
265 @kbd{C-x 5 f} (@code{find-file-other-frame}) is similar, but opens a
266 new frame, or makes visible any existing frame showing the file you
267 seek. This feature is available only when you are using a window
268 system. @xref{Frames}.
269
270 @findex find-file-literally
271 If you wish to edit a file as a sequence of characters with no special
272 encoding or conversion, use the @kbd{M-x find-file-literally} command.
273 It visits a file, like @kbd{C-x C-f}, but does not do format conversion
274 (@pxref{Formatted Text}), character code conversion (@pxref{Coding
275 Systems}), or automatic uncompression (@pxref{Compressed Files}).
276 If you already have visited the same file in the usual (non-literal)
277 manner, this command asks you whether to visit it literally instead.
278
279 @vindex find-file-hooks
280 @vindex find-file-not-found-hooks
281 Two special hook variables allow extensions to modify the operation of
282 visiting files. Visiting a file that does not exist runs the functions
283 in the list @code{find-file-not-found-hooks}; this variable holds a list
284 of functions, and the functions are called one by one (with no
285 arguments) until one of them returns non-@code{nil}. This is not a
286 normal hook, and the name ends in @samp{-hooks} rather than @samp{-hook}
287 to indicate that fact.
288
289 Any visiting of a file, whether extant or not, expects
290 @code{find-file-hooks} to contain a list of functions, and calls them
291 all, one by one, with no arguments. This variable is really a normal
292 hook, but it has an abnormal name for historical compatibility. In the
293 case of a nonexistent file, the @code{find-file-not-found-hooks} are run
294 first. @xref{Hooks}.
295
296 There are several ways to specify automatically the major mode for
297 editing the file (@pxref{Choosing Modes}), and to specify local
298 variables defined for that file (@pxref{File Variables}).
299
300 @node Saving
301 @section Saving Files
302
303 @dfn{Saving} a buffer in Emacs means writing its contents back into the file
304 that was visited in the buffer.
305
306 @table @kbd
307 @item C-x C-s
308 Save the current buffer in its visited file (@code{save-buffer}).
309 @item C-x s
310 Save any or all buffers in their visited files (@code{save-some-buffers}).
311 @item M-~
312 Forget that the current buffer has been changed (@code{not-modified}).
313 @item C-x C-w
314 Save the current buffer in a specified file (@code{write-file}).
315 @item M-x set-visited-file-name
316 Change file the name under which the current buffer will be saved.
317 @end table
318
319 @kindex C-x C-s
320 @findex save-buffer
321 When you wish to save the file and make your changes permanent, type
322 @kbd{C-x C-s} (@code{save-buffer}). After saving is finished, @kbd{C-x C-s}
323 displays a message like this:
324
325 @example
326 Wrote /u/rms/gnu/gnu.tasks
327 @end example
328
329 @noindent
330 If the selected buffer is not modified (no changes have been made in it
331 since the buffer was created or last saved), saving is not really done,
332 because it would have no effect. Instead, @kbd{C-x C-s} displays a message
333 like this in the echo area:
334
335 @example
336 (No changes need to be saved)
337 @end example
338
339 @kindex C-x s
340 @findex save-some-buffers
341 The command @kbd{C-x s} (@code{save-some-buffers}) offers to save any
342 or all modified buffers. It asks you what to do with each buffer. The
343 possible responses are analogous to those of @code{query-replace}:
344
345 @table @kbd
346 @item y
347 Save this buffer and ask about the rest of the buffers.
348 @item n
349 Don't save this buffer, but ask about the rest of the buffers.
350 @item !
351 Save this buffer and all the rest with no more questions.
352 @c following generates acceptable underfull hbox
353 @item @key{RET}
354 Terminate @code{save-some-buffers} without any more saving.
355 @item .
356 Save this buffer, then exit @code{save-some-buffers} without even asking
357 about other buffers.
358 @item C-r
359 View the buffer that you are currently being asked about. When you exit
360 View mode, you get back to @code{save-some-buffers}, which asks the
361 question again.
362 @item C-h
363 Display a help message about these options.
364 @end table
365
366 @kbd{C-x C-c}, the key sequence to exit Emacs, invokes
367 @code{save-some-buffers} and therefore asks the same questions.
368
369 @kindex M-~
370 @findex not-modified
371 If you have changed a buffer but you do not want to save the changes,
372 you should take some action to prevent it. Otherwise, each time you use
373 @kbd{C-x s} or @kbd{C-x C-c}, you are liable to save this buffer by
374 mistake. One thing you can do is type @kbd{M-~} (@code{not-modified}),
375 which clears out the indication that the buffer is modified. If you do
376 this, none of the save commands will believe that the buffer needs to be
377 saved. (@samp{~} is often used as a mathematical symbol for `not'; thus
378 @kbd{M-~} is `not', metafied.) You could also use
379 @code{set-visited-file-name} (see below) to mark the buffer as visiting
380 a different file name, one which is not in use for anything important.
381 Alternatively, you can cancel all the changes made since the file was
382 visited or saved, by reading the text from the file again. This is
383 called @dfn{reverting}. @xref{Reverting}. You could also undo all the
384 changes by repeating the undo command @kbd{C-x u} until you have undone
385 all the changes; but reverting is easier.
386
387 @findex set-visited-file-name
388 @kbd{M-x set-visited-file-name} alters the name of the file that the
389 current buffer is visiting. It reads the new file name using the
390 minibuffer. Then it specifies the visited file name and changes the
391 buffer name correspondingly (as long as the new name is not in use).
392 @code{set-visited-file-name} does not save the buffer in the newly
393 visited file; it just alters the records inside Emacs in case you do
394 save later. It also marks the buffer as ``modified'' so that @kbd{C-x
395 C-s} in that buffer @emph{will} save.
396
397 @kindex C-x C-w
398 @findex write-file
399 If you wish to mark the buffer as visiting a different file and save it
400 right away, use @kbd{C-x C-w} (@code{write-file}). It is precisely
401 equivalent to @code{set-visited-file-name} followed by @kbd{C-x C-s}.
402 @kbd{C-x C-s} used on a buffer that is not visiting a file has the
403 same effect as @kbd{C-x C-w}; that is, it reads a file name, marks the
404 buffer as visiting that file, and saves it there. The default file name in
405 a buffer that is not visiting a file is made by combining the buffer name
406 with the buffer's default directory.
407
408 If the new file name implies a major mode, then @kbd{C-x C-w} switches
409 to that major mode, in most cases. The command
410 @code{set-visited-file-name} also does this. @xref{Choosing Modes}.
411
412 If Emacs is about to save a file and sees that the date of the latest
413 version on disk does not match what Emacs last read or wrote, Emacs
414 notifies you of this fact, because it probably indicates a problem caused
415 by simultaneous editing and requires your immediate attention.
416 @xref{Interlocking,, Simultaneous Editing}.
417
418 @vindex require-final-newline
419 If the variable @code{require-final-newline} is non-@code{nil}, Emacs
420 puts a newline at the end of any file that doesn't already end in one,
421 every time a file is saved or written. The default is @code{nil}.
422
423 @menu
424 * Backup:: How Emacs saves the old version of your file.
425 * Interlocking:: How Emacs protects against simultaneous editing
426 of one file by two users.
427 @end menu
428
429 @node Backup
430 @subsection Backup Files
431 @cindex backup file
432 @vindex make-backup-files
433 @vindex vc-make-backup-files
434 @vindex backup-enable-predicate
435
436 On most operating systems, rewriting a file automatically destroys all
437 record of what the file used to contain. Thus, saving a file from Emacs
438 throws away the old contents of the file---or it would, except that
439 Emacs carefully copies the old contents to another file, called the
440 @dfn{backup} file, before actually saving.
441
442 For most files, the variable @code{make-backup-files} determines
443 whether to make backup files. On most operating systems, its default
444 value is @code{t}, so that Emacs does write backup files.
445
446 For files managed by a version control system (@pxref{Version
447 Control}), the variable @code{vc-make-backup-files} determines whether
448 to make backup files. By default, it is @code{nil}, since backup files
449 are redundant when you store all the previous versions in a version
450 control system. @xref{VC Workfile Handling}.
451
452 The default value of the @code{backup-enable-predicate} variable
453 prevents backup files being written for files in @file{/tmp}.
454
455 At your option, Emacs can keep either a single backup file or a series of
456 numbered backup files for each file that you edit.
457
458 Emacs makes a backup for a file only the first time the file is saved
459 from one buffer. No matter how many times you save a file, its backup file
460 continues to contain the contents from before the file was visited.
461 Normally this means that the backup file contains the contents from before
462 the current editing session; however, if you kill the buffer and then visit
463 the file again, a new backup file will be made by the next save.
464
465 You can also explicitly request making another backup file from a
466 buffer even though it has already been saved at least once. If you save
467 the buffer with @kbd{C-u C-x C-s}, the version thus saved will be made
468 into a backup file if you save the buffer again. @kbd{C-u C-u C-x C-s}
469 saves the buffer, but first makes the previous file contents into a new
470 backup file. @kbd{C-u C-u C-u C-x C-s} does both things: it makes a
471 backup from the previous contents, and arranges to make another from the
472 newly saved contents, if you save again.
473
474 @menu
475 * Names: Backup Names. How backup files are named;
476 choosing single or numbered backup files.
477 * Deletion: Backup Deletion. Emacs deletes excess numbered backups.
478 * Copying: Backup Copying. Backups can be made by copying or renaming.
479 @end menu
480
481 @node Backup Names
482 @subsubsection Single or Numbered Backups
483
484 If you choose to have a single backup file (this is the default),
485 the backup file's name is constructed by appending @samp{~} to the
486 file name being edited; thus, the backup file for @file{eval.c} would
487 be @file{eval.c~}.
488
489 If you choose to have a series of numbered backup files, backup file
490 names are made by appending @samp{.~}, the number, and another @samp{~} to
491 the original file name. Thus, the backup files of @file{eval.c} would be
492 called @file{eval.c.~1~}, @file{eval.c.~2~}, and so on, through names
493 like @file{eval.c.~259~} and beyond.
494
495 If protection stops you from writing backup files under the usual names,
496 the backup file is written as @file{%backup%~} in your home directory.
497 Only one such file can exist, so only the most recently made such backup is
498 available.
499
500 @vindex version-control
501 The choice of single backup or numbered backups is controlled by the
502 variable @code{version-control}. Its possible values are
503
504 @table @code
505 @item t
506 Make numbered backups.
507 @item nil
508 Make numbered backups for files that have numbered backups already.
509 Otherwise, make single backups.
510 @item never
511 Do not in any case make numbered backups; always make single backups.
512 @end table
513
514 @noindent
515 You can set @code{version-control} locally in an individual buffer to
516 control the making of backups for that buffer's file. For example,
517 Rmail mode locally sets @code{version-control} to @code{never} to make sure
518 that there is only one backup for an Rmail file. @xref{Locals}.
519
520 @cindex @code{VERSION_CONTROL} environment variable
521 If you set the environment variable @code{VERSION_CONTROL}, to tell
522 various GNU utilities what to do with backup files, Emacs also obeys the
523 environment variable by setting the Lisp variable @code{version-control}
524 accordingly at startup. If the environment variable's value is @samp{t}
525 or @samp{numbered}, then @code{version-control} becomes @code{t}; if the
526 value is @samp{nil} or @samp{existing}, then @code{version-control}
527 becomes @code{nil}; if it is @samp{never} or @samp{simple}, then
528 @code{version-control} becomes @code{never}.
529
530 @node Backup Deletion
531 @subsubsection Automatic Deletion of Backups
532
533 To prevent unlimited consumption of disk space, Emacs can delete numbered
534 backup versions automatically. Generally Emacs keeps the first few backups
535 and the latest few backups, deleting any in between. This happens every
536 time a new backup is made.
537
538 @vindex kept-old-versions
539 @vindex kept-new-versions
540 The two variables @code{kept-old-versions} and
541 @code{kept-new-versions} control this deletion. Their values are,
542 respectively the number of oldest (lowest-numbered) backups to keep and
543 the number of newest (highest-numbered) ones to keep, each time a new
544 backup is made. Recall that these values are used just after a new
545 backup version is made; that newly made backup is included in the count
546 in @code{kept-new-versions}. By default, both variables are 2.
547
548 @vindex delete-old-versions
549 If @code{delete-old-versions} is non-@code{nil}, the excess
550 middle versions are deleted without a murmur. If it is @code{nil}, the
551 default, then you are asked whether the excess middle versions should
552 really be deleted.
553
554 Dired's @kbd{.} (Period) command can also be used to delete old versions.
555 @xref{Dired Deletion}.
556
557 @node Backup Copying
558 @subsubsection Copying vs.@: Renaming
559
560 Backup files can be made by copying the old file or by renaming it. This
561 makes a difference when the old file has multiple names. If the old file
562 is renamed into the backup file, then the alternate names become names for
563 the backup file. If the old file is copied instead, then the alternate
564 names remain names for the file that you are editing, and the contents
565 accessed by those names will be the new contents.
566
567 The method of making a backup file may also affect the file's owner
568 and group. If copying is used, these do not change. If renaming is used,
569 you become the file's owner, and the file's group becomes the default
570 (different operating systems have different defaults for the group).
571
572 Having the owner change is usually a good idea, because then the owner
573 always shows who last edited the file. Also, the owners of the backups
574 show who produced those versions. Occasionally there is a file whose
575 owner should not change; it is a good idea for such files to contain
576 local variable lists to set @code{backup-by-copying-when-mismatch}
577 locally (@pxref{File Variables}).
578
579 @vindex backup-by-copying
580 @vindex backup-by-copying-when-linked
581 @vindex backup-by-copying-when-mismatch
582 The choice of renaming or copying is controlled by three variables.
583 Renaming is the default choice. If the variable
584 @code{backup-by-copying} is non-@code{nil}, copying is used. Otherwise,
585 if the variable @code{backup-by-copying-when-linked} is non-@code{nil},
586 then copying is used for files that have multiple names, but renaming
587 may still be used when the file being edited has only one name. If the
588 variable @code{backup-by-copying-when-mismatch} is non-@code{nil}, then
589 copying is used if renaming would cause the file's owner or group to
590 change. @code{backup-by-copying-when-mismatch} is @code{t} by default
591 if you start Emacs as the superuser.
592
593 When a file is managed with a version control system (@pxref{Version
594 Control}), Emacs does not normally make backups in the usual way for
595 that file. But check-in and check-out are similar in some ways to
596 making backups. One unfortunate similarity is that these operations
597 typically break hard links, disconnecting the file name you visited from
598 any alternate names for the same file. This has nothing to do with
599 Emacs---the version control system does it.
600
601 @node Interlocking
602 @subsection Protection against Simultaneous Editing
603
604 @cindex file dates
605 @cindex simultaneous editing
606 Simultaneous editing occurs when two users visit the same file, both
607 make changes, and then both save them. If nobody were informed that
608 this was happening, whichever user saved first would later find that his
609 changes were lost.
610
611 On some systems, Emacs notices immediately when the second user starts
612 to change the file, and issues an immediate warning. On all systems,
613 Emacs checks when you save the file, and warns if you are about to
614 overwrite another user's changes. You can prevent loss of the other
615 user's work by taking the proper corrective action instead of saving the
616 file.
617
618 @findex ask-user-about-lock
619 @cindex locking files
620 When you make the first modification in an Emacs buffer that is
621 visiting a file, Emacs records that the file is @dfn{locked} by you.
622 (It does this by creating a symbolic link in the same directory with a
623 different name.) Emacs removes the lock when you save the changes. The
624 idea is that the file is locked whenever an Emacs buffer visiting it has
625 unsaved changes.
626
627 @cindex collision
628 If you begin to modify the buffer while the visited file is locked by
629 someone else, this constitutes a @dfn{collision}. When Emacs detects a
630 collision, it asks you what to do, by calling the Lisp function
631 @code{ask-user-about-lock}. You can redefine this function for the sake
632 of customization. The standard definition of this function asks you a
633 question and accepts three possible answers:
634
635 @table @kbd
636 @item s
637 Steal the lock. Whoever was already changing the file loses the lock,
638 and you gain the lock.
639 @item p
640 Proceed. Go ahead and edit the file despite its being locked by someone else.
641 @item q
642 Quit. This causes an error (@code{file-locked}) and the modification you
643 were trying to make in the buffer does not actually take place.
644 @end table
645
646 Note that locking works on the basis of a file name; if a file has
647 multiple names, Emacs does not realize that the two names are the same file
648 and cannot prevent two users from editing it simultaneously under different
649 names. However, basing locking on names means that Emacs can interlock the
650 editing of new files that will not really exist until they are saved.
651
652 Some systems are not configured to allow Emacs to make locks, and
653 there are cases where lock files cannot be written. In these cases,
654 Emacs cannot detect trouble in advance, but it still can detect the
655 collision when you try to save a file and overwrite someone else's
656 changes.
657
658 If Emacs or the operating system crashes, this may leave behind lock
659 files which are stale. So you may occasionally get warnings about
660 spurious collisions. When you determine that the collision is spurious,
661 just use @kbd{p} to tell Emacs to go ahead anyway.
662
663 Every time Emacs saves a buffer, it first checks the last-modification
664 date of the existing file on disk to verify that it has not changed since the
665 file was last visited or saved. If the date does not match, it implies
666 that changes were made in the file in some other way, and these changes are
667 about to be lost if Emacs actually does save. To prevent this, Emacs
668 prints a warning message and asks for confirmation before saving.
669 Occasionally you will know why the file was changed and know that it does
670 not matter; then you can answer @kbd{yes} and proceed. Otherwise, you should
671 cancel the save with @kbd{C-g} and investigate the situation.
672
673 The first thing you should do when notified that simultaneous editing
674 has already taken place is to list the directory with @kbd{C-u C-x C-d}
675 (@pxref{Directories}). This shows the file's current author. You
676 should attempt to contact him to warn him not to continue editing.
677 Often the next step is to save the contents of your Emacs buffer under a
678 different name, and use @code{diff} to compare the two files.@refill
679
680 @node Reverting
681 @section Reverting a Buffer
682 @findex revert-buffer
683 @cindex drastic changes
684
685 If you have made extensive changes to a file and then change your mind
686 about them, you can get rid of them by reading in the previous version
687 of the file. To do this, use @kbd{M-x revert-buffer}, which operates on
688 the current buffer. Since reverting a buffer unintentionally could lose
689 a lot of work, you must confirm this command with @kbd{yes}.
690
691 @code{revert-buffer} keeps point at the same distance (measured in
692 characters) from the beginning of the file. If the file was edited only
693 slightly, you will be at approximately the same piece of text after
694 reverting as before. If you have made drastic changes, the same value of
695 point in the old file may address a totally different piece of text.
696
697 Reverting marks the buffer as ``not modified'' until another change is
698 made.
699
700 Some kinds of buffers whose contents reflect data bases other than files,
701 such as Dired buffers, can also be reverted. For them, reverting means
702 recalculating their contents from the appropriate data base. Buffers
703 created explicitly with @kbd{C-x b} cannot be reverted; @code{revert-buffer}
704 reports an error when asked to do so.
705
706 @vindex revert-without-query
707 When you edit a file that changes automatically and frequently---for
708 example, a log of output from a process that continues to run---it may be
709 useful for Emacs to revert the file without querying you, whenever you
710 visit the file again with @kbd{C-x C-f}.
711
712 To request this behavior, set the variable @code{revert-without-query}
713 to a list of regular expressions. When a file name matches one of these
714 regular expressions, @code{find-file} and @code{revert-buffer} will
715 revert it automatically if it has changed---provided the buffer itself
716 is not modified. (If you have edited the text, it would be wrong to
717 discard your changes.)
718
719 @node Auto Save
720 @section Auto-Saving: Protection Against Disasters
721 @cindex Auto Save mode
722 @cindex mode, Auto Save
723 @cindex crashes
724
725 Emacs saves all the visited files from time to time (based on counting
726 your keystrokes) without being asked. This is called @dfn{auto-saving}.
727 It prevents you from losing more than a limited amount of work if the
728 system crashes.
729
730 When Emacs determines that it is time for auto-saving, each buffer is
731 considered, and is auto-saved if auto-saving is turned on for it and it
732 has been changed since the last time it was auto-saved. The message
733 @samp{Auto-saving...} is displayed in the echo area during auto-saving,
734 if any files are actually auto-saved. Errors occurring during
735 auto-saving are caught so that they do not interfere with the execution
736 of commands you have been typing.
737
738 @menu
739 * Files: Auto Save Files. The file where auto-saved changes are
740 actually made until you save the file.
741 * Control: Auto Save Control. Controlling when and how often to auto-save.
742 * Recover:: Recovering text from auto-save files.
743 @end menu
744
745 @node Auto Save Files
746 @subsection Auto-Save Files
747
748 Auto-saving does not normally save in the files that you visited, because
749 it can be very undesirable to save a program that is in an inconsistent
750 state when you have made half of a planned change. Instead, auto-saving
751 is done in a different file called the @dfn{auto-save file}, and the
752 visited file is changed only when you request saving explicitly (such as
753 with @kbd{C-x C-s}).
754
755 Normally, the auto-save file name is made by appending @samp{#} to the
756 front and rear of the visited file name. Thus, a buffer visiting file
757 @file{foo.c} is auto-saved in a file @file{#foo.c#}. Most buffers that
758 are not visiting files are auto-saved only if you request it explicitly;
759 when they are auto-saved, the auto-save file name is made by appending
760 @samp{#%} to the front and @samp{#} to the rear of buffer name. For
761 example, the @samp{*mail*} buffer in which you compose messages to be
762 sent is auto-saved in a file named @file{#%*mail*#}. Auto-save file
763 names are made this way unless you reprogram parts of Emacs to do
764 something different (the functions @code{make-auto-save-file-name} and
765 @code{auto-save-file-name-p}). The file name to be used for auto-saving
766 in a buffer is calculated when auto-saving is turned on in that buffer.
767
768 When you delete a substantial part of the text in a large buffer, auto
769 save turns off temporarily in that buffer. This is because if you
770 deleted the text unintentionally, you might find the auto-save file more
771 useful if it contains the deleted text. To reenable auto-saving after
772 this happens, save the buffer with @kbd{C-x C-s}, or use @kbd{C-u 1 M-x
773 auto-save}.
774
775 @vindex auto-save-visited-file-name
776 If you want auto-saving to be done in the visited file, set the variable
777 @code{auto-save-visited-file-name} to be non-@code{nil}. In this mode,
778 there is really no difference between auto-saving and explicit saving.
779
780 @vindex delete-auto-save-files
781 A buffer's auto-save file is deleted when you save the buffer in its
782 visited file. To inhibit this, set the variable @code{delete-auto-save-files}
783 to @code{nil}. Changing the visited file name with @kbd{C-x C-w} or
784 @code{set-visited-file-name} renames any auto-save file to go with
785 the new visited name.
786
787 @node Auto Save Control
788 @subsection Controlling Auto-Saving
789
790 @vindex auto-save-default
791 @findex auto-save-mode
792 Each time you visit a file, auto-saving is turned on for that file's
793 buffer if the variable @code{auto-save-default} is non-@code{nil} (but not
794 in batch mode; @pxref{Entering Emacs}). The default for this variable is
795 @code{t}, so auto-saving is the usual practice for file-visiting buffers.
796 Auto-saving can be turned on or off for any existing buffer with the
797 command @kbd{M-x auto-save-mode}. Like other minor mode commands, @kbd{M-x
798 auto-save-mode} turns auto-saving on with a positive argument, off with a
799 zero or negative argument; with no argument, it toggles.
800
801 @vindex auto-save-interval
802 Emacs does auto-saving periodically based on counting how many characters
803 you have typed since the last time auto-saving was done. The variable
804 @code{auto-save-interval} specifies how many characters there are between
805 auto-saves. By default, it is 300.
806
807 @vindex auto-save-timeout
808 Auto-saving also takes place when you stop typing for a while. The
809 variable @code{auto-save-timeout} says how many seconds Emacs should
810 wait before it does an auto save (and perhaps also a garbage
811 collection). (The actual time period is longer if the current buffer is
812 long; this is a heuristic which aims to keep out of your way when you
813 are editing long buffers, in which auto-save takes an appreciable amount
814 of time.) Auto-saving during idle periods accomplishes two things:
815 first, it makes sure all your work is saved if you go away from the
816 terminal for a while; second, it may avoid some auto-saving while you
817 are actually typing.
818
819 Emacs also does auto-saving whenever it gets a fatal error. This
820 includes killing the Emacs job with a shell command such as @samp{kill
821 %emacs}, or disconnecting a phone line or network connection.
822
823 @findex do-auto-save
824 You can request an auto-save explicitly with the command @kbd{M-x
825 do-auto-save}.
826
827 @node Recover
828 @subsection Recovering Data from Auto-Saves
829
830 @findex recover-file
831 You can use the contents of an auto-save file to recover from a loss
832 of data with the command @kbd{M-x recover-file @key{RET} @var{file}
833 @key{RET}}. This visits @var{file} and then (after your confirmation)
834 restores the contents from its auto-save file @file{#@var{file}#}.
835 You can then save with @kbd{C-x C-s} to put the recovered text into
836 @var{file} itself. For example, to recover file @file{foo.c} from its
837 auto-save file @file{#foo.c#}, do:@refill
838
839 @example
840 M-x recover-file @key{RET} foo.c @key{RET}
841 yes @key{RET}
842 C-x C-s
843 @end example
844
845 Before asking for confirmation, @kbd{M-x recover-file} displays a
846 directory listing describing the specified file and the auto-save file,
847 so you can compare their sizes and dates. If the auto-save file
848 is older, @kbd{M-x recover-file} does not offer to read it.
849
850 @findex recover-session
851 If Emacs or the computer crashes, you can recover all the files you
852 were editing from their auto save files with the command @kbd{M-x
853 recover-session}. This first shows you a list of recorded interrupted
854 sessions. Move point to the one you choose, and type @kbd{C-c C-c}.
855
856 Then @code{recover-session} asks about each of the files that were
857 being edited during that session, asking whether to recover that file.
858 If you answer @kbd{y}, it calls @code{recover-file}, which works in its
859 normal fashion. It shows the dates of the original file and its
860 auto-save file, and asks once again whether to recover that file.
861
862 When @code{recover-session} is done, the files you've chosen to
863 recover are present in Emacs buffers. You should then save them. Only
864 this---saving them---updates the files themselves.
865
866 @vindex auto-save-list-file-prefix
867 Interrupted sessions are recorded for later recovery in files named
868 @file{~/.saves-@var{pid}-@var{hostname}}. The @samp{~/.saves} portion of
869 these names comes from the value of @code{auto-save-list-file-prefix}.
870 You can arrange to record sessions in a different place by setting that
871 variable in your @file{.emacs} file, but you'll have to redefine
872 @code{recover-session} as well to make it look in the new place. If you
873 set @code{auto-save-list-file-prefix} to @code{nil} in your
874 @file{.emacs} file, sessions are not recorded for recovery.
875
876 @node File Aliases
877 @section File Name Aliases
878
879 Symbolic links and hard links both make it possible for several file
880 names to refer to the same file. Hard links are alternate names that
881 refer directly to the file; all the names are equally valid, and no one
882 of them is preferred. By contrast, a symbolic link is a kind of defined
883 alias: when @file{foo} is a symbolic link to @file{bar}, you can use
884 either name to refer to the file, but @file{bar} is the real name, while
885 @file{foo} is just an alias. More complex cases occur when symbolic
886 links point to directories.
887
888 If you visit two names for the same file, normally Emacs makes
889 two different buffers, but it warns you about the situation.
890
891 @vindex find-file-existing-other-name
892 If you wish to avoid visiting the same file in two buffers under
893 different names, set the variable @code{find-file-existing-other-name}
894 to a non-@code{nil} value. Then @code{find-file} uses the existing
895 buffer visiting the file, no matter which of the file's names you
896 specify.
897
898 @vindex find-file-visit-truename
899 @cindex truenames of files
900 @cindex file truenames
901 If the variable @code{find-file-visit-truename} is non-@code{nil},
902 then the file name recorded for a buffer is the file's @dfn{truename}
903 (made by replacing all symbolic links with their target names), rather
904 than the name you specify. Setting @code{find-file-visit-truename} also
905 implies the effect of @code{find-file-existing-other-name}.
906
907 @node Version Control
908 @section Version Control
909 @cindex version control
910
911 @dfn{Version control systems} are packages that can record multiple
912 versions of a source file, usually storing the unchanged parts of the
913 file just once. Version control systems also record history information
914 such as the creation time of each version, who created it, and a
915 description of what was changed in that version.
916
917 The Emacs version control interface is called VC. Its commands work
918 with three version control systems---RCS, CVS and SCCS. The GNU project
919 recommends RCS and CVS, which are free software and available from the
920 Free Software Foundation.
921
922 @menu
923 * Introduction to VC:: How version control works in general.
924 * VC Mode Line:: How the mode line shows version control status.
925 * Basic VC Editing:: How to edit a file under version control.
926 * Old Versions:: Examining and comparing old versions.
927 * Secondary VC Commands:: The commands used a little less frequently.
928 * Branches:: Multiple lines of development.
929 * Snapshots:: Sets of file versions treated as a unit.
930 * Miscellaneous VC:: Various other commands and features of VC.
931 * Customizing VC:: Variables that change VC's behavior.
932 @end menu
933
934 @node Introduction to VC
935 @subsection Introduction to Version Control
936
937 VC allows you to use a version control system from within Emacs,
938 integrating the version control operations smoothly with editing. VC
939 provides a uniform interface to version control, so that regardless of
940 which version control system is in use, you can use it the same way.
941
942 This section provides a general overview of version control, and
943 describes the version control systems that VC supports. You can skip
944 this section if you are already familiar with the version control system
945 you want to use.
946
947 @menu
948 * Version Systems:: Supported version control back-end systems.
949 * VC Concepts:: Words and concepts related to version control.
950 @end menu
951
952 @node Version Systems
953 @subsubsection Supported Version Control Systems
954
955 @cindex RCS
956 @cindex back end (version control)
957 VC currently works with three different version control systems or
958 ``back ends'': RCS, CVS, and SCCS.
959
960 RCS is a free version control system that is available from the Free
961 Software Foundation. It is perhaps the most mature of the supported
962 back ends, and the VC commands are conceptually closest to RCS. Almost
963 everything you can do with RCS can be done through VC.
964
965 @cindex CVS
966 CVS is built on top of RCS, and extends the features of RCS, allowing
967 for more sophisticated release management, and concurrent multi-user
968 development. VC supports basic editing operations under CVS, but for
969 some less common tasks you still need to call CVS from the command line.
970 Note also that before using CVS you must set up a repository, which is a
971 subject too complex to treat here.
972
973 @cindex SCCS
974 SCCS is a proprietary but widely used version control system. In
975 terms of capabilities, it is the weakest of the three that VC
976 supports. VC compensates for certain features missing in SCCS
977 (snapshots, for example) by implementing them itself, but some other VC
978 features, such as multiple branches, are not available with SCCS. You
979 should use SCCS only if for some reason you cannot use RCS.
980
981 @node VC Concepts
982 @subsubsection Concepts of Version Control
983
984 @cindex master file
985 @cindex registered file
986 When a file is under version control, we also say that it is
987 @dfn{registered} in the version control system. Each registered file
988 has a corresponding @dfn{master file} which represents the file's
989 present state plus its change history---enough to reconstruct the
990 current version or any earlier version. Usually the master file also
991 records a @dfn{log entry} for each version, describing in words what was
992 changed in that version.
993
994 @cindex work file
995 @cindex checking out files
996 The file that is maintained under version control is sometimes called
997 the @dfn{work file} corresponding to its master file. You edit the work
998 file and make changes in it, as you would with an ordinary file. (With
999 SCCS and RCS, you must @dfn{lock} the file before you start to edit it.)
1000 After you are done with a set of changes, you @dfn{check the file in},
1001 which records the changes in the master file, along with a log entry for
1002 them.
1003
1004 With CVS, there are usually multiple work files corresponding to a
1005 single master file---often each user has his own copy. It is also
1006 possible to use RCS in this way, but this is not the usual way to use
1007 RCS.
1008
1009 @cindex locking and version control
1010 A version control system typically has some mechanism to coordinate
1011 between users who want to change the same file. One method is
1012 @dfn{locking} (analogous to the locking that Emacs uses to detect
1013 simultaneous editing of a file, but distinct from it). The other method
1014 is to merge your changes with other people's changes when you check them
1015 in.
1016
1017 With version control locking, work files are normally read-only so
1018 that you cannot change them. You ask the version control system to make
1019 a work file writable for you by locking it; only one user can do
1020 this at any given time. When you check in your changes, that unlocks
1021 the file, making the work file read-only again. This allows other users
1022 to lock the file to make further changes. SCCS always uses locking, and
1023 RCS normally does.
1024
1025 The other alternative for RCS is to let each user modify the work file
1026 at any time. In this mode, locking is not required, but it is
1027 permitted; check-in is still the way to record a new version.
1028
1029 CVS normally allows each user to modify his own copy of the work file
1030 at any time, but requires merging with changes from other users at
1031 check-in time. However, CVS can also be set up to require locking.
1032 (@pxref{Backend Options}).
1033
1034 @node VC Mode Line
1035 @subsection Version Control and the Mode Line
1036
1037 When you visit a file that is under version control, Emacs indicates
1038 this on the mode line. For example, @samp{RCS-1.3} says that RCS is
1039 used for that file, and the current version is 1.3.
1040
1041 The character between the back-end name and the version number
1042 indicates the version control status of the file. @samp{-} means that
1043 the work file is not locked (if locking is in use), or not modified (if
1044 locking is not in use). @samp{:} indicates that the file is locked, or
1045 that it is modified. If the file is locked by some other user (for
1046 instance, @samp{jim}), that is displayed as @samp{RCS:jim:1.3}.
1047
1048 @node Basic VC Editing
1049 @subsection Basic Editing under Version Control
1050
1051 The principal VC command is an all-purpose command that performs
1052 either locking or check-in, depending on the situation.
1053
1054 @table @kbd
1055 @item C-x C-q
1056 @itemx C-x v v
1057 Perform the next logical version control operation on this file.
1058 @end table
1059
1060 @findex vc-next-action
1061 @findex vc-toggle-read-only
1062 @kindex C-x v v
1063 @kindex C-x C-q @r{(Version Control)}
1064 Strictly speaking, the command for this job is @code{vc-next-action},
1065 bound to @kbd{C-x v v}. However, the normal meaning of @kbd{C-x C-q} is
1066 to make a read-only buffer writable, or vice versa; we have extended it
1067 to do the same job properly for files managed by version control, by
1068 performing the appropriate version control operations. When you type
1069 @kbd{C-x C-q} on a registered file, it acts like @kbd{C-x v v}.
1070
1071 The precise action of this command depends on the state of the file,
1072 and whether the version control system uses locking or not. SCCS and
1073 RCS normally use locking; CVS normally does not use locking.
1074
1075 @menu
1076 * VC with Locking:: RCS in its default mode, SCCS, and optionally CVS.
1077 * Without Locking:: Without locking: default mode for CVS.
1078 * Log Buffer:: Features available in log entry buffers.
1079 @end menu
1080
1081 @node VC with Locking
1082 @subsubsection Basic Version Control with Locking
1083
1084 If locking is used for the file (as with SCCS, and RCS in its default
1085 mode), @kbd{C-x C-q} can either lock a file or check it in:
1086
1087 @itemize @bullet
1088 @item
1089 If the file is not locked, @kbd{C-x C-q} locks it, and
1090 makes it writable so that you can change it.
1091
1092 @item
1093 If the file is locked by you, and contains changes, @kbd{C-x C-q} checks
1094 in the changes. In order to do this, it first reads the log entry
1095 for the new version. @xref{Log Buffer}.
1096
1097 @item
1098 If the file is locked by you, but you have not changed it since you
1099 locked it, @kbd{C-x C-q} releases the lock and makes the file read-only
1100 again.
1101
1102 @item
1103 If the file is locked by some other user, @kbd{C-x C-q} asks you whether
1104 you want to ``steal the lock'' from that user. If you say yes, the file
1105 becomes locked by you, but a message is sent to the person who had
1106 formerly locked the file, to inform him of what has happened.
1107 @end itemize
1108
1109 These rules also apply when you use CVS in locking mode, except
1110 that there is no such thing as stealing a lock.
1111
1112 @node Without Locking
1113 @subsubsection Basic Version Control without Locking
1114
1115 When there is no locking---the default for CVS---work files are always
1116 writable; you do not need to do anything before you begin to edit a
1117 file. The status indicator on the mode line is @samp{-} if the file is
1118 unmodified; it flips to @samp{:} as soon as you save any changes in the
1119 work file.
1120
1121 Here is what @kbd{C-x C-q} does when using CVS:
1122
1123 @itemize @bullet
1124 @item
1125 If some other user has checked in changes into the master file,
1126 Emacs asks you whether you want to merge those changes into your own
1127 work file (@pxref{Merging}). You must do this before you can check in
1128 your own changes.
1129
1130 @item
1131 If there are no new changes in the master file, but you have made
1132 modifications in your work file, @kbd{C-x C-q} checks in your changes.
1133 In order to do this, it first reads the log entry for the new version.
1134 @xref{Log Buffer}.
1135
1136 @item
1137 If the file is not modified, the @kbd{C-x C-q} does nothing.
1138 @end itemize
1139
1140 These rules also apply when you use RCS in the mode that does not
1141 require locking, except that automatic merging of changes from the
1142 master file is not implemented. Unfortunately, this means that nothing
1143 informs you if another user has checked in changes in the same file
1144 since you began editing it, and when this happens, his changes will be
1145 effectively removed when you check in your version (though they will
1146 remain in the master file, so they will not be entirely lost). You must
1147 therefore verify the current version is unchanged, before you check in your
1148 changes. We hope to eliminate this risk and provide automatic merging
1149 with RCS in a future Emacs version.
1150
1151 In addition, locking is possible with RCS even in this mode, although
1152 it is not required; @kbd{C-x C-q} with an unmodified file locks the
1153 file, just as it does with RCS in its normal (locking) mode.
1154
1155 @node Log Buffer
1156 @subsubsection Features of the Log Entry Buffer
1157
1158 When you check in changes, @kbd{C-x C-q} first reads a log entry. It
1159 pops up a buffer called @samp{*VC-Log*} for you to enter the log entry.
1160 When you are finished, type @kbd{C-c C-c} in the @samp{*VC-Log*} buffer.
1161 That is when check-in really happens.
1162
1163 To abort check-in, just @strong{don't} type @kbd{C-c C-c} in that
1164 buffer. You can switch buffers and do other editing. As long as you
1165 don't try to check in another file, the entry you were editing remains
1166 in the @samp{*VC-Log*} buffer, and you can go back to that buffer at any
1167 time to complete the check-in.
1168
1169 If you change several source files for the same reason, it is often
1170 convenient to specify the same log entry for many of the files. To do
1171 this, use the history of previous log entries. The commands @kbd{M-n},
1172 @kbd{M-p}, @kbd{M-s} and @kbd{M-r} for doing this work just like the
1173 minibuffer history commands (except that these versions are used outside
1174 the minibuffer).
1175
1176 @vindex vc-log-mode-hook
1177 Each time you check in a file, the log entry buffer is put into VC Log
1178 mode, which involves running two hooks: @code{text-mode-hook} and
1179 @code{vc-log-mode-hook}. @xref{Hooks}.
1180
1181 @node Old Versions
1182 @subsection Examining And Comparing Old Versions
1183
1184 One of the convenient features of version control is the ability
1185 to examine any version of a file, or compare two versions.
1186
1187 @table @kbd
1188 @item C-x v ~ @var{version} @key{RET}
1189 Examine version @var{version} of the visited file, in a buffer of its
1190 own.
1191
1192 @item C-x v =
1193 Compare the current buffer contents with the latest checked-in version
1194 of the file.
1195
1196 @item C-u C-x v = @var{file} @key{RET} @var{oldvers} @key{RET} @var{newvers} @key{RET}
1197 Compare the specified two versions of @var{file}.
1198
1199 @item C-x v g
1200 Display the result of the CVS annotate command using colors.
1201 @end table
1202
1203 @findex vc-version-other-window
1204 @kindex C-x v ~
1205 To examine an old version in toto, visit the file and then type
1206 @kbd{C-x v ~ @var{version} @key{RET}} (@code{vc-version-other-window}).
1207 This puts the text of version @var{version} in a file named
1208 @file{@var{filename}.~@var{version}~}, and visits it in its own buffer
1209 in a separate window. (In RCS, you can also select an old version
1210 and create a branch from it. @xref{Branches}.)
1211
1212 @findex vc-diff
1213 @kindex C-x v =
1214 But usually it is more convenient to compare two versions of the file,
1215 with the command @kbd{C-x v =} (@code{vc-diff}). Plain @kbd{C-x v =}
1216 compares the current buffer contents (saving them in the file if
1217 necessary) with the last checked-in version of the file. @kbd{C-u C-x v
1218 =}, with a numeric argument, reads a file name and two version numbers,
1219 then compares those versions of the specified file.
1220
1221 If you supply a directory name instead of the name of a registered
1222 file, this command compares the two specified versions of all registered
1223 files in that directory and its subdirectories.
1224
1225 You can specify a checked-in version by its number; an empty input
1226 specifies the current contents of the work file (which may be different
1227 from all the checked-in versions). You can also specify a snapshot name
1228 (@pxref{Snapshots}) instead of one or both version numbers.
1229
1230 This command works by running the @code{diff} utility, getting the
1231 options from the variable @code{diff-switches}. It displays the output
1232 in a special buffer in another window. Unlike the @kbd{M-x diff}
1233 command, @kbd{C-x v =} does not try to locate the changes in the old and
1234 new versions. This is because normally one or both versions do not
1235 exist as files when you compare them; they exist only in the records of
1236 the master file. @xref{Comparing Files}, for more information about
1237 @kbd{M-x diff}.
1238
1239 @findex vc-annotate
1240 @kindex C-x v g
1241 For CVS-controlled files, you can display the result of the CVS
1242 annotate command, using colors to enhance the visual appearance. Use
1243 the command @kbd{M-x vc-annotate} to do this. Red means new, blue means
1244 old, and intermediate colors indicate intermediate ages. A prefix
1245 argument @var{n} specifies a stretch factor for the time scale; it makes
1246 each color cover a period @var{n} times as long.
1247
1248 @node Secondary VC Commands
1249 @subsection The Secondary Commands of VC
1250
1251 This section explains the secondary commands of VC; those that you might
1252 use once a day.
1253
1254 @menu
1255 * Registering:: Putting a file under version control.
1256 * VC Status:: Viewing the VC status of files.
1257 * VC Undo:: Cancelling changes before or after check-in.
1258 * VC Dired Mode:: Listing files managed by version control.
1259 * VC Dired Commands:: Commands to use in a VC Dired buffer.
1260 @end menu
1261
1262 @node Registering
1263 @subsubsection Registering a File for Version Control
1264
1265 @kindex C-x v i
1266 @findex vc-register
1267 You can put any file under version control by simply visiting it, and
1268 then typing @w{@kbd{C-x v i}} (@code{vc-register}).
1269
1270 @table @kbd
1271 @item C-x v i
1272 Register the visited file for version control.
1273 @end table
1274
1275 @vindex vc-default-back-end
1276 To register the file, Emacs must choose which version control system
1277 to use for it. You can specify your choice explicitly by setting
1278 @code{vc-default-back-end} to @code{RCS}, @code{CVS} or @code{SCCS}.
1279 Otherwise, if there is a subdirectory named @file{RCS}, @file{SCCS}, or
1280 @file{CVS}, Emacs uses the corresponding version control system. In the
1281 absence of any specification, the default choice is RCS if RCS is
1282 installed, otherwise SCCS.
1283
1284 If locking is in use, @kbd{C-x v i} leaves the file unlocked and
1285 read-only. Type @kbd{C-x C-q} if you wish to start editing it. After
1286 registering a file with CVS, you must subsequently commit the initial
1287 version by typing @kbd{C-x C-q}.
1288
1289 @vindex vc-default-init-version
1290 The initial version number for a newly registered file is 1.1, by
1291 default. You can specify a different default by setting the variable
1292 @code{vc-default-init-version}, or you can give @kbd{C-x v i} a numeric
1293 argument; then it reads the initial version number for this particular
1294 file using the minibuffer.
1295
1296 @vindex vc-initial-comment
1297 If @code{vc-initial-comment} is non-@code{nil}, @kbd{C-x v i} reads an
1298 initial comment to describe the purpose of this source file. Reading
1299 the initial comment works like reading a log entry (@pxref{Log Buffer}).
1300
1301 @node VC Status
1302 @subsubsection VC Status Commands
1303
1304 @table @kbd
1305 @item C-x v l
1306 Display version control state and change history.
1307 @end table
1308
1309 @kindex C-x v l
1310 @findex vc-print-log
1311 To view the detailed version control status and history of a file,
1312 type @kbd{C-x v l} (@code{vc-print-log}). It displays the history of
1313 changes to the current file, including the text of the log entries. The
1314 output appears in a separate window.
1315
1316 @node VC Undo
1317 @subsubsection Undoing Version Control Actions
1318
1319 @table @kbd
1320 @item C-x v u
1321 Revert the buffer and the file to the last checked-in version.
1322
1323 @item C-x v c
1324 Remove the last-entered change from the master for the visited file.
1325 This undoes your last check-in.
1326 @end table
1327
1328 @kindex C-x v u
1329 @findex vc-revert-buffer
1330 If you want to discard your current set of changes and revert to the
1331 last version checked in, use @kbd{C-x v u} (@code{vc-revert-buffer}).
1332 This leaves the file unlocked; if locking is in use, you must first lock
1333 the file again before you change it again. @kbd{C-x v u} requires
1334 confirmation, unless it sees that you haven't made any changes since the
1335 last checked-in version.
1336
1337 @kbd{C-x v u} is also the command to unlock a file if you lock it and
1338 then decide not to change it.
1339
1340 @kindex C-x v c
1341 @findex vc-cancel-version
1342 To cancel a change that you already checked in, use @kbd{C-x v c}
1343 (@code{vc-cancel-version}). This command discards all record of the
1344 most recent checked-in version. @kbd{C-x v c} also offers to revert
1345 your work file and buffer to the previous version (the one that precedes
1346 the version that is deleted).
1347
1348 If you answer @kbd{no}, VC keeps your changes in the buffer, and locks
1349 the file. The no-revert option is useful when you have checked in a
1350 change and then discover a trivial error in it; you can cancel the
1351 erroneous check-in, fix the error, and check the file in again.
1352
1353 When @kbd{C-x v c} does not revert the buffer, it unexpands all
1354 version control headers in the buffer instead (@pxref{Version Headers}).
1355 This is because the buffer no longer corresponds to any existing
1356 version. If you check it in again, the check-in process will expand the
1357 headers properly for the new version number.
1358
1359 However, it is impossible to unexpand the RCS @samp{@w{$}Log$} header
1360 automatically. If you use that header feature, you have to unexpand it
1361 by hand---by deleting the entry for the version that you just canceled.
1362
1363 Be careful when invoking @kbd{C-x v c}, as it is easy to lose a lot of
1364 work with it. To help you be careful, this command always requires
1365 confirmation with @kbd{yes}. Note also that this command is disabled
1366 under CVS, because canceling versions is very dangerous and discouraged
1367 with CVS.
1368
1369 @node VC Dired Mode
1370 @subsubsection Dired under VC
1371
1372 @kindex C-x v d
1373 @findex vc-directory
1374 When you are working on a large program, it is often useful to find
1375 out which files have changed within an entire directory tree, or to view
1376 the status of all files under version control at once, and to perform
1377 version control operations on collections of files. You can use the
1378 command @kbd{C-x v d} (@code{vc-directory}) to make a directory listing
1379 that includes only files relevant for version control.
1380
1381 @vindex vc-dired-terse-display
1382 @kbd{C-x v d} creates a buffer which uses VC Dired Mode. This looks
1383 much like an ordinary Dired buffer (@pxref{Dired}); however, normally it
1384 shows only the noteworthy files (those locked or not up-to-date). This
1385 is called @dfn{terse display}. If you set the variable
1386 @code{vc-dired-terse-display} to @code{nil}, then VC Dired shows all
1387 relevant files---those managed under version control, plus all
1388 subdirectories (@dfn{full display}). The command @kbd{v t} in a VC
1389 Dired buffer toggles between terse display and full display (@pxref{VC
1390 Dired Commands}).
1391
1392 @vindex vc-dired-recurse
1393 By default, VC Dired produces a recursive listing of noteworthy or
1394 relevant files at or below the given directory. You can change this by
1395 setting the variable @code{vc-dired-recurse} to @code{nil}; then VC
1396 Dired shows only the files in the given directory.
1397
1398 The line for an individual file shows the version control state in the
1399 place of the hard link count, owner, group, and size of the file. If
1400 the file is unmodified, in sync with the master file, the version
1401 control state shown is blank. Otherwise it consists of text in
1402 parentheses. Under RCS and SCCS, the name of the user locking the file
1403 is shown; under CVS, an abbreviated version of the @samp{cvs status}
1404 output is used. Here is an example using RCS:
1405
1406 @smallexample
1407 @group
1408 /home/jim/project:
1409
1410 -rw-r--r-- (jim) Apr 2 23:39 file1
1411 -r--r--r-- Apr 5 20:21 file2
1412 @end group
1413 @end smallexample
1414
1415 @noindent
1416 The files @samp{file1} and @samp{file2} are under version control,
1417 @samp{file1} is locked by user jim, and @samp{file2} is unlocked.
1418
1419 Here is an example using CVS:
1420
1421 @smallexample
1422 @group
1423 /home/joe/develop:
1424
1425 -rw-r--r-- (modified) Aug 2 1997 file1.c
1426 -rw-r--r-- Apr 4 20:09 file2.c
1427 -rw-r--r-- (merge) Sep 13 1996 file3.c
1428 @end group
1429 @end smallexample
1430
1431 Here @samp{file1.c} is modified with respect to the repository, and
1432 @samp{file2.c} is not. @samp{file3.c} is modified, but other changes
1433 have also been checked in to the repository---you need to merge them
1434 with the work file before you can check it in.
1435
1436 @vindex vc-directory-exclusion-list
1437 When VC Dired displays subdirectories (in the ``full'' display mode),
1438 it omits some that should never contain any files under version control.
1439 By default, this includes Version Control subdirectories such as
1440 @samp{RCS} and @samp{CVS}; you can customize this by setting the
1441 variable @code{vc-directory-exclusion-list}.
1442
1443 You can fine-tune VC Dired's format by typing @kbd{C-u C-x v d}---as in
1444 ordinary Dired, that allows you to specify additional switches for the
1445 @samp{ls} command.
1446
1447 @node VC Dired Commands
1448 @subsubsection VC Dired Commands
1449
1450 All the usual Dired commands work normally in VC Dired mode, except
1451 for @kbd{v}, which is redefined as the version control prefix. You can
1452 invoke VC commands such as @code{vc-diff} and @code{vc-print-log} by
1453 typing @kbd{v =}, or @kbd{v l}, and so on. Most of these commands apply
1454 to the file name on the current line.
1455
1456 The command @kbd{v v} (@code{vc-next-action}) operates on all the
1457 marked files, so that you can lock or check in several files at once.
1458 If it operates on more than one file, it handles each file according to
1459 its current state; thus, it might lock one file, but check in another
1460 file. This could be confusing; it is up to you to avoid confusing
1461 behavior by marking a set of files that are in a similar state.
1462
1463 If any files call for check-in, @kbd{v v} reads a single log entry,
1464 then uses it for all the files being checked in. This is convenient for
1465 registering or checking in several files at once, as part of the same
1466 change.
1467
1468 @findex vc-dired-toggle-terse-mode
1469 @findex vc-dired-mark-locked
1470 You can toggle between terse display (only locked files, or files not
1471 up-to-date) and full display at any time by typing @kbd{v t}
1472 @code{vc-dired-toggle-terse-mode}. There is also a special command
1473 @kbd{* l} (@code{vc-dired-mark-locked}), which marks all files currently
1474 locked (or, with CVS, all files not up-to-date). Thus, typing @kbd{* l
1475 t k} is another way to delete from the buffer all files except those
1476 currently locked.
1477
1478 @node Branches
1479 @subsection Multiple Branches of a File
1480 @cindex branch (version control)
1481 @cindex trunk (version control)
1482
1483 One use of version control is to maintain multiple ``current''
1484 versions of a file. For example, you might have different versions of a
1485 program in which you are gradually adding various unfinished new
1486 features. Each such independent line of development is called a
1487 @dfn{branch}. VC allows you to create branches, switch between
1488 different branches, and merge changes from one branch to another.
1489 Please note, however, that branches are only supported for RCS at the
1490 moment.
1491
1492 A file's main line of development is usually called the @dfn{trunk}.
1493 The versions on the trunk are normally numbered 1.1, 1.2, 1.3, etc. At
1494 any such version, you can start an independent branch. A branch
1495 starting at version 1.2 would have version number 1.2.1.1, and consecutive
1496 versions on this branch would have numbers 1.2.1.2, 1.2.1.3, 1.2.1.4,
1497 and so on. If there is a second branch also starting at version 1.2, it
1498 would consist of versions 1.2.2.1, 1.2.2.2, 1.2.2.3, etc.
1499
1500 @cindex head version
1501 If you omit the final component of a version number, that is called a
1502 @dfn{branch number}. It refers to the highest existing version on that
1503 branch---the @dfn{head version} of that branch. The branches in the
1504 example above have branch numbers 1.2.1 and 1.2.2.
1505
1506 @menu
1507 * Switching Branches:: How to get to another existing branch.
1508 * Creating Branches:: How to start a new branch.
1509 * Merging:: Transferring changes between branches.
1510 * Multi-User Branching:: Multiple users working at multiple branches
1511 in parallel.
1512 @end menu
1513
1514 @node Switching Branches
1515 @subsubsection Switching between Branches
1516
1517 To switch between branches, type @kbd{C-u C-x C-q} and specify the
1518 version number you want to select. This version is then visited
1519 @emph{unlocked} (write-protected), so you can examine it before locking
1520 it. Switching branches in this way is allowed only when the file is not
1521 locked.
1522
1523 You can omit the minor version number, thus giving only the branch
1524 number; this takes you to the head version on the chosen branch. If you
1525 only type @key{RET}, Emacs goes to the highest version on the trunk.
1526
1527 After you have switched to any branch (including the main branch), you
1528 stay on it for subsequent VC commands, until you explicitly select some
1529 other branch.
1530
1531 @node Creating Branches
1532 @subsubsection Creating New Branches
1533
1534 To create a new branch from a head version (one that is the latest in
1535 the branch that contains it), first select that version if necessary,
1536 lock it with @kbd{C-x C-q}, and make whatever changes you want. Then,
1537 when you check in the changes, use @kbd{C-u C-x C-q}. This lets you
1538 specify the version number for the new version. You should specify a
1539 suitable branch number for a branch starting at the current version.
1540 For example, if the current version is 2.5, the branch number should be
1541 2.5.1, 2.5.2, and so on, depending on the number of existing branches at
1542 that point.
1543
1544 To create a new branch at an older version (one that is no longer the
1545 head of a branch), first select that version (@pxref{Switching
1546 Branches}), then lock it with @kbd{C-x C-q}. You'll be asked to
1547 confirm, when you lock the old version, that you really mean to create a
1548 new branch---if you say no, you'll be offered a chance to lock the
1549 latest version instead.
1550
1551 Then make your changes and type @kbd{C-x C-q} again to check in a new
1552 version. This automatically creates a new branch starting from the
1553 selected version. You need not specially request a new branch, because
1554 that's the only way to add a new version at a point that is not the head
1555 of a branch.
1556
1557 After the branch is created, you ``stay'' on it. That means that
1558 subsequent check-ins create new versions on that branch. To leave the
1559 branch, you must explicitly select a different version with @kbd{C-u C-x
1560 C-q}. To transfer changes from one branch to another, use the merge
1561 command, described in the next section.
1562
1563 @node Merging
1564 @subsubsection Merging Branches
1565
1566 @cindex merging changes
1567 When you have finished the changes on a certain branch, you will
1568 often want to incorporate them into the file's main line of development
1569 (the trunk). This is not a trivial operation, because development might
1570 also have proceeded on the trunk, so that you must @dfn{merge} the
1571 changes into a file that has already been changed otherwise. VC allows
1572 you to do this (and other things) with the @code{vc-merge} command.
1573
1574 @table @kbd
1575 @item C-x v m (vc-merge)
1576 Merge changes into the work file.
1577 @end table
1578
1579 @kindex C-x v m
1580 @findex vc-merge
1581 @kbd{C-x v m} (@code{vc-merge}) takes a set of changes and merges it
1582 into the current version of the work file. It first asks you for a
1583 branch number or a pair of version numbers in the minibuffer. Then it
1584 finds the changes from that branch, or between the two versions you
1585 specified, and merges them into the current version of the current file.
1586
1587 As an example, suppose that you have finished a certain feature on
1588 branch 1.3.1. In the meantime, development on the trunk has proceeded
1589 to version 1.5. To merge the changes from the branch to the trunk,
1590 first go to the head version of the trunk, by typing @kbd{C-u C-x C-q
1591 RET}. Version 1.5 is now current. If locking is used for the file,
1592 type @kbd{C-x C-q} to lock version 1.5 so that you can change it. Next,
1593 type @kbd{C-x v m 1.3.1 RET}. This takes the entire set of changes on
1594 branch 1.3.1 (relative to version 1.3, where the branch started, up to
1595 the last version on the branch) and merges it into the current version
1596 of the work file. You can now check in the changed file, thus creating
1597 version 1.6 containing the changes from the branch.
1598
1599 It is possible to do further editing after merging the branch, before
1600 the next check-in. But it is usually wiser to check in the merged
1601 version, then lock it and make the further changes. This will keep
1602 a better record of the history of changes.
1603
1604 @cindex conflicts
1605 @cindex resolving conflicts
1606 When you merge changes into a file that has itself been modified, the
1607 changes might overlap. We call this situation a @dfn{conflict}, and
1608 reconciling the conflicting changes is called @dfn{resolving a
1609 conflict}.
1610
1611 Whenever conflicts occur during merging, VC detects them, tells you
1612 about them in the echo area, and asks whether you want help in merging.
1613 If you say yes, it starts an Ediff session (@pxref{Top,
1614 Ediff, Ediff, ediff, The Ediff Manual}).
1615
1616 If you say no, the conflicting changes are both inserted into the
1617 file, surrounded by @dfn{conflict markers}. The example below shows how
1618 a conflict region looks; the file is called @samp{name} and the current
1619 master file version with user B's changes in it is 1.11.
1620
1621 @c @w here is so CVS won't think this is a conflict.
1622 @smallexample
1623 @group
1624 @w{<}<<<<<< name
1625 @var{User A's version}
1626 =======
1627 @var{User B's version}
1628 @w{>}>>>>>> 1.11
1629 @end group
1630 @end smallexample
1631
1632 @cindex vc-resolve-conflicts
1633 Then you can resolve the conflicts by editing the file manually. Or
1634 you can type @code{M-x vc-resolve-conflicts} after visiting the file.
1635 This starts an Ediff session, as described above.
1636
1637 @node Multi-User Branching
1638 @subsubsection Multi-User Branching
1639
1640 It is often useful for multiple developers to work simultaneously on
1641 different branches of a file. CVS allows this by default; for RCS, it
1642 is possible if you create multiple source directories. Each source
1643 directory should have a link named @file{RCS} which points to a common
1644 directory of RCS master files. Then each source directory can have its
1645 own choice of selected versions, but all share the same common RCS
1646 records.
1647
1648 This technique works reliably and automatically, provided that the
1649 source files contain RCS version headers (@pxref{Version Headers}). The
1650 headers enable Emacs to be sure, at all times, which version number is
1651 present in the work file.
1652
1653 If the files do not have version headers, you must instead tell Emacs
1654 explicitly in each session which branch you are working on. To do this,
1655 first find the file, then type @kbd{C-u C-x C-q} and specify the correct
1656 branch number. This ensures that Emacs knows which branch it is using
1657 during this particular editing session.
1658
1659 @node Snapshots
1660 @subsection Snapshots
1661 @cindex snapshots and version control
1662
1663 A @dfn{snapshot} is a named set of file versions (one for each
1664 registered file) that you can treat as a unit. One important kind of
1665 snapshot is a @dfn{release}, a (theoretically) stable version of the
1666 system that is ready for distribution to users.
1667
1668 @menu
1669 * Making Snapshots:: The snapshot facilities.
1670 * Snapshot Caveats:: Things to be careful of when using snapshots.
1671 @end menu
1672
1673 @node Making Snapshots
1674 @subsubsection Making and Using Snapshots
1675
1676 There are two basic commands for snapshots; one makes a
1677 snapshot with a given name, the other retrieves a named snapshot.
1678
1679 @table @code
1680 @kindex C-x v s
1681 @findex vc-create-snapshot
1682 @item C-x v s @var{name} @key{RET}
1683 Define the last saved versions of every registered file in or under the
1684 current directory as a snapshot named @var{name}
1685 (@code{vc-create-snapshot}).
1686
1687 @kindex C-x v r
1688 @findex vc-retrieve-snapshot
1689 @item C-x v r @var{name} @key{RET}
1690 For all registered files at or below the current directory level, select
1691 whatever versions correspond to the snapshot @var{name}
1692 (@code{vc-retrieve-snapshot}).
1693
1694 This command reports an error if any files are locked at or below the
1695 current directory, without changing anything; this is to avoid
1696 overwriting work in progress.
1697 @end table
1698
1699 A snapshot uses a very small amount of resources---just enough to record
1700 the list of file names and which version belongs to the snapshot. Thus,
1701 you need not hesitate to create snapshots whenever they are useful.
1702
1703 You can give a snapshot name as an argument to @kbd{C-x v =} or
1704 @kbd{C-x v ~} (@pxref{Old Versions}). Thus, you can use it to compare a
1705 snapshot against the current files, or two snapshots against each other,
1706 or a snapshot against a named version.
1707
1708 @node Snapshot Caveats
1709 @subsubsection Snapshot Caveats
1710
1711 @cindex named configurations (RCS)
1712 VC's snapshot facilities are modeled on RCS's named-configuration
1713 support. They use RCS's native facilities for this, so under VC
1714 snapshots made using RCS are visible even when you bypass VC.
1715
1716 @c worded verbosely to avoid overfull hbox.
1717 For SCCS, VC implements snapshots itself. The files it uses contain
1718 name/file/version-number triples. These snapshots are visible only
1719 through VC.
1720
1721 A snapshot is a set of checked-in versions. So make sure that all the
1722 files are checked in and not locked when you make a snapshot.
1723
1724 File renaming and deletion can create some difficulties with snapshots.
1725 This is not a VC-specific problem, but a general design issue in version
1726 control systems that no one has solved very well yet.
1727
1728 If you rename a registered file, you need to rename its master along
1729 with it (the command @code{vc-rename-file} does this automatically). If
1730 you are using SCCS, you must also update the records of the snapshot, to
1731 mention the file by its new name (@code{vc-rename-file} does this,
1732 too). An old snapshot that refers to a master file that no longer
1733 exists under the recorded name is invalid; VC can no longer retrieve
1734 it. It would be beyond the scope of this manual to explain enough about
1735 RCS and SCCS to explain how to update the snapshots by hand.
1736
1737 Using @code{vc-rename-file} makes the snapshot remain valid for
1738 retrieval, but it does not solve all problems. For example, some of the
1739 files in the program probably refer to others by name. At the very
1740 least, the makefile probably mentions the file that you renamed. If you
1741 retrieve an old snapshot, the renamed file is retrieved under its new
1742 name, which is not the name that the makefile expects. So the program
1743 won't really work as retrieved.
1744
1745 @node Miscellaneous VC
1746 @subsection Miscellaneous Commands and Features of VC
1747
1748 This section explains the less-frequently-used features of VC.
1749
1750 @menu
1751 * Change Logs and VC:: Generating a change log file from log entries.
1752 * Renaming and VC:: A command to rename both the source and master
1753 file correctly.
1754 * Version Headers:: Inserting version control headers into working files.
1755 @end menu
1756
1757 @node Change Logs and VC
1758 @subsubsection Change Logs and VC
1759
1760 If you use RCS or CVS for a program and also maintain a change log
1761 file for it (@pxref{Change Log}), you can generate change log entries
1762 automatically from the version control log entries:
1763
1764 @table @kbd
1765 @item C-x v a
1766 @kindex C-x v a
1767 @findex vc-update-change-log
1768 Visit the current directory's change log file and, for registered files
1769 in that directory, create new entries for versions checked in since the
1770 most recent entry in the change log file.
1771 (@code{vc-update-change-log}).
1772
1773 This command works with RCS or CVS only, not with SCCS.
1774
1775 @item C-u C-x v a
1776 As above, but only find entries for the current buffer's file.
1777
1778 @item M-1 C-x v a
1779 As above, but find entries for all the currently visited files that are
1780 maintained with version control. This works only with RCS, and it puts
1781 all entries in the log for the default directory, which may not be
1782 appropriate.
1783 @end table
1784
1785 For example, suppose the first line of @file{ChangeLog} is dated
1786 1999-04-10, and that the only check-in since then was by Nathaniel
1787 Bowditch to @file{rcs2log} on 1999-05-22 with log text @samp{Ignore log
1788 messages that start with `#'.}. Then @kbd{C-x v a} visits
1789 @file{ChangeLog} and inserts text like this:
1790
1791 @iftex
1792 @medbreak
1793 @end iftex
1794 @smallexample
1795 @group
1796 1999-05-22 Nathaniel Bowditch <nat@@apn.org>
1797
1798 * rcs2log: Ignore log messages that start with `#'.
1799 @end group
1800 @end smallexample
1801 @iftex
1802 @medbreak
1803 @end iftex
1804
1805 @noindent
1806 You can then edit the new change log entry further as you wish.
1807
1808 Unfortunately, timestamps in ChangeLog files are only dates, so some
1809 of the new change log entry may duplicate what's already in ChangeLog.
1810 You will have to remove these duplicates by hand.
1811
1812 Normally, the log entry for file @file{foo} is displayed as @samp{*
1813 foo: @var{text of log entry}}. The @samp{:} after @file{foo} is omitted
1814 if the text of the log entry starts with @w{@samp{(@var{functionname}):
1815 }}. For example, if the log entry for @file{vc.el} is
1816 @samp{(vc-do-command): Check call-process status.}, then the text in
1817 @file{ChangeLog} looks like this:
1818
1819 @iftex
1820 @medbreak
1821 @end iftex
1822 @smallexample
1823 @group
1824 1999-05-06 Nathaniel Bowditch <nat@@apn.org>
1825
1826 * vc.el (vc-do-command): Check call-process status.
1827 @end group
1828 @end smallexample
1829 @iftex
1830 @medbreak
1831 @end iftex
1832
1833 When @kbd{C-x v a} adds several change log entries at once, it groups
1834 related log entries together if they all are checked in by the same
1835 author at nearly the same time. If the log entries for several such
1836 files all have the same text, it coalesces them into a single entry.
1837 For example, suppose the most recent check-ins have the following log
1838 entries:
1839
1840 @flushleft
1841 @bullet{} For @file{vc.texinfo}: @samp{Fix expansion typos.}
1842 @bullet{} For @file{vc.el}: @samp{Don't call expand-file-name.}
1843 @bullet{} For @file{vc-hooks.el}: @samp{Don't call expand-file-name.}
1844 @end flushleft
1845
1846 @noindent
1847 They appear like this in @file{ChangeLog}:
1848
1849 @iftex
1850 @medbreak
1851 @end iftex
1852 @smallexample
1853 @group
1854 1999-04-01 Nathaniel Bowditch <nat@@apn.org>
1855
1856 * vc.texinfo: Fix expansion typos.
1857
1858 * vc.el, vc-hooks.el: Don't call expand-file-name.
1859 @end group
1860 @end smallexample
1861 @iftex
1862 @medbreak
1863 @end iftex
1864
1865 Normally, @kbd{C-x v a} separates log entries by a blank line, but you
1866 can mark several related log entries to be clumped together (without an
1867 intervening blank line) by starting the text of each related log entry
1868 with a label of the form @w{@samp{@{@var{clumpname}@} }}. The label
1869 itself is not copied to @file{ChangeLog}. For example, suppose the log
1870 entries are:
1871
1872 @flushleft
1873 @bullet{} For @file{vc.texinfo}: @samp{@{expand@} Fix expansion typos.}
1874 @bullet{} For @file{vc.el}: @samp{@{expand@} Don't call expand-file-name.}
1875 @bullet{} For @file{vc-hooks.el}: @samp{@{expand@} Don't call expand-file-name.}
1876 @end flushleft
1877
1878 @noindent
1879 Then the text in @file{ChangeLog} looks like this:
1880
1881 @iftex
1882 @medbreak
1883 @end iftex
1884 @smallexample
1885 @group
1886 1999-04-01 Nathaniel Bowditch <nat@@apn.org>
1887
1888 * vc.texinfo: Fix expansion typos.
1889 * vc.el, vc-hooks.el: Don't call expand-file-name.
1890 @end group
1891 @end smallexample
1892 @iftex
1893 @medbreak
1894 @end iftex
1895
1896 A log entry whose text begins with @samp{#} is not copied to
1897 @file{ChangeLog}. For example, if you merely fix some misspellings in
1898 comments, you can log the change with an entry beginning with @samp{#}
1899 to avoid putting such trivia into @file{ChangeLog}.
1900
1901 @node Renaming and VC
1902 @subsubsection Renaming VC Work Files and Master Files
1903
1904 @findex vc-rename-file
1905 When you rename a registered file, you must also rename its master
1906 file correspondingly to get proper results. Use @code{vc-rename-file}
1907 to rename the source file as you specify, and rename its master file
1908 accordingly. It also updates any snapshots (@pxref{Snapshots}) that
1909 mention the file, so that they use the new name; despite this, the
1910 snapshot thus modified may not completely work (@pxref{Snapshot
1911 Caveats}).
1912
1913 You cannot use @code{vc-rename-file} on a file that is locked by
1914 someone else.
1915
1916 @node Version Headers
1917 @subsubsection Inserting Version Control Headers
1918
1919 Sometimes it is convenient to put version identification strings
1920 directly into working files. Certain special strings called
1921 @dfn{version headers} are replaced in each successive version by the
1922 number of that version.
1923
1924 If you are using RCS, and version headers are present in your working
1925 files, Emacs can use them to determine the current version and the
1926 locking state of the files. This is more reliable than referring to the
1927 master files, which is done when there are no version headers. Note
1928 that in a multi-branch environment, version headers are necessary to
1929 make VC behave correctly (@pxref{Multi-User Branching}).
1930
1931 Searching for version headers is controlled by the variable
1932 @code{vc-consult-headers}. If it is non-@code{nil}, Emacs searches for
1933 headers to determine the version number you are editing. Setting it to
1934 @code{nil} disables this feature.
1935
1936 @kindex C-x v h
1937 @findex vc-insert-headers
1938 You can use the @kbd{C-x v h} command (@code{vc-insert-headers}) to
1939 insert a suitable header string.
1940
1941 @table @kbd
1942 @item C-x v h
1943 Insert headers in a file for use with your version-control system.
1944 @end table
1945
1946 @vindex vc-header-alist
1947 The default header string is @samp{@w{$}Id$} for RCS and
1948 @samp{@w{%}W%} for SCCS. You can specify other headers to insert by
1949 setting the variable @code{vc-header-alist}. Its value is a list of
1950 elements of the form @code{(@var{program} . @var{string})} where
1951 @var{program} is @code{RCS} or @code{SCCS} and @var{string} is the
1952 string to use.
1953
1954 Instead of a single string, you can specify a list of strings; then
1955 each string in the list is inserted as a separate header on a line of
1956 its own.
1957
1958 It is often necessary to use ``superfluous'' backslashes when writing
1959 the strings that you put in this variable. This is to prevent the
1960 string in the constant from being interpreted as a header itself if the
1961 Emacs Lisp file containing it is maintained with version control.
1962
1963 @vindex vc-comment-alist
1964 Each header is inserted surrounded by tabs, inside comment delimiters,
1965 on a new line at point. Normally the ordinary comment
1966 start and comment end strings of the current mode are used, but for
1967 certain modes, there are special comment delimiters for this purpose;
1968 the variable @code{vc-comment-alist} specifies them. Each element of
1969 this list has the form @code{(@var{mode} @var{starter} @var{ender})}.
1970
1971 @vindex vc-static-header-alist
1972 The variable @code{vc-static-header-alist} specifies further strings
1973 to add based on the name of the buffer. Its value should be a list of
1974 elements of the form @code{(@var{regexp} . @var{format})}. Whenever
1975 @var{regexp} matches the buffer name, @var{format} is inserted as part
1976 of the header. A header line is inserted for each element that matches
1977 the buffer name, and for each string specified by
1978 @code{vc-header-alist}. The header line is made by processing the
1979 string from @code{vc-header-alist} with the format taken from the
1980 element. The default value for @code{vc-static-header-alist} is as follows:
1981
1982 @example
1983 @group
1984 (("\\.c$" .
1985 "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n\
1986 #endif /* lint */\n"))
1987 @end group
1988 @end example
1989
1990 @noindent
1991 It specifies insertion of text of this form:
1992
1993 @example
1994 @group
1995
1996 #ifndef lint
1997 static char vcid[] = "@var{string}";
1998 #endif /* lint */
1999 @end group
2000 @end example
2001
2002 @noindent
2003 Note that the text above starts with a blank line.
2004
2005 If you use more than one version header in a file, put them close
2006 together in the file. The mechanism in @code{revert-buffer} that
2007 preserves markers may not handle markers positioned between two version
2008 headers.
2009
2010 @node Customizing VC
2011 @subsection Customizing VC
2012
2013 There are many ways of customizing VC. The options you can set fall
2014 into four categories, described in the following sections.
2015
2016 @vindex vc-ignore-vc-files
2017 @cindex Version control, deactivating
2018 In addition, it is possible to turn VC on and off generally by setting
2019 the variable @code{vc-ignore-vc-files}. Normally VC will notice the
2020 presence of version control on a file you visit and automatically invoke
2021 the relevant program to check the file's state. Change
2022 @code{vc-ignore-vc-files} if this isn't the right thing, for instance,
2023 if you edit files under version control but don't have the relevant
2024 version control programs available.
2025
2026 @menu
2027 * Backend Options:: Customizing the back-end to your needs.
2028 * VC Workfile Handling:: Various options concerning working files.
2029 * VC Status Retrieval:: How VC finds the version control status of a file,
2030 and how to customize this.
2031 * VC Command Execution:: Which commands VC should run, and how.
2032 @end menu
2033
2034 @node Backend Options
2035 @subsubsection Options for VC Backends
2036
2037 @cindex backend options (VC)
2038 @cindex locking under version control
2039 You can tell RCS and CVS whether to use locking for a file or not
2040 (@pxref{VC Concepts}, for a description of locking). VC automatically
2041 recognizes what you have chosen, and behaves accordingly.
2042
2043 @cindex non-strict locking (RCS)
2044 @cindex locking, non-strict (RCS)
2045 For RCS, the default is to use locking, but there is a mode called
2046 @dfn{non-strict locking} in which you can check-in changes without
2047 locking the file first. Use @samp{rcs -U} to switch to non-strict
2048 locking for a particular file, see the @samp{rcs} manpage for details.
2049
2050 @cindex locking (CVS)
2051 Under CVS, the default is not to use locking; anyone can change a work
2052 file at any time. However, there are ways to restrict this, resulting
2053 in behavior that resembles locking.
2054
2055 @cindex CVSREAD environment variable (CVS)
2056 For one thing, you can set the @code{CVSREAD} environment variable to
2057 an arbitrary value. If this variable is defined, CVS makes your work
2058 files read-only by default. In Emacs, you must type @kbd{C-x C-q} to
2059 make the file writeable, so that editing works in fact similar as if
2060 locking was used. Note however, that no actual locking is performed, so
2061 several users can make their files writeable at the same time. When
2062 setting @code{CVSREAD} for the first time, make sure to check out all
2063 your modules anew, so that the file protections are set correctly.
2064
2065 @cindex cvs watch feature
2066 @cindex watching files (CVS)
2067 Another way to achieve something similar to locking is to use the
2068 @dfn{watch} feature of CVS. If a file is being watched, CVS makes it
2069 read-only by default, and you must also use @kbd{C-x C-q} in Emacs to
2070 make it writable. VC calls @code{cvs edit} to make the file writeable,
2071 and CVS takes care to notify other developers of the fact that you
2072 intend to change the file. See the CVS documentation for details on
2073 using the watch feature.
2074
2075 @vindex vc-handle-cvs
2076 You can turn off use of VC for CVS-managed files by setting the
2077 variable @code{vc-handle-cvs} to @code{nil}. If you do this, Emacs
2078 treats these files as if they were not registered, and the VC commands
2079 are not available for them. You must do all CVS operations manually.
2080
2081 @node VC Workfile Handling
2082 @subsubsection VC Workfile Handling
2083
2084 @vindex vc-make-backup-files
2085 Emacs normally does not save backup files for source files that are
2086 maintained with version control. If you want to make backup files even
2087 for files that use version control, set the variable
2088 @code{vc-make-backup-files} to a non-@code{nil} value.
2089
2090 @vindex vc-keep-workfiles
2091 Normally the work file exists all the time, whether it is locked or
2092 not. If you set @code{vc-keep-workfiles} to @code{nil}, then checking
2093 in a new version with @kbd{C-x C-q} deletes the work file; but any
2094 attempt to visit the file with Emacs creates it again. (With CVS, work
2095 files are always kept.)
2096
2097 @vindex vc-follow-symlinks
2098 Editing a version-controlled file through a symbolic link can be
2099 dangerous. It bypasses the version control system---you can edit the
2100 file without locking it, and fail to check your changes in. Also,
2101 your changes might overwrite those of another user. To protect against
2102 this, VC checks each symbolic link that you visit, to see if it points
2103 to a file under version control.
2104
2105 The variable @code{vc-follow-symlinks} controls what to do when a
2106 symbolic link points to a version-controlled file. If it is @code{nil},
2107 VC only displays a warning message. If it is @code{t}, VC automatically
2108 follows the link, and visits the real file instead, telling you about
2109 this in the echo area. If the value is @code{ask} (the default), VC
2110 asks you each time whether to follow the link.
2111
2112 @node VC Status Retrieval
2113 @subsubsection VC Status Retrieval
2114 @c There is no need to tell users about vc-master-templates.
2115
2116 When deducing the locked/unlocked state of a file, VC first looks for
2117 an RCS version header string in the file (@pxref{Version Headers}). If
2118 there is no header string, or if you are using SCCS, VC normally looks
2119 at the file permissions of the work file; this is fast. But there might
2120 be situations when the file permissions cannot be trusted. In this case
2121 the master file has to be consulted, which is rather expensive. Also
2122 the master file can only tell you @emph{if} there's any lock on the
2123 file, but not whether your work file really contains that locked
2124 version.
2125
2126 @vindex vc-consult-headers
2127 You can tell VC not to use version headers to determine lock status by
2128 setting @code{vc-consult-headers} to @code{nil}. VC then always uses
2129 the file permissions (if it can trust them), or else checks the master
2130 file.
2131
2132 @vindex vc-mistrust-permissions
2133 You can specify the criterion for whether to trust the file
2134 permissions by setting the variable @code{vc-mistrust-permissions}. Its
2135 value can be @code{t} (always mistrust the file permissions and check
2136 the master file), @code{nil} (always trust the file permissions), or a
2137 function of one argument which makes the decision. The argument is the
2138 directory name of the @file{RCS}, @file{CVS} or @file{SCCS}
2139 subdirectory. A non-@code{nil} value from the function says to mistrust
2140 the file permissions. If you find that the file permissions of work
2141 files are changed erroneously, set @code{vc-mistrust-permissions} to
2142 @code{t}. Then VC always checks the master file to determine the file's
2143 status.
2144
2145 @node VC Command Execution
2146 @subsubsection VC Command Execution
2147
2148 @vindex vc-suppress-confirm
2149 If @code{vc-suppress-confirm} is non-@code{nil}, then @kbd{C-x C-q}
2150 and @kbd{C-x v i} can save the current buffer without asking, and
2151 @kbd{C-x v u} also operates without asking for confirmation. (This
2152 variable does not affect @kbd{C-x v c}; that operation is so drastic
2153 that it should always ask for confirmation.)
2154
2155 @vindex vc-command-messages
2156 VC mode does much of its work by running the shell commands for RCS,
2157 CVS and SCCS. If @code{vc-command-messages} is non-@code{nil}, VC
2158 displays messages to indicate which shell commands it runs, and
2159 additional messages when the commands finish.
2160
2161 @vindex vc-path
2162 You can specify additional directories to search for version control
2163 programs by setting the variable @code{vc-path}. These directories are
2164 searched before the usual search path. But the proper files are usually
2165 found automatically.
2166
2167 @node Directories
2168 @section File Directories
2169
2170 @cindex file directory
2171 @cindex directory listing
2172 The file system groups files into @dfn{directories}. A @dfn{directory
2173 listing} is a list of all the files in a directory. Emacs provides
2174 commands to create and delete directories, and to make directory
2175 listings in brief format (file names only) and verbose format (sizes,
2176 dates, and authors included). There is also a directory browser called
2177 Dired; see @ref{Dired}.
2178
2179 @table @kbd
2180 @item C-x C-d @var{dir-or-pattern} @key{RET}
2181 Display a brief directory listing (@code{list-directory}).
2182 @item C-u C-x C-d @var{dir-or-pattern} @key{RET}
2183 Display a verbose directory listing.
2184 @item M-x make-directory @key{RET} @var{dirname} @key{RET}
2185 Create a new directory named @var{dirname}.
2186 @item M-x delete-directory @key{RET} @var{dirname} @key{RET}
2187 Delete the directory named @var{dirname}. It must be empty,
2188 or you get an error.
2189 @end table
2190
2191 @findex list-directory
2192 @kindex C-x C-d
2193 The command to display a directory listing is @kbd{C-x C-d}
2194 (@code{list-directory}). It reads using the minibuffer a file name
2195 which is either a directory to be listed or a wildcard-containing
2196 pattern for the files to be listed. For example,
2197
2198 @example
2199 C-x C-d /u2/emacs/etc @key{RET}
2200 @end example
2201
2202 @noindent
2203 lists all the files in directory @file{/u2/emacs/etc}. Here is an
2204 example of specifying a file name pattern:
2205
2206 @example
2207 C-x C-d /u2/emacs/src/*.c @key{RET}
2208 @end example
2209
2210 Normally, @kbd{C-x C-d} prints a brief directory listing containing
2211 just file names. A numeric argument (regardless of value) tells it to
2212 make a verbose listing including sizes, dates, and authors (like
2213 @samp{ls -l}).
2214
2215 @vindex list-directory-brief-switches
2216 @vindex list-directory-verbose-switches
2217 The text of a directory listing is obtained by running @code{ls} in an
2218 inferior process. Two Emacs variables control the switches passed to
2219 @code{ls}: @code{list-directory-brief-switches} is a string giving the
2220 switches to use in brief listings (@code{"-CF"} by default), and
2221 @code{list-directory-verbose-switches} is a string giving the switches to
2222 use in a verbose listing (@code{"-l"} by default).
2223
2224 @node Comparing Files
2225 @section Comparing Files
2226 @cindex comparing files
2227
2228 @findex diff
2229 @vindex diff-switches
2230 The command @kbd{M-x diff} compares two files, displaying the
2231 differences in an Emacs buffer named @samp{*Diff*}. It works by running
2232 the @code{diff} program, using options taken from the variable
2233 @code{diff-switches}, whose value should be a string.
2234
2235 The buffer @samp{*Diff*} has Compilation mode as its major mode, so
2236 you can use @kbd{C-x `} to visit successive changed locations in the two
2237 source files. You can also move to a particular hunk of changes and
2238 type @key{RET} or @kbd{C-c C-c}, or click @kbd{Mouse-2} on it, to move
2239 to the corresponding source location. You can also use the other
2240 special commands of Compilation mode: @key{SPC} and @key{DEL} for
2241 scrolling, and @kbd{M-p} and @kbd{M-n} for cursor motion.
2242 @xref{Compilation}.
2243
2244 @findex diff-backup
2245 The command @kbd{M-x diff-backup} compares a specified file with its most
2246 recent backup. If you specify the name of a backup file,
2247 @code{diff-backup} compares it with the source file that it is a backup
2248 of.
2249
2250 @findex compare-windows
2251 The command @kbd{M-x compare-windows} compares the text in the current
2252 window with that in the next window. Comparison starts at point in each
2253 window, and each starting position is pushed on the mark ring in its
2254 respective buffer. Then point moves forward in each window, a character
2255 at a time, until a mismatch between the two windows is reached. Then
2256 the command is finished. For more information about windows in Emacs,
2257 @ref{Windows}.
2258
2259 @vindex compare-ignore-case
2260 With a numeric argument, @code{compare-windows} ignores changes in
2261 whitespace. If the variable @code{compare-ignore-case} is
2262 non-@code{nil}, it ignores differences in case as well.
2263
2264 See also @ref{Emerge}, for convenient facilities for merging two
2265 similar files.
2266
2267 @node Misc File Ops
2268 @section Miscellaneous File Operations
2269
2270 Emacs has commands for performing many other operations on files.
2271 All operate on one file; they do not accept wildcard file names.
2272
2273 @findex view-file
2274 @cindex viewing
2275 @cindex View mode
2276 @cindex mode, View
2277 @kbd{M-x view-file} allows you to scan or read a file by sequential
2278 screenfuls. It reads a file name argument using the minibuffer. After
2279 reading the file into an Emacs buffer, @code{view-file} displays the
2280 beginning. You can then type @key{SPC} to scroll forward one windowful,
2281 or @key{DEL} to scroll backward. Various other commands are provided
2282 for moving around in the file, but none for changing it; type @kbd{?}
2283 while viewing for a list of them. They are mostly the same as normal
2284 Emacs cursor motion commands. To exit from viewing, type @kbd{q}.
2285 The commands for viewing are defined by a special major mode called View
2286 mode.
2287
2288 A related command, @kbd{M-x view-buffer}, views a buffer already present
2289 in Emacs. @xref{Misc Buffer}.
2290
2291 @findex insert-file
2292 @kbd{M-x insert-file} inserts a copy of the contents of the specified
2293 file into the current buffer at point, leaving point unchanged before the
2294 contents and the mark after them.
2295
2296 @findex write-region
2297 @kbd{M-x write-region} is the inverse of @kbd{M-x insert-file}; it
2298 copies the contents of the region into the specified file. @kbd{M-x
2299 append-to-file} adds the text of the region to the end of the specified
2300 file. @xref{Accumulating Text}.
2301
2302 @findex delete-file
2303 @cindex deletion (of files)
2304 @kbd{M-x delete-file} deletes the specified file, like the @code{rm}
2305 command in the shell. If you are deleting many files in one directory, it
2306 may be more convenient to use Dired (@pxref{Dired}).
2307
2308 @findex rename-file
2309 @kbd{M-x rename-file} reads two file names @var{old} and @var{new} using
2310 the minibuffer, then renames file @var{old} as @var{new}. If a file named
2311 @var{new} already exists, you must confirm with @kbd{yes} or renaming is not
2312 done; this is because renaming causes the old meaning of the name @var{new}
2313 to be lost. If @var{old} and @var{new} are on different file systems, the
2314 file @var{old} is copied and deleted.
2315
2316 @findex add-name-to-file
2317 The similar command @kbd{M-x add-name-to-file} is used to add an
2318 additional name to an existing file without removing its old name.
2319 The new name must belong on the same file system that the file is on.
2320
2321 @findex copy-file
2322 @cindex copying files
2323 @kbd{M-x copy-file} reads the file @var{old} and writes a new file named
2324 @var{new} with the same contents. Confirmation is required if a file named
2325 @var{new} already exists, because copying has the consequence of overwriting
2326 the old contents of the file @var{new}.
2327
2328 @findex make-symbolic-link
2329 @kbd{M-x make-symbolic-link} reads two file names @var{target} and
2330 @var{linkname}, then creates a symbolic link named @var{linkname} and
2331 pointing at @var{target}. The effect is that future attempts to open file
2332 @var{linkname} will refer to whatever file is named @var{target} at the
2333 time the opening is done, or will get an error if the name @var{target} is
2334 not in use at that time. This command does not expand the argument
2335 @var{target}, so that it allows you to specify a relative name
2336 as the target of the link.
2337
2338 Confirmation is required when creating the link if @var{linkname} is
2339 in use. Note that not all systems support symbolic links.
2340
2341 @node Compressed Files
2342 @section Accessing Compressed Files
2343 @cindex compression
2344 @cindex uncompression
2345 @cindex Auto Compression mode
2346 @cindex mode, Auto Compression
2347 @pindex gzip
2348
2349 @findex auto-compression-mode
2350 @vindex auto-compression-mode
2351 Emacs comes with a library that can automatically uncompress
2352 compressed files when you visit them, and automatically recompress them
2353 if you alter them and save them. To enable this feature, type the
2354 command @kbd{M-x auto-compression-mode}. You can enable it permanently
2355 by customizing the option @var{auto-compression-mode}.
2356
2357 When automatic compression (which implies automatic uncompression as
2358 well) is enabled, Emacs recognizes compressed files by their file names.
2359 File names ending in @samp{.gz} indicate a file compressed with
2360 @code{gzip}. Other endings indicate other compression programs.
2361
2362 Automatic uncompression and compression apply to all the operations in
2363 which Emacs uses the contents of a file. This includes visiting it,
2364 saving it, inserting its contents into a buffer, loading it, and byte
2365 compiling it.
2366
2367 @node File Archives
2368 @section File Archives
2369 @cindex mode, tar
2370 @cindex Tar mode
2371 @cindex Archive mode
2372 @cindex mode, archive
2373 @cindex @code{arc}
2374 @cindex @code{jar}
2375 @cindex @code{zip}
2376 @cindex @code{lzh}
2377 @cindex @code{zoo}
2378 @pindex tar
2379 @pindex arc
2380 @pindex jar
2381 @pindex zip
2382 @pindex lzh
2383 @pindex zoo
2384
2385 If you visit a file with extension @samp{.tar}, it is assumed to be an
2386 `archive' made by the @code{tar} program and it is viewed in a Tar mode
2387 buffer. This provides a Dired-like listing of the contents.
2388 @xref{Dired}. You can move around the component files as in Dired to
2389 visit and manipulate them.
2390
2391 The keys @kbd{e}, @kbd{f} and @kbd{RET} all extract a component file
2392 into its own buffer. You can edit it there and when you save the buffer
2393 the edited version will replace the version in the Tar buffer. @var{v}
2394 extracts a file into a buffer in View mode. @kbd{d} marks a file for
2395 deletion when you later use @kbd{x}, as in Dired. @kbd{C} copies a file
2396 from the archive to disk and @kbd{R} renames a file.
2397
2398 Saving the Tar buffer writes a new version of the archive to disk with
2399 the changes you made to the components.
2400
2401 If you enable Auto Compression mode (@pxref{Compressed Files}), then Tar
2402 mode will be used also for compressed archives in files with extensions
2403 @samp{.tgz}, @code{.tar.Z} and @code{.tar.gz}.
2404
2405 @cindex @code{arc}
2406 @cindex @code{jar}
2407 @cindex @code{zip}
2408 @cindex @code{lzh}
2409 @cindex @code{zoo}
2410 @pindex tar
2411 @pindex arc
2412 @pindex jar
2413 @pindex zip
2414 @pindex lzh
2415 @pindex zoo
2416 @cindex Java class archives
2417 A separate but similar Archive mode, is used for archives produced by
2418 the programs @code{arc}, @code{zip}, @code{lzh} and @code{zoo} which
2419 have extensions corresponding to the program names. These archiving
2420 programs are typically used on MS-DOS and MS-Windows systems. Java
2421 class archives with extension @samp{.jar} are also recognized. The
2422 keybindings in Archive mode are similar to those in Tar mode.
2423
2424 It is not necessary to have the archiving programs installed to use Tar
2425 mode or Archive mode---Emacs reads the archives directly.
2426
2427 @node Remote Files
2428 @section Remote Files
2429
2430 @cindex FTP
2431 @cindex remote file access
2432 You can refer to files on other machines using a special file name syntax:
2433
2434 @example
2435 @group
2436 /@var{host}:@var{filename}
2437 /@var{user}@@@var{host}:@var{filename}
2438 /@var{user}@@@var{host}#@var{port}:@var{filename}
2439 @end group
2440 @end example
2441
2442 @noindent
2443 When you do this, Emacs uses the FTP program to read and write files on
2444 the specified host. It logs in through FTP using your user name or the
2445 name @var{user}. It may ask you for a password from time to time; this
2446 is used for logging in on @var{host}. The form using @var{port} allows
2447 you to access servers running on a non-default TCP port.
2448
2449 @cindex ange-ftp
2450 @vindex ange-ftp-default-user
2451 Normally, if you do not specify a user name in a remote file name,
2452 that means to use your own user name. But if you set the variable
2453 @code{ange-ftp-default-user} to a string, that string is used instead.
2454 (The Emacs package that implements FTP file access is called
2455 @code{ange-ftp}.)
2456
2457 @vindex file-name-handler-alist
2458 You can entirely turn off the FTP file name feature by removing the
2459 entries @var{ange-ftp-completion-hook-function} and
2460 @var{ange-ftp-hook-function} from the variable
2461 @code{file-name-handler-alist}.
2462
2463 @node Quoted File Names
2464 @section Quoted File Names
2465
2466 @cindex quoting file names
2467 You can @dfn{quote} an absolute file name to prevent special
2468 characters and syntax in it from having their special effects.
2469 The way to do this is to add @samp{/:} at the beginning.
2470
2471 For example, you can quote a local file name which appears remote, to
2472 prevent it from being treated as a remote file name. Thus, if you have
2473 a directory named @file{/foo:} and a file named @file{bar} in it, you
2474 can refer to that file in Emacs as @samp{/:/foo:/bar}.
2475
2476 @samp{/:} can also prevent @samp{~} from being treated as a special
2477 character for a user's home directory. For example, @file{/:/tmp/~hack}
2478 refers to a file whose name is @file{~hack} in directory @file{/tmp}.
2479
2480 Likewise, quoting with @samp{/:} is one way to enter in the minibuffer
2481 a file name that contains @samp{$}. However, the @samp{/:} must be at
2482 the beginning of the buffer in order to quote @samp{$}.
2483
2484 You can also quote wildcard characters with @samp{/:}, for visiting.
2485 For example, @file{/:/tmp/foo*bar} visits the file @file{/tmp/foo*bar}.
2486 However, in most cases you can simply type the wildcard characters for
2487 themselves. For example, if the only file name in @file{/tmp} that
2488 starts with @samp{foo} and ends with @samp{bar} is @file{foo*bar}, then
2489 specifying @file{/tmp/foo*bar} will visit just @file{/tmp/foo*bar}.
2490