files.texi 79 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. @node Files, Buffers, Fixit, Top
  2. @chapter File Handling
  3. @cindex files
  4. The basic unit of stored data in Unix is the @dfn{file}. To edit a file,
  5. you must tell Emacs to examine the file and prepare a buffer containing a
  6. copy of the file's text. This is called @dfn{visiting} the file. Editing
  7. commands apply directly to text in the buffer; that is, to the copy inside
  8. Emacs. Your changes appear in the file itself only when you @dfn{save} the
  9. buffer back into the file.
  10. @cindex files, remote
  11. @cindex remote files
  12. Emacs is also able to handle ``remote files'' which are stored on
  13. other hosts. Not only is Emacs somewhat aware of the special issues
  14. involved with network file systems, but it can also use FTP and ssh (or
  15. rsh) to make local copies of the files, and refresh them on the remote
  16. host automatically when you save the buffer. The FTP interface is
  17. provided by the standard @samp{efs} package @ref{Top, EFS, , efs}. The
  18. ssh/rsh interface is provided by the optional @samp{tramp} package
  19. @ref{Top, TRAMP, , tramp}. These packages attempt to implement all of
  20. the operations described below, making remote file use transparent
  21. (except for unavoidable network delays).
  22. In addition to visiting and saving files, Emacs can delete, copy, rename,
  23. and append to files, and operate on file directories.
  24. @menu
  25. * File Names:: How to type and edit file name arguments.
  26. * Visiting:: Visiting a file prepares Emacs to edit the file.
  27. * Saving:: Saving makes your changes permanent.
  28. * Reverting:: Reverting cancels all the changes not saved.
  29. * Auto Save:: Auto Save periodically protects against loss of data.
  30. * Version Control:: Version control systems (RCS and SCCS).
  31. * ListDir:: Listing the contents of a file directory.
  32. * Comparing Files:: Finding where two files differ.
  33. * Dired:: ``Editing'' a directory to delete, rename, etc.
  34. the files in it.
  35. * Misc File Ops:: Other things you can do on files.
  36. @end menu
  37. @node File Names, Visiting, Files, Files
  38. @section File Names
  39. @cindex file names
  40. Most Emacs commands that operate on a file require you to specify the
  41. file name. (Saving and reverting are exceptions; the buffer knows which
  42. file name to use for them.) File names are specified in the minibuffer
  43. (@pxref{Minibuffer}). @dfn{Completion} is available, to make it easier to
  44. specify long file names. @xref{Completion}.
  45. There is always a @dfn{default file name} which is used if you
  46. enter an empty argument by typing just @key{RET}. Normally the default
  47. file name is the name of the file visited in the current buffer; this
  48. makes it easy to operate on that file with any of the Emacs file
  49. commands.
  50. The syntax for accessing remote files unfortunately varies depending on
  51. the method used. The syntax for using FTP is
  52. @samp{/@var{user}@@@var{remote-host}:@var{path-on-remote-host}}. The
  53. syntax for using ssh is
  54. @samp{/[@var{user}@@@var{remote-host}]@var{path-on-remote-host}}.
  55. In both cases the @samp{@var{user}@@} portion is optional (it defaults
  56. to your local user name). @var{path-on-remote-host} may use the
  57. @samp{~} notation to indicate @var{user}'s home directory on the remote
  58. host. The default file name will reflect the remote host information.
  59. @vindex default-directory
  60. Each buffer has a default directory, normally the same as the
  61. directory of the file visited in that buffer. When Emacs reads a file
  62. name, the default directory is used if you do not specify a directory.
  63. If you specify a directory in a relative fashion, with a name that does
  64. not start with a slash, it is interpreted with respect to the default
  65. directory. The default directory of the current buffer is kept in the
  66. variable @code{default-directory}, which has a separate value in every
  67. buffer. The value of the variable should end with a slash.
  68. For example, if the default file name is @file{/u/rms/gnu/gnu.tasks} then
  69. the default directory is @file{/u/rms/gnu/}. If you type just @samp{foo},
  70. which does not specify a directory, it is short for @file{/u/rms/gnu/foo}.
  71. @samp{../.login} would stand for @file{/u/rms/.login}. @samp{new/foo}
  72. would stand for the filename @file{/u/rms/gnu/new/foo}.
  73. When visiting a remote file via EFS or TRAMP, the remote directory
  74. becomes the default directory (@pxref{Visiting}) for that buffer, just
  75. as a local directory would.
  76. @vindex default-directory-alist
  77. The variable @code{default-directory-alist} takes an alist of major
  78. modes and their opinions on @code{default-directory} as a Lisp
  79. expression to evaluate. A resulting value of @code{nil} is ignored in
  80. favor of @code{default-directory}.
  81. @findex make-directory
  82. @findex remove-directory
  83. @cindex creating directories
  84. @cindex removing directories
  85. You can create a new directory with the function @code{make-directory},
  86. which takes as an argument a file name string. The current directory is
  87. displayed in the minibuffer when the function is called; you can delete
  88. the old directory name and supply a new directory name. For example, if
  89. the current directory is @file{/u/rms/gnu}, you can delete @file{gnu}
  90. and type @file{oryx} and @key{RET} to create @file{/u/rms/oryx}.
  91. Removing a directory is similar to creating one. To remove a directory,
  92. use @code{remove-directory}; it takes one argument, a file name string.
  93. The command @kbd{M-x pwd} prints the current buffer's default directory,
  94. and the command @kbd{M-x cd} sets it (to a value read using the
  95. minibuffer). A buffer's default directory changes only when the @code{cd}
  96. command is used. A file-visiting buffer's default directory is initialized
  97. to the directory of the file that is visited there. If a buffer is created
  98. with @kbd{C-x b}, its default directory is copied from that of the
  99. buffer that was current at the time.
  100. @vindex insert-default-directory
  101. The default directory name actually appears in the minibuffer when the
  102. minibuffer becomes active to read a file name. This serves two
  103. purposes: it shows you what the default is, so that you can type a
  104. relative file name and know with certainty what it will mean, and it
  105. allows you to edit the default to specify a different directory. To
  106. inhibit the insertion of the default directory, set the variable
  107. @code{insert-default-directory} to @code{nil}.
  108. Note that it is legitimate to type an absolute file name after you
  109. enter the minibuffer, ignoring the presence of the default directory
  110. name. The final minibuffer contents may look invalid, but that is not
  111. so. @xref{Minibuffer File}.
  112. @samp{$} in a file name is used to substitute environment variables. For
  113. example, if you have used the shell command @samp{setenv FOO rms/hacks} to
  114. set up an environment variable named @samp{FOO}, then you can use
  115. @file{/u/$FOO/test.c} or @file{/u/$@{FOO@}/test.c} as an abbreviation for
  116. @file{/u/rms/hacks/test.c}. The environment variable name consists of all
  117. the alphanumeric characters after the @samp{$}; alternatively, it may be
  118. enclosed in braces after the @samp{$}. Note that the @samp{setenv} command
  119. affects Emacs only if done before Emacs is started.
  120. To access a file with @samp{$} in its name, type @samp{$$}. This pair
  121. is converted to a single @samp{$} at the same time variable substitution
  122. is performed for single @samp{$}. The Lisp function that performs the
  123. substitution is called @code{substitute-in-file-name}. The substitution
  124. is performed only on filenames read as such using the minibuffer.
  125. @node Visiting, Saving, File Names, Files
  126. @section Visiting Files
  127. @cindex visiting files
  128. @c WideCommands
  129. @table @kbd
  130. @item C-x C-f
  131. Visit a file (@code{find-file}).
  132. @item C-x C-v
  133. Visit a different file instead of the one visited last
  134. (@code{find-alternate-file}).
  135. @item C-x 4 C-f
  136. Visit a file, in another window (@code{find-file-other-window}). Don't
  137. change this window.
  138. @item C-x 5 C-f
  139. Visit a file, in another frame (@code{find-file-other-frame}). Don't
  140. change this window or frame.
  141. @end table
  142. @cindex files
  143. @cindex visiting
  144. @cindex saving
  145. @dfn{Visiting} a file means copying its contents into an Emacs buffer
  146. so you can edit it. Emacs creates a new buffer for each file you
  147. visit. We say that the buffer is visiting the file that it was created
  148. to hold. Emacs constructs the buffer name from the file name by
  149. throwing away the directory and keeping just the file name. For example,
  150. a file named @file{/usr/rms/emacs.tex} is displayed in a buffer named
  151. @samp{emacs.tex}. If a buffer with that name exists, a unique
  152. name is constructed by appending @samp{<2>}, @samp{<3>},and so on, using
  153. the lowest number that makes a name that is not already in use.
  154. Each window's mode line shows the name of the buffer that is being displayed
  155. in that window, so you can always tell what buffer you are editing.
  156. The changes you make with Emacs are made in the Emacs buffer. They do
  157. not take effect in the file that you visit, or any other permanent
  158. place, until you @dfn{save} the buffer. Saving the buffer means that
  159. Emacs writes the current contents of the buffer into its visited file.
  160. @xref{Saving}.
  161. @cindex modified (buffer)
  162. If a buffer contains changes that have not been saved, the buffer is said
  163. to be @dfn{modified}. This is important because it implies that some
  164. changes will be lost if the buffer is not saved. The mode line displays
  165. two stars near the left margin if the buffer is modified.
  166. @kindex C-x 5 C-f
  167. @findex find-file
  168. @findex find-file-other-frame
  169. To visit a file, use the command @kbd{C-x C-f} (@code{find-file}). Follow
  170. the command with the name of the file you wish to visit, terminated by a
  171. @key{RET}. If you are using XEmacs under X, you can also use the
  172. @b{Open...} command from the @b{File} menu bar item.
  173. The file name is read using the minibuffer (@pxref{Minibuffer}), with
  174. defaulting and completion in the standard manner (@pxref{File Names}).
  175. While in the minibuffer, you can abort @kbd{C-x C-f} by typing @kbd{C-g}.
  176. @kbd{C-x C-f} has completed successfully when text appears on the
  177. screen and a new buffer name appears in the mode line. If the specified
  178. file does not exist and could not be created or cannot be read, an error
  179. results. The error message is printed in the echo area, and includes
  180. the name of the file that Emacs was trying to visit.
  181. If you visit a file that is already in Emacs, @kbd{C-x C-f} does not make
  182. another copy. It selects the existing buffer containing that file.
  183. However, before doing so, it checks that the file itself has not changed
  184. since you visited or saved it last. If the file has changed, Emacs
  185. prints a warning message. @xref{Interlocking,,Simultaneous Editing}.
  186. @findex find-this-file
  187. You can switch to a specific file called out in the current buffer by
  188. calling the function @code{find-this-file}. By providing a prefix
  189. argument, this function calls @code{filename-at-point} and switches to a
  190. buffer visiting the file @var{filename}. It creates one if none already
  191. exists. You can use this function to edit the file mentioned in the
  192. buffer you are working in or to test if the file exists. You can do that
  193. by using the minibuffer completion after snatching the all or part of
  194. the filename.
  195. @vindex find-file-use-truenames
  196. @vindex buffer-file-name
  197. If the variable @code{find-file-use-truenames}'s value is
  198. non-@code{nil}, a buffer's visited filename will always be traced back
  199. to the real file. The filename will never be a symbolic link, and there
  200. will never be a symbolic link anywhere in its directory path. In other
  201. words, the @code{buffer-file-name} and @code{buffer-file-truename} will
  202. be equal.
  203. @vindex find-file-compare-truenames
  204. @vindex buffer-file-truename
  205. If the variable @code{find-file-compare-truenames} value is
  206. non-@code{nil}, the @code{find-file} command will check the
  207. @code{buffer-file-truename} of all visited files when deciding whether a
  208. given file is already in a buffer, instead of just
  209. @code{buffer-file-name}. If you attempt to visit another file which is
  210. a symbolic link to a file that is already in a buffer, the existing
  211. buffer will be found instead of a newly created one. This works if any
  212. component of the pathname (including a non-terminal component) is a
  213. symbolic link as well, but doesn't work with hard links (nothing does).
  214. @cindex creating files
  215. If you want to create a file, just visit it. Emacs prints
  216. @samp{(New File)} in the echo area, but in other respects behaves as if you
  217. had visited an existing empty file. If you make any changes and save them,
  218. the file is created.
  219. @kindex C-x C-v
  220. @findex find-alternate-file
  221. If you visit a nonexistent file unintentionally (because you typed the
  222. wrong file name), use the @kbd{C-x C-v} (@code{find-alternate-file})
  223. command to visit the file you wanted. @kbd{C-x C-v} is similar to @kbd{C-x
  224. C-f}, but it kills the current buffer (after first offering to save it if
  225. it is modified). @kbd{C-x C-v} is allowed even if the current buffer
  226. is not visiting a file.
  227. @vindex find-file-run-dired
  228. If the file you specify is actually a directory, Dired is called on
  229. that directory (@pxref{Dired}). To inhibit this, set the variable
  230. @code{find-file-run-dired} to @code{nil}; then it is an error to try to
  231. visit a directory.
  232. @kindex C-x 4 f
  233. @findex find-file-other-window
  234. @kbd{C-x 4 f} (@code{find-file-other-window}) is like @kbd{C-x C-f}
  235. except that the buffer containing the specified file is selected in another
  236. window. The window that was selected before @kbd{C-x 4 f} continues to
  237. show the same buffer it was already showing. If you use this command when
  238. only one window is being displayed, that window is split in two, with one
  239. window showing the same buffer as before, and the other one showing the
  240. newly requested file. @xref{Windows}.
  241. @kindex C-x 5 C-f
  242. @findex find-file-other-frame
  243. @kbd{C-x 5 C-f} (@code{find-file-other-frame}) is like @kbd{C-x C-f}
  244. except that it creates a new frame in which the file is displayed.
  245. @findex find-this-file-other-window
  246. Use the function @code{find-this-file-other-window} to edit a file
  247. mentioned in the buffer you are editing or to test if that file exists.
  248. To do this, use the minibuffer completion after snatching the part or
  249. all of the filename. By providing a prefix argument, the function calls
  250. @code{filename-at-point} and switches you to a buffer visiting the file
  251. @var{filename} in another window. The function creates a buffer if none
  252. already exists. This function is similar to @code{find-file-other-window}.
  253. @vindex find-file-hooks
  254. @vindex find-file-not-found-hooks
  255. There are two hook variables that allow extensions to modify the
  256. operation of visiting files. Visiting a file that does not exist runs the
  257. functions in the list @code{find-file-not-found-hooks}; the value of this
  258. variable is expected to be a list of functions which are
  259. called one by one until one of them returns non-@code{nil}. Any visiting
  260. of a file, whether extant or not, expects @code{find-file-hooks} to
  261. contain list of functions and calls them all, one by one. In both cases
  262. the functions receive no arguments. Visiting a nonexistent file
  263. runs the @code{find-file-not-found-hooks} first.
  264. @node Saving, Reverting, Visiting, Files
  265. @section Saving Files
  266. @dfn{Saving} a buffer in Emacs means writing its contents back into the file
  267. that was visited in the buffer.
  268. @table @kbd
  269. @item C-x C-s
  270. Save the current buffer in its visited file (@code{save-buffer}).
  271. @item C-x s
  272. Save any or all buffers in their visited files (@code{save-some-buffers}).
  273. @item M-~
  274. Forget that the current buffer has been changed (@code{not-modified}).
  275. @item C-x C-w
  276. Save the current buffer in a specified file, and record that file as
  277. the one visited in the buffer (@code{write-file}).
  278. @item M-x set-visited-file-name
  279. Change file the name under which the current buffer will be saved.
  280. @end table
  281. @kindex C-x C-s
  282. @findex save-buffer
  283. To save a file and make your changes permanent, type
  284. @kbd{C-x C-s} (@code{save-buffer}). After saving is finished, @kbd{C-x C-s}
  285. prints a message such as:
  286. @example
  287. Wrote /u/rms/gnu/gnu.tasks
  288. @end example
  289. @noindent
  290. If the selected buffer is not modified (no changes have been made in it
  291. since the buffer was created or last saved), Emacs does not save it
  292. because it would have no effect. Instead, @kbd{C-x C-s} prints a message
  293. in the echo area saying:
  294. @example
  295. (No changes need to be saved)
  296. @end example
  297. @kindex C-x s
  298. @findex save-some-buffers
  299. The command @kbd{C-x s} (@code{save-some-buffers}) can save any or all
  300. modified buffers. First it asks, for each modified buffer, whether to
  301. save it. The questions should be answered with @kbd{y} or @kbd{n}.
  302. @kbd{C-x C-c}, the key that kills Emacs, invokes
  303. @code{save-some-buffers} and therefore asks the same questions.
  304. @kindex M-~
  305. @findex not-modified
  306. If you have changed a buffer and do not want the changes to be saved,
  307. you should take some action to prevent it. Otherwise, you are liable to
  308. save it by mistake each time you use @code{save-some-buffers} or a
  309. related command. One thing you can do is type @kbd{M-~}
  310. (@code{not-modified}), which removes the indication that the buffer
  311. is modified. If you do this, none of the save commands will believe
  312. that the buffer needs to be saved. (@samp{~} is often used as a
  313. mathematical symbol for `not'; thus @kbd{Meta-~} is `not', metafied.)
  314. You could also use @code{set-visited-file-name} (see below) to mark the
  315. buffer as visiting a different file name, not in use for
  316. anything important.
  317. You can also undo all the changes made since the file was visited or
  318. saved, by reading the text from the file again. This is called
  319. @dfn{reverting}. @xref{Reverting}. Alternatively, you can undo all the
  320. changes by repeating the undo command @kbd{C-x u}; but this only works
  321. if you have not made more changes than the undo mechanism can remember.
  322. @findex set-visited-file-name
  323. @kbd{M-x set-visited-file-name} alters the name of the file that the
  324. current buffer is visiting. It prompts you for the new file name in the
  325. minibuffer. You can also use @code{set-visited-file-name} on a buffer
  326. that is not visiting a file. The buffer's name is changed to correspond
  327. to the file it is now visiting unless the new name is already used by a
  328. different buffer; in that case, the buffer name is not changed.
  329. @code{set-visited-file-name} does not save the buffer in the newly
  330. visited file; it just alters the records inside Emacs so that it will
  331. save the buffer in that file. It also marks the buffer as ``modified''
  332. so that @kbd{C-x C-s} @i{will} save.
  333. @kindex C-x C-w
  334. @findex write-file
  335. If you wish to mark a buffer as visiting a different file and save it
  336. right away, use @kbd{C-x C-w} (@code{write-file}). It is precisely
  337. equivalent to @code{set-visited-file-name} followed by @kbd{C-x C-s}.
  338. @kbd{C-x C-s} used on a buffer that is not visiting a file has the
  339. same effect as @kbd{C-x C-w}; that is, it reads a file name, marks the
  340. buffer as visiting that file, and saves it there. The default file name in
  341. a buffer that is not visiting a file is made by combining the buffer name
  342. with the buffer's default directory.
  343. If Emacs is about to save a file and sees that the date of the latest
  344. version on disk does not match what Emacs last read or wrote, Emacs
  345. notifies you of this fact, because it probably indicates a problem caused
  346. by simultaneous editing and requires your immediate attention.
  347. @xref{Interlocking,, Simultaneous Editing}.
  348. @vindex require-final-newline
  349. If the variable @code{require-final-newline} is non-@code{nil}, Emacs
  350. puts a newline at the end of any file that doesn't already end in one,
  351. every time a file is saved or written.
  352. @vindex write-file-hooks
  353. @vindex after-save-hook
  354. Use the hook variable @code{write-file-hooks} to implement other ways
  355. to write files, and specify things to be done before files are written. The
  356. value of this variable should be a list of Lisp functions. When a file
  357. is to be written, the functions in the list are called, one by one, with
  358. no arguments. If one of them returns a non-@code{nil} value, Emacs
  359. takes this to mean that the file has been written in some suitable
  360. fashion; the rest of the functions are not called, and normal writing is
  361. not done. Use the hook variable @code{after-save-hook} to list
  362. all the functions to be called after writing out a buffer to a file.
  363. @menu
  364. * Backup:: How Emacs saves the old version of your file.
  365. * Interlocking:: How Emacs protects against simultaneous editing
  366. of one file by two users.
  367. @end menu
  368. @node Backup, Interlocking, Saving, Saving
  369. @subsection Backup Files
  370. @cindex backup file
  371. @vindex make-backup-files
  372. Because Unix does not provide version numbers in file names, rewriting a
  373. file in Unix automatically destroys all record of what the file used to
  374. contain. Thus, saving a file from Emacs throws away the old contents of
  375. the file---or it would, except that Emacs carefully copies the old contents
  376. to another file, called the @dfn{backup} file, before actually saving.
  377. (Make sure that the variable @code{make-backup-files} is non-@code{nil}.
  378. Backup files are not written if this variable is @code{nil}).
  379. At your option, Emacs can keep either a single backup file or a series of
  380. numbered backup files for each file you edit.
  381. Emacs makes a backup for a file only the first time a file is saved
  382. from one buffer. No matter how many times you save a file, its backup file
  383. continues to contain the contents from before the file was visited.
  384. Normally this means that the backup file contains the contents from before
  385. the current editing session; however, if you kill the buffer and then visit
  386. the file again, a new backup file is made by the next save.
  387. @menu
  388. * Names: Backup Names. How backup files are named;
  389. Choosing single or numbered backup files.
  390. * Deletion: Backup Deletion. Emacs deletes excess numbered backups.
  391. * Copying: Backup Copying. Backups can be made by copying or renaming.
  392. @end menu
  393. @node Backup Names, Backup Deletion, Backup, Backup
  394. @subsubsection Single or Numbered Backups
  395. If you choose to have a single backup file (the default),
  396. the backup file's name is constructed by appending @samp{~} to the
  397. file name being edited; thus, the backup file for @file{eval.c} is
  398. @file{eval.c~}.
  399. If you choose to have a series of numbered backup files, backup file
  400. names are made by appending @samp{.~}, the number, and another @samp{~} to
  401. the original file name. Thus, the backup files of @file{eval.c} would be
  402. called @file{eval.c.~1~}, @file{eval.c.~2~}, and so on, through names
  403. like @file{eval.c.~259~} and beyond.
  404. If protection stops you from writing backup files under the usual names,
  405. the backup file is written as @file{%backup%~} in your home directory.
  406. Only one such file can exist, so only the most recently made backup is
  407. available.
  408. @vindex version-control
  409. The choice of single backup or numbered backups is controlled by the
  410. variable @code{version-control}. Its possible values are:
  411. @table @code
  412. @item t
  413. Make numbered backups.
  414. @item nil
  415. Make numbered backups for files that have numbered backups already.
  416. Otherwise, make single backups.
  417. @item never
  418. Never make numbered backups; always make single backups.
  419. @end table
  420. @noindent
  421. @code{version-control} may be set locally in an individual buffer to
  422. control the making of backups for that buffer's file. For example,
  423. Rmail mode locally sets @code{version-control} to @code{never} to make sure
  424. that there is only one backup for an Rmail file. @xref{Locals}.
  425. @node Backup Deletion, Backup Copying, Backup Names, Backup
  426. @subsubsection Automatic Deletion of Backups
  427. @vindex kept-old-versions
  428. @vindex kept-new-versions
  429. To prevent unlimited consumption of disk space, Emacs can delete numbered
  430. backup versions automatically. Generally Emacs keeps the first few backups
  431. and the latest few backups, deleting any in between. This happens every
  432. time a new backup is made. The two variables that control the deletion are
  433. @code{kept-old-versions} and @code{kept-new-versions}. Their values are, respectively
  434. the number of oldest (lowest-numbered) backups to keep and the number of
  435. newest (highest-numbered) ones to keep, each time a new backup is made.
  436. The values are used just after a new backup version is made;
  437. that newly made backup is included in the count in @code{kept-new-versions}.
  438. By default, both variables are 2.
  439. @vindex delete-old-versions
  440. If @code{delete-old-versions} is non-@code{nil}, excess
  441. middle versions are deleted without notification. If it is @code{nil}, the
  442. default, you are asked whether the excess middle versions should
  443. really be deleted.
  444. You can also use Dired's @kbd{.} (Period) command to delete old versions.
  445. @xref{Dired}.
  446. @node Backup Copying, , Backup Deletion, Backup
  447. @subsubsection Copying vs.@: Renaming
  448. You can make backup files by copying the old file or by renaming it.
  449. This makes a difference when the old file has multiple names. If you
  450. rename the old file into the backup file, the alternate names
  451. become names for the backup file. If you copy the old file instead,
  452. the alternate names remain names for the file that you are editing,
  453. and the contents accessed by those names will be the new contents.
  454. How you make a backup file may also affect the file's owner
  455. and group. If you use copying, they do not change. If renaming is used,
  456. you become the file's owner, and the file's group becomes the default
  457. (different operating systems have different defaults for the group).
  458. Having the owner change is usually a good idea, because then the owner
  459. is always the person who last edited the file. Occasionally there is a
  460. file whose owner should not change. Since most files should change
  461. owners, it is a good idea to use local variable lists to set
  462. @code{backup-by-copying-when-mismatch} for the special cases where the
  463. owner should not change (@pxref{File Variables}).
  464. @vindex backup-by-copying
  465. @vindex backup-by-copying-when-linked
  466. @vindex backup-by-copying-when-mismatch
  467. Three variables control the choice of renaming or copying.
  468. Normally, renaming is done. If the variable @code{backup-by-copying} is
  469. non-@code{nil}, copying is used. Otherwise, if the variable
  470. @code{backup-by-copying-when-linked} is non-@code{nil}, copying is
  471. done for files that have multiple names, but renaming may still be done when
  472. the file being edited has only one name. If the variable
  473. @code{backup-by-copying-when-mismatch} is non-@code{nil}, copying is
  474. done if renaming would cause the file's owner or group to change. @refill
  475. @node Interlocking, , Backup, Saving
  476. @subsection Protection Against Simultaneous Editing
  477. @cindex file dates
  478. @cindex simultaneous editing
  479. Simultaneous editing occurs when two users visit the same file, both
  480. make changes, and both save their changes. If no one was informed that
  481. this was happening, and you saved first, you would later find that your
  482. changes were lost. On some systems, Emacs notices immediately when the
  483. second user starts to change a file already being edited, and issues a
  484. warning. When this is not possible, or if the second user has started
  485. to change the file despite the warning, Emacs checks when the file is
  486. saved, and issues a second warning when a user is about to overwrite a
  487. file containing another user's changes. If you are the user editing the
  488. file, you can take corrective action at this point and prevent actual
  489. loss of work.
  490. @findex ask-user-about-lock
  491. When you make the first modification in an Emacs buffer that is visiting
  492. a file, Emacs records that you have locked the file. (It does this by
  493. writing another file in a directory reserved for this purpose.) The lock
  494. is removed when you save the changes. The idea is that the file is locked
  495. whenever the buffer is modified. If you begin to modify the buffer while
  496. the visited file is locked by someone else, this constitutes a collision,
  497. and Emacs asks you what to do. It does this by calling the Lisp function
  498. @code{ask-user-about-lock}, which you can redefine to customize what it
  499. does. The standard definition of this function asks you a
  500. question and accepts three possible answers:
  501. @table @kbd
  502. @item s
  503. Steal the lock. Whoever was already changing the file loses the lock,
  504. and you get the lock.
  505. @item p
  506. Proceed. Go ahead and edit the file despite its being locked by someone else.
  507. @item q
  508. Quit. This causes an error (@code{file-locked}) and the modification you
  509. were trying to make in the buffer does not actually take place.
  510. @end table
  511. Note that locking works on the basis of a file name; if a file has
  512. multiple names, Emacs does not realize that the two names are the same file
  513. and cannot prevent two users from editing it simultaneously under different
  514. names. However, basing locking on names means that Emacs can interlock the
  515. editing of new files that do not really exist until they are saved.
  516. Some systems are not configured to allow Emacs to make locks. On
  517. these systems, Emacs cannot detect trouble in advance, but it can still
  518. detect it in time to prevent you from overwriting someone else's changes.
  519. Every time Emacs saves a buffer, it first checks the last-modification
  520. date of the existing file on disk to see that it has not changed since the
  521. file was last visited or saved. If the date does not match, it implies
  522. that changes were made in the file in some other way, and these changes are
  523. about to be lost if Emacs actually does save. To prevent this, Emacs
  524. prints a warning message and asks for confirmation before saving.
  525. Occasionally you will know why the file was changed and know that it does
  526. not matter; then you can answer @kbd{yes} and proceed. Otherwise, you should
  527. cancel the save with @kbd{C-g} and investigate the situation.
  528. The first thing you should do when notified that simultaneous editing
  529. has already taken place is to list the directory with @kbd{C-u C-x C-d}
  530. (@pxref{ListDir,,Directory Listing}). This will show the file's current
  531. author. You should attempt to contact that person and ask him not to
  532. continue editing. Often the next step is to save the contents of your
  533. Emacs buffer under a different name, and use @code{diff} to compare the
  534. two files.@refill
  535. Simultaneous editing checks are also made when you visit a file that
  536. is already visited with @kbd{C-x C-f} and when you start to modify a
  537. file. This is not strictly necessary, but it is useful to find out
  538. about such a problem as early as possible, when corrective action takes
  539. less work.
  540. @findex set-default-file-modes
  541. @cindex file protection
  542. Another way to protect your file is to set the read, write, and
  543. executable permissions for the file. Use the function
  544. @code{set-default-file-modes} to set the UNIX @code{umask} value to the
  545. @var{nmask} argument. The @code{umask} value is the default protection
  546. mode for new files.
  547. @node Reverting, Auto Save, Saving, Files
  548. @section Reverting a Buffer
  549. @findex revert-buffer
  550. @cindex drastic changes
  551. If you have made extensive changes to a file and then change your mind
  552. about them, you can get rid of all changes by reading in the previous
  553. version of the file. To do this, use @kbd{M-x revert-buffer}, which
  554. operates on the current buffer. Since reverting a buffer can result in
  555. very extensive changes, you must confirm it with @kbd{yes}.
  556. You may request that @code{revert-buffer} check for an auto-save file
  557. that is more recent than the visited file by providing a prefix
  558. argument. If a recent auto-save file exists, @code{revert-buffer}
  559. offers to read the auto-save file instead of the visited file
  560. (@pxref{Auto Save}). Emacs asks you about the auto-save file before the
  561. request for confirmation of the @kbd{revert-buffer} operation, and
  562. demands @kbd{y} or @kbd{n} as an answer. If you have started to type
  563. @kbd{yes} to confirm the revert operation, the @kbd{y} will answer the
  564. question about using the auto-save file, but the @kbd{es} will not be
  565. valid confirmation for the reversion. This gives you a chance to cancel
  566. the operation with @kbd{C-g} and try again with the answers you really
  567. intend.
  568. @code{revert-buffer} preserves the value of point (in characters from
  569. the beginning of the file). If the file was edited only slightly, you
  570. will be at approximately the same piece of text after reverting as
  571. before. If you have made more extensive changes, after reversion point
  572. may be in a totally different context than your last edits before
  573. reversion.
  574. A buffer reverted from its visited file is marked ``not modified'' until
  575. you make a change. The buffer's modes will also be recalculated, by
  576. @code{normal-mode}.
  577. Some kinds of buffers whose contents reflect data bases other than files,
  578. such as Dired buffers, can also be reverted. For them, reverting means
  579. refreshing their contents from the appropriate data. Buffers created
  580. randomly with @kbd{C-x b} cannot be reverted; @code{revert-buffer}
  581. reports an error when asked to do so.
  582. @node Auto Save, Version Control, Reverting, Files
  583. @section Auto-Saving: Protection Against Disasters
  584. @cindex Auto-Save mode
  585. @cindex crashes
  586. Emacs saves all the visited files from time to time (based on counting
  587. your keystrokes) without being asked. This is called @dfn{auto-saving}.
  588. It prevents you from losing more than a limited amount of work if the
  589. system crashes.
  590. When Emacs determines it is time for auto-saving, each buffer is
  591. considered and is auto-saved if auto-saving is turned on for it and it has
  592. changed since the last time it was auto-saved. If any auto-saving is
  593. done, the message @samp{Auto-saving...} is displayed in the echo area until
  594. auto-saving is finished. Errors occurring during auto-saving are caught
  595. so that they do not interfere with the execution of commands you have been
  596. typing.
  597. @menu
  598. * Files: Auto Save Files.
  599. * Control: Auto Save Control.
  600. * Recover:: Recovering text from auto-save files.
  601. @end menu
  602. @node Auto Save Files, Auto Save Control, Auto Save, Auto Save
  603. @subsection Auto-Save Files
  604. Auto-saving does not normally write to the files you visited, because
  605. it can be undesirable to save a program that is in an inconsistent
  606. state when you have made only half of a planned change. Instead, auto-saving
  607. is done in a different file called the @dfn{auto-save file}, and the
  608. visited file is changed only when you save explicitly, for example,
  609. with @kbd{C-x C-s}.
  610. Normally, the name of the auto-save file is generated by appending
  611. @samp{#} to the front and back of the visited file name. Thus, a buffer
  612. visiting file @file{foo.c} would be auto-saved in a file @file{#foo.c#}.
  613. Most buffers that are not visiting files are auto-saved only if you
  614. request it explicitly; when they are auto-saved, the auto-save file name
  615. is generated by appending @samp{#%} to the front and @samp{#} to the
  616. back of buffer name. For example, the @samp{*mail*} buffer in which you
  617. compose messages to be sent is auto-saved in a file named
  618. @file{#%*mail*#}. Names of auto-save files are generated this way
  619. unless you customize the functions @code{make-auto-save-file-name} and
  620. @code{auto-save-file-name-p} to do something different. The file name
  621. to be used for auto-saving a buffer is calculated at the time auto-saving is
  622. turned on in that buffer.
  623. @vindex auto-save-visited-file-name
  624. If you want auto-saving to be done in the visited file, set the variable
  625. @code{auto-save-visited-file-name} to be non-@code{nil}. In this mode,
  626. there is really no difference between auto-saving and explicit saving.
  627. @vindex delete-auto-save-files
  628. Emacs deletes a buffer's auto-save file when you explicitly save the
  629. buffer. To inhibit the deletion, set the variable
  630. @code{delete-auto-save-files} to @code{nil}. Changing the visited file
  631. name with @kbd{C-x C-w} or @code{set-visited-file-name} renames any
  632. auto-save file to correspond to the new visited name.
  633. @node Auto Save Control, Recover, Auto Save Files, Auto Save
  634. @subsection Controlling Auto-Saving
  635. @vindex auto-save-default
  636. @findex auto-save-mode
  637. Each time you visit a file, auto-saving is turned on for that file's
  638. buffer if the variable @code{auto-save-default} is non-@code{nil} (but
  639. not in batch mode; @pxref{Entering Emacs}). The default for this
  640. variable is @code{t}, so Emacs auto-saves buffers that visit files by
  641. default. You can use the command @kbd{M-x auto-save-mode} to turn
  642. auto-saving for a buffer on or off. Like other minor mode commands,
  643. @kbd{M-x auto-save-mode} turns auto-saving on with a positive argument,
  644. off with a zero or negative argument; with no argument, it toggles.
  645. @vindex auto-save-interval
  646. @findex do-auto-save
  647. Emacs performs auto-saving periodically based on counting how many
  648. characters you have typed since the last time auto-saving happened. The
  649. variable @code{auto-save-interval} specifies the number of characters
  650. between auto-saves. By default, it is 300. Emacs also auto-saves
  651. whenever you call the function @code{do-auto-save}.
  652. Emacs also does auto-saving whenever it gets a fatal error. This
  653. includes killing the Emacs job with a shell command such as @code{kill
  654. -emacs}, or disconnecting a phone line or network connection.
  655. @vindex auto-save-timeout
  656. You can set the number of seconds of idle time before an auto-save is
  657. done. Setting the value of the variable @code{auto-save-timeout} to zero or
  658. @code{nil} will disable auto-saving due to idleness.
  659. The actual amount of idle time between auto-saves is logarithmically
  660. related to the size of the current buffer. This variable is the number
  661. of seconds after which an auto-save will happen when the current buffer
  662. is 50k or less; the timeout will be 2 1/4 times this in a 200k buffer, 3
  663. 3/4 times this in a 1000k buffer, and 4 1/2 times this in a 2000k
  664. buffer.
  665. For this variable to have any effect, you must do @code{(require 'timer)}.
  666. @node Recover, , Auto Save Control, Auto Save
  667. @subsection Recovering Data from Auto-Saves
  668. @findex recover-file
  669. If you want to use the contents of an auto-save file to recover from a
  670. loss of data, use the command @kbd{M-x recover-file @key{RET} @var{file}
  671. @key{RET}}. Emacs visits @var{file} and then (after your confirmation)
  672. restores the contents from the auto-save file @file{#@var{file}#}. You
  673. can then save the file with @kbd{C-x C-s} to put the recovered text into
  674. @var{file} itself. For example, to recover file @file{foo.c} from its
  675. auto-save file @file{#foo.c#}, do:@refill
  676. @example
  677. M-x recover-file @key{RET} foo.c @key{RET}
  678. C-x C-s
  679. @end example
  680. Before asking for confirmation, @kbd{M-x recover-file} displays a
  681. directory listing describing the specified file and the auto-save file,
  682. so you can compare their sizes and dates. If the auto-save file
  683. is older, @kbd{M-x recover-file} does not offer to read it.
  684. Auto-saving is disabled by @kbd{M-x recover-file} because using
  685. this command implies that the auto-save file contains valuable data
  686. from a past session. If you save the data in the visited file and
  687. then go on to make new changes, turn auto-saving back on
  688. with @kbd{M-x auto-save-mode}.
  689. @node Version Control, ListDir, Auto Save, Files
  690. @section Version Control
  691. @cindex version control
  692. @dfn{Version control systems} are packages that can record multiple
  693. versions of a source file, usually storing the unchanged parts of the
  694. file just once. Version control systems also record history information
  695. such as the creation time of each version, who created it, and a
  696. description of what was changed in that version.
  697. The GNU project recommends the version control system known as RCS,
  698. which is free software and available from the Free Software Foundation.
  699. Emacs supports use of either RCS or SCCS (a proprietary, but widely
  700. used, version control system that is not quite as powerful as RCS)
  701. through a facility called VC. The same Emacs commands work with either
  702. RCS or SCCS, so you hardly have to know which one of them you are
  703. using.
  704. @menu
  705. * Concepts of VC:: Basic version control information;
  706. checking files in and out.
  707. * Editing with VC:: Commands for editing a file maintained
  708. with version control.
  709. * Variables for Check-in/out:: Variables that affect the commands used
  710. to check files in or out.
  711. * Log Entries:: Logging your changes.
  712. * Change Logs and VC:: Generating a change log file from log
  713. entries.
  714. * Old Versions:: Examining and comparing old versions.
  715. * VC Status:: Commands to view the VC status of files and
  716. look at log entries.
  717. * Renaming and VC:: A command to rename both the source and
  718. master file correctly.
  719. * Snapshots:: How to make and use snapshots, a set of
  720. file versions that can be treated as a unit.
  721. * Version Headers:: Inserting version control headers into
  722. working files.
  723. @end menu
  724. @node Concepts of VC, Editing with VC, Version Control, Version Control
  725. @subsection Concepts of Version Control
  726. @cindex RCS
  727. @cindex SCCS
  728. @cindex master file
  729. @cindex registered file
  730. @cindex work file
  731. When a file is under version control, we also say that it is
  732. @dfn{registered} in the version control system. Each registered file
  733. has a corresponding @dfn{master file} which represents the file's
  734. present state plus its change history, so that you can reconstruct from
  735. it either the current version or any specified earlier version. Usually
  736. the master file also records a @dfn{log entry} for each version describing
  737. what was changed in that version.
  738. The file that is maintained under version control is sometimes called
  739. the @dfn{work file} corresponding to its master file.
  740. @cindex checking out files
  741. @cindex checking in files
  742. @cindex locking and version control
  743. To examine a file, you @dfn{check it out}. This extracts a version
  744. of the source file (typically, the most recent) from the master file.
  745. If you want to edit the file, you must check it out @dfn{locked}. Only
  746. one user can do this at a time for any given source file. (This kind
  747. of locking is completely unrelated to the locking that Emacs uses to
  748. detect simultaneous editing of a file.)
  749. When you are done with your editing, you must @dfn{check in} the new
  750. version. This records the new version in the master file, and unlocks
  751. the source file so that other people can lock it and thus modify it.
  752. Checkin and checkout are the basic operations of version control. You
  753. can do both of them with a single Emacs command: @w{@kbd{C-x C-q}}
  754. (@code{vc-toggle-read-only}).
  755. A @dfn{snapshot} is a coherent collection of versions of the various
  756. files that make up a program. @xref{Snapshots}.
  757. @node Editing with VC, Variables for Check-in/out, Concepts of VC, Version Control
  758. @subsection Editing with Version Control
  759. When you visit a file that is maintained using version control, the
  760. mode line displays @samp{RCS} or @samp{SCCS} to inform you that version
  761. control is in use, and also (in case you care) which low-level system
  762. the file is actually stored in. Normally, such a source file is
  763. read-only, and the mode line indicates this with @samp{%%}. With RCS,
  764. the mode line also indicates the number of the head version, which is
  765. normally also the version you are looking at.
  766. These are the commands for editing a file maintained with
  767. version control:
  768. @table @kbd
  769. @item C-x C-q
  770. Check the visited file in or out.
  771. @item C-x v u
  772. Revert the buffer and the file to the last checked in version.
  773. @item C-x v c
  774. Remove the last-entered change from the master for the visited file.
  775. This undoes your last check-in.
  776. @item C-x v i
  777. Register the visited file in version control.
  778. @end table
  779. @noindent
  780. (@kbd{C-x v} is the prefix key for version control commands; all of these
  781. commands except for @kbd{C-x C-q} start with @kbd{C-x v}.)
  782. @kindex C-x C-q @r{(version control)}
  783. When you want to modify a file maintained with version control, type
  784. @kbd{C-x C-q} (@code{vc-toggle-read-only}). This @dfn{checks out} the
  785. file, and tells RCS or SCCS to lock the file. This means making the
  786. file writable for you (but not for anyone else).
  787. @cindex log entry
  788. When you are finished editing the file, type @kbd{C-x C-q} again.
  789. When used on a file that is checked out, this command checks the file
  790. in. But check-in does not start immediately; first, you must enter the
  791. @dfn{log entry}---a description of the changes in the new version.
  792. @kbd{C-x C-q} pops up a buffer for you to enter this in. When you are
  793. finished typing in the log entry, type @kbd{C-c C-c} to terminate it; this is
  794. when actual check-in takes place.
  795. Once you have checked in your changes, the file is unlocked, so that
  796. other users can lock it and modify it.
  797. @vindex vc-make-backup-files
  798. Emacs does not save backup files for source files that are maintained
  799. with version control. If you want to make backup files despite version
  800. control, set the variable @code{vc-make-backup-files} to a
  801. non-@code{nil} value.
  802. @vindex vc-keep-workfiles
  803. Normally the work file exists all the time, whether it is locked or
  804. not. If you set @code{vc-keep-workfiles} to @code{nil}, then checking
  805. in a new version with @kbd{C-x C-q} deletes the work file; but any
  806. attempt to visit the file with Emacs creates it again.
  807. It is not impossible to lock a file that someone else has locked. If
  808. you try to check out a file that is locked, @kbd{C-x C-q} asks you
  809. whether you want to ``steal the lock.'' If you say yes, the file
  810. becomes locked by you, but a message is sent to the person who had
  811. formerly locked the file, to inform him of what has happened. The mode
  812. line indicates that a file is locked by someone else by displaying the
  813. login name of that person, before the version number.
  814. @kindex C-x v u
  815. @findex vc-revert-buffer
  816. If you want to discard your current set of changes and revert to the
  817. last version checked in, use @kbd{C-x v u} (@code{vc-revert-buffer}).
  818. This cancels your last check-out, leaving the file unlocked. If you want
  819. to make a different set of changes, you must first check the file out
  820. again. @kbd{C-x v u} requires confirmation, unless it sees that
  821. you haven't made any changes since the last checked-in version.
  822. @kbd{C-x v u} is also the command to use if you lock a file and then
  823. don't actually change it.
  824. @kindex C-x v c
  825. @findex vc-cancel-version
  826. You can cancel a change after checking it in, with @kbd{C-x v c}
  827. (@code{vc-cancel-version}). This command discards all record of the
  828. most recent checked in version, so be careful about using it. It
  829. requires confirmation with @kbd{yes}. By default, @kbd{C-x v c} reverts
  830. your workfile and buffer to the previous version (the one that precedes
  831. the version that is deleted), but you can prevent the reversion by
  832. giving the command a prefix argument. Then the buffer does not change.
  833. This command with a prefix argument is useful when you have checked in
  834. a change and then discover a trivial error in it; you can cancel the
  835. erroneous check-in, fix the error, and repeat the check-in.
  836. Be careful when invoking @kbd{C-x v c}, as it is easy to throw away a
  837. lot of work with it. To help you be careful, this command always
  838. requires confirmation with @samp{yes}.
  839. @kindex C-x v i
  840. @findex vc-register
  841. @vindex vc-default-back-end
  842. You can register the visited file for version control using
  843. @w{@kbd{C-x v i}} (@code{vc-register}). If the variable
  844. @code{vc-default-back-end} is non-@code{nil}, it specifies which
  845. version control system to use; otherwise, this uses RCS if it is
  846. installed on your system and SCCS if not. After @kbd{C-x v i},
  847. the file is unlocked and read-only. Type @kbd{C-x C-q} if you wish to
  848. edit it.
  849. By default, the initial version number is 1.1. If you want to use a
  850. different number, give @kbd{C-x v i} a prefix argument; then it reads
  851. the initial version number using the minibuffer.
  852. @vindex vc-initial-comment
  853. If @code{vc-initial-comment} is non-@code{nil}, @kbd{C-x v i} reads
  854. an initial comment (much like a log entry) to describe the purpose of
  855. this source file.
  856. @kindex C-u C-x v v
  857. @findex vc-next-action
  858. To specify the version number for a subsequent checkin, use the
  859. command @kbd{C-u C-x v v}. @kbd{C-x v v} (@code{vc-next-action}) is the
  860. command that @kbd{C-x C-q} uses to do the ``real work'' when the visited
  861. file uses version control. When used for checkin, and given a prefix
  862. argument, it reads the version number with the minibuffer.
  863. @node Variables for Check-in/out, Log Entries, Editing with VC, Version Control
  864. @subsection Variables Affecting Check-in and Check-out
  865. @c There is no need to tell users about vc-master-templates.
  866. @vindex vc-suppress-confirm
  867. If @code{vc-suppress-confirm} is non-@code{nil}, then @kbd{C-x C-q}
  868. and @kbd{C-x v i} can save the current buffer without asking, and
  869. @kbd{C-x v u} also operates without asking for confirmation.
  870. (This variable does not affect @kbd{C-x v c}; that is so drastic
  871. that it should always ask for confirmation.)
  872. @vindex vc-command-messages
  873. VC mode does much of its work by running the shell commands for RCS
  874. and SCCS. If @code{vc-command-messages} is non-@code{nil}, VC displays
  875. messages to indicate which shell commands it runs, and additional
  876. messages when the commands finish.
  877. Normally, VC assumes that it can deduce the locked/unlocked state of
  878. files by looking at the file permissions of the work file; this is
  879. fast. However, if the @file{RCS} or @file{SCCS} subdirectory is
  880. actually a symbolic link, then VC does not trust the file permissions to
  881. reflect this status.
  882. @vindex vc-mistrust-permissions
  883. You can specify the criterion for whether to trust the file permissions
  884. by setting the variable @code{vc-mistrust-permissions}. Its value may
  885. be @code{t} (always mistrust the file permissions and check the master
  886. file), @code{nil} (always trust the file permissions), or a function of
  887. one argument which makes the decision. The argument is the directory
  888. name of the @file{RCS} or @file{SCCS} subdirectory. A non-@code{nil}
  889. value from the function says to mistrust the file permissions.
  890. If you find that the file permissions of work files are changed
  891. erroneously, set @code{vc-mistrust-permissions} to @code{t}. Then VC
  892. always checks the master file to determine the file's status.
  893. @vindex vc-path
  894. You can specify additional directories to search for version control
  895. programs by setting the variable @code{vc-path}. These directories
  896. are searched before the usual search path. The proper result usually
  897. happens automatically.
  898. @node Log Entries, Change Logs and VC, Variables for Check-in/out, Version Control
  899. @subsection Log Entries
  900. When you're editing an initial comment or log entry for inclusion in a
  901. master file, finish your entry by typing @kbd{C-c C-c}.
  902. @table @kbd
  903. @item C-c C-c
  904. Finish the comment edit normally (@code{vc-finish-logentry}).
  905. This finishes check-in.
  906. @end table
  907. To abort check-in, just don't type @kbd{C-c C-c} in that buffer. You
  908. can switch buffers and do other editing. As long as you don't try to
  909. check in another file, the entry you were editing remains in its
  910. buffer, and you can go back to that buffer at any time to complete the
  911. check-in.
  912. If you change several source files for the same reason, it is often
  913. convenient to specify the same log entry for many of the files. To do
  914. this, use the history of previous log entries. The commands @kbd{M-n},
  915. @kbd{M-p}, @kbd{M-s} and @kbd{M-r} for doing this work just like the
  916. minibuffer history commands (except that these versions are used outside
  917. the minibuffer).
  918. @vindex vc-log-mode-hook
  919. Each time you check in a file, the log entry buffer is put into VC Log
  920. mode, which involves running two hooks: @code{text-mode-hook} and
  921. @code{vc-log-mode-hook}.
  922. @node Change Logs and VC, Old Versions, Log Entries, Version Control
  923. @subsection Change Logs and VC
  924. If you use RCS for a program and also maintain a change log file for
  925. it (@pxref{Change Log}), you can generate change log entries
  926. automatically from the version control log entries:
  927. @table @kbd
  928. @item C-x v a
  929. @kindex C-x v a
  930. @findex vc-update-change-log
  931. Visit the current directory's change log file and create new entries for
  932. versions checked in since the most recent entry in the change log file
  933. (@code{vc-update-change-log}).
  934. This command works with RCS only; it does not work with SCCS.
  935. @end table
  936. For example, suppose the first line of @file{ChangeLog} is dated 10
  937. April 1992, and that the only check-in since then was by Nathaniel
  938. Bowditch to @file{rcs2log} on 8 May 1992 with log text @samp{Ignore log
  939. messages that start with `#'.}. Then @kbd{C-x v a} visits
  940. @file{ChangeLog} and inserts text like this:
  941. @smallexample
  942. @group
  943. Fri May 8 21:45:00 1992 Nathaniel Bowditch (nat@@apn.org)
  944. * rcs2log: Ignore log messages that start with `#'.
  945. @end group
  946. @end smallexample
  947. @noindent
  948. You can then edit the new change log entry further as you wish.
  949. Normally, the log entry for file @file{foo} is displayed as @samp{*
  950. foo: @var{text of log entry}}. The @samp{:} after @file{foo} is omitted
  951. if the text of the log entry starts with @w{@samp{(@var{functionname}):
  952. }}. For example, if the log entry for @file{vc.el} is
  953. @samp{(vc-do-command): Check call-process status.}, then the text in
  954. @file{ChangeLog} looks like this:
  955. @smallexample
  956. @group
  957. Wed May 6 10:53:00 1992 Nathaniel Bowditch (nat@@apn.org)
  958. * vc.el (vc-do-command): Check call-process status.
  959. @end group
  960. @end smallexample
  961. When @kbd{C-x v a} adds several change log entries at once, it groups
  962. related log entries together if they all are checked in by the same
  963. author at nearly the same time. If the log entries for several such
  964. files all have the same text, it coalesces them into a single entry.
  965. For example, suppose the most recent checkins have the following log
  966. entries:
  967. @example
  968. @exdent For @file{vc.texinfo}:
  969. Fix expansion typos.
  970. @exdent For @file{vc.el}:
  971. Don't call expand-file-name.
  972. @exdent For @file{vc-hooks.el}:
  973. Don't call expand-file-name.
  974. @end example
  975. They appear like this in @file{ChangeLog}:
  976. @smallexample
  977. @group
  978. Wed Apr 1 08:57:59 1992 Nathaniel Bowditch (nat@@apn.org)
  979. * vc.texinfo: Fix expansion typos.
  980. * vc.el, vc-hooks.el: Don't call expand-file-name.
  981. @end group
  982. @end smallexample
  983. Normally, @kbd{C-x v a} separates log entries by a blank line, but you
  984. can mark several related log entries to be clumped together (without an
  985. intervening blank line) by starting the text of each related log entry
  986. with a label of the form @w{@samp{@{@var{clumpname}@} }}. The label
  987. itself is not copied to @file{ChangeLog}. For example, suppose the log
  988. entries are:
  989. @example
  990. @exdent For @file{vc.texinfo}:
  991. @{expand@} Fix expansion typos.
  992. @exdent For @file{vc.el}:
  993. @{expand@} Don't call expand-file-name.
  994. @exdent For @file{vc-hooks.el}:
  995. @{expand@} Don't call expand-file-name.
  996. @end example
  997. @noindent
  998. Then the text in @file{ChangeLog} looks like this:
  999. @smallexample
  1000. @group
  1001. Wed Apr 1 08:57:59 1992 Nathaniel Bowditch (nat@@apn.org)
  1002. * vc.texinfo: Fix expansion typos.
  1003. * vc.el, vc-hooks.el: Don't call expand-file-name.
  1004. @end group
  1005. @end smallexample
  1006. A log entry whose text begins with @samp{#} is not copied to
  1007. @file{ChangeLog}. For example, if you merely fix some misspellings in
  1008. comments, you can log the change with an entry beginning with @samp{#}
  1009. to avoid putting such trivia into @file{ChangeLog}.
  1010. @node Old Versions, VC Status, Change Logs and VC, Version Control
  1011. @subsection Examining And Comparing Old Versions
  1012. @table @kbd
  1013. @item C-x v ~ @var{version} @key{RET}
  1014. Examine version @var{version} of the visited file, in a buffer of its
  1015. own (@code{vc-version-other-window}).
  1016. @item C-x v =
  1017. Compare the current buffer contents with the latest checked-in version
  1018. of the file.
  1019. @item C-u C-x v = @var{file} @key{RET} @var{oldvers} @key{RET} @var{newvers} @key{RET}
  1020. Compare the specified two versions of @var{file}.
  1021. @end table
  1022. @findex vc-version-other-window
  1023. @kindex C-x v ~
  1024. You can examine any version of a file by first visiting it, and then
  1025. using @kbd{C-x v ~ @var{version} @key{RET}}
  1026. (@code{vc-version-other-window}). This puts the text of version
  1027. @var{version} in a file named @file{@var{filename}.~@var{version}~},
  1028. then visits it in a separate window.
  1029. @findex vc-diff
  1030. @kindex C-x v =
  1031. To compare two versions of a file, use the command @kbd{C-x v =}
  1032. (@code{vc-diff}).
  1033. Plain @kbd{C-x v =} compares the current buffer contents (saving them
  1034. in the file if necessary) with the last checked-in version of the file.
  1035. With a prefix argument, @kbd{C-x v =} reads a file name and two version
  1036. numbers, then compares those versions of the specified file.
  1037. If you supply a directory name instead of the name of a work file,
  1038. this command compares the two specified versions of all registered files
  1039. in that directory and its subdirectories. You can also specify a
  1040. snapshot name (@pxref{Snapshots}) instead of one or both version
  1041. numbers.
  1042. You can specify a checked-in version by its number; you can specify
  1043. the most recent checked-in version with an empty version number.
  1044. This command works by running the @code{vcdiff} utility, getting the
  1045. options from the variable @code{diff-switches}. It displays the output
  1046. in a special buffer in another window. Unlike the @kbd{M-x diff}
  1047. command, @kbd{C-x v =} does not try to find the changes in the old and
  1048. new versions. This is because one or both versions normally do not
  1049. exist as files. They exist only in the records of the master file.
  1050. @xref{Comparing Files}, for more information about @kbd{M-x diff}.
  1051. @node VC Status, Renaming and VC, Old Versions, Version Control
  1052. @subsection VC Status Commands
  1053. @kindex C-x v l
  1054. @findex vc-print-log
  1055. To view the detailed version control status and history of a file,
  1056. type @kbd{C-x v l} (@code{vc-print-log}). It displays the history of
  1057. changes to the current file, including the text of the log entries. The
  1058. output appears in a separate window.
  1059. @kindex C-x v d
  1060. @findex vc-directory
  1061. When you are working on a large program, it's often useful to find all
  1062. the files that are currently locked, or all the files maintained in
  1063. version control at all. You can use @kbd{C-x v d} (@code{vc-directory})
  1064. to show all the locked files in or beneath the current directory. This
  1065. includes all files that are locked by any user. @kbd{C-u C-x v d} lists
  1066. all files in or beneath the current directory that are maintained with
  1067. version control.
  1068. The list of files is displayed as a buffer that uses an augmented
  1069. Dired mode. The names of the users locking various files are shown (in
  1070. parentheses) in place of the owner and group. All the normal Dired
  1071. commands work in this buffer. Most interactive VC commands work also,
  1072. and apply to the file name on the current line.
  1073. The @kbd{C-x v v} command (@code{vc-next-action}), when used in the
  1074. augmented Dired buffer, operates on all the marked files (or the file on
  1075. the current line). If it operates on more than one file, it handles
  1076. each file according to its current state; thus, it may check out one
  1077. file and check in another (because it is already checked out). If it
  1078. has to check in any files, it reads a single log entry, then uses that
  1079. text for all the files being checked in. This can be convenient for
  1080. registering or checking in several files at once, as part of the same
  1081. change.
  1082. @node Renaming and VC, Snapshots, VC Status, Version Control
  1083. @subsection Renaming VC Work Files and Master Files
  1084. @findex vc-rename-file
  1085. When you rename a registered file, you must also rename its master
  1086. file correspondingly to get proper results. Use @code{vc-rename-file}
  1087. to rename the source file as you specify, and rename its master file
  1088. accordingly. It also updates any snapshots (@pxref{Snapshots}) that
  1089. mention the file, so that they use the new name; despite this, the
  1090. snapshot thus modified may not completely work (@pxref{Snapshot
  1091. Caveats}).
  1092. You cannot use @code{vc-rename-file} on a file that is locked by
  1093. someone else.
  1094. @node Snapshots, Version Headers, Renaming and VC, Version Control
  1095. @subsection Snapshots
  1096. @cindex snapshots and version control
  1097. A @dfn{snapshot} is a named set of file versions (one for each
  1098. registered file) that you can treat as a unit. One important kind of
  1099. snapshot is a @dfn{release}, a (theoretically) stable version of the
  1100. system that is ready for distribution to users.
  1101. @menu
  1102. * Making Snapshots:: The snapshot facilities.
  1103. * Snapshot Caveats:: Things to be careful of when using snapshots.
  1104. @end menu
  1105. @node Making Snapshots, Snapshot Caveats, Snapshots, Snapshots
  1106. @subsubsection Making and Using Snapshots
  1107. There are two basic commands for snapshots; one makes a
  1108. snapshot with a given name, the other retrieves a named snapshot.
  1109. @table @code
  1110. @kindex C-x v s
  1111. @findex vc-create-snapshot
  1112. @item C-x v s @var{name} @key{RET}
  1113. Define the last saved versions of every registered file in or under the
  1114. current directory as a snapshot named @var{name}
  1115. (@code{vc-create-snapshot}).
  1116. @kindex C-x v r
  1117. @findex vc-retrieve-snapshot
  1118. @item C-x v r @var{name} @key{RET}
  1119. Check out all registered files at or below the current directory level
  1120. using whatever versions correspond to the snapshot @var{name}
  1121. (@code{vc-retrieve-snapshot}).
  1122. This command reports an error if any files are locked at or below the
  1123. current directory, without changing anything; this is to avoid
  1124. overwriting work in progress.
  1125. @end table
  1126. A snapshot uses a very small amount of resources---just enough to record
  1127. the list of file names and which version belongs to the snapshot. Thus,
  1128. you need not hesitate to create snapshots whenever they are useful.
  1129. You can give a snapshot name as an argument to @kbd{C-x v =} or
  1130. @kbd{C-x v ~} (@pxref{Old Versions}). Thus, you can use it to compare a
  1131. snapshot against the current files, or two snapshots against each other,
  1132. or a snapshot against a named version.
  1133. @node Snapshot Caveats, , Making Snapshots, Snapshots
  1134. @subsubsection Snapshot Caveats
  1135. @cindex named configurations (RCS)
  1136. VC's snapshot facilities are modeled on RCS's named-configuration
  1137. support. They use RCS's native facilities for this, so under VC
  1138. snapshots made using RCS are visible even when you bypass VC.
  1139. @c worded verbosely to avoid overfull hbox.
  1140. For SCCS, VC implements snapshots itself. The files it uses contain
  1141. name/file/version-number triples. These snapshots are visible only
  1142. through VC.
  1143. A snapshot is a set of checked-in versions. So make sure that all the
  1144. files are checked in and not locked when you make a snapshot.
  1145. File renaming and deletion can create some difficulties with snapshots.
  1146. This is not a VC-specific problem, but a general design issue in version
  1147. control systems that no one has solved very well yet.
  1148. If you rename a registered file, you need to rename its master along
  1149. with it (the command @code{vc-rename-file} does this automatically). If
  1150. you are using SCCS, you must also update the records of the snapshot, to
  1151. mention the file by its new name (@code{vc-rename-file} does this,
  1152. too). An old snapshot that refers to a master file that no longer
  1153. exists under the recorded name is invalid; VC can no longer retrieve
  1154. it. It would be beyond the scope of this manual to explain enough about
  1155. RCS and SCCS to explain how to update the snapshots by hand.
  1156. Using @code{vc-rename-file} makes the snapshot remain valid for
  1157. retrieval, but it does not solve all problems. For example, some of the
  1158. files in the program probably refer to others by name. At the very
  1159. least, the makefile probably mentions the file that you renamed. If you
  1160. retrieve an old snapshot, the renamed file is retrieved under its new
  1161. name, which is not the name that the makefile expects. So the program
  1162. won't really work as retrieved.
  1163. @node Version Headers, , Snapshots, Version Control
  1164. @subsection Inserting Version Control Headers
  1165. Sometimes it is convenient to put version identification strings
  1166. directly into working files. Certain special strings called
  1167. @dfn{version headers} are replaced in each successive version by the
  1168. number of that version.
  1169. @kindex C-x v h
  1170. @findex vc-insert-headers
  1171. You can use the @kbd{C-x v h} command (@code{vc-insert-headers}) to
  1172. insert a suitable header string.
  1173. @table @kbd
  1174. @item C-x v h
  1175. Insert headers in a file for use with your version-control system.
  1176. @end table
  1177. @vindex vc-header-alist
  1178. The default header string is @samp{\$Id\$} for RCS and @samp{\%W\%}
  1179. for SCCS. (The actual strings inserted do not have the backslashes
  1180. in them. They were placed in the Info source file so that the
  1181. strings don't get interpreted as version-control headers when the
  1182. Info source files are maintained under version control.) You can
  1183. specify other headers to insert by setting the variable
  1184. @code{vc-header-alist}. Its value is a list of elements of the form
  1185. @code{(@var{program} . @var{string})} where @var{program} is @code{RCS}
  1186. or @code{SCCS} and @var{string} is the string to use.
  1187. Instead of a single string, you can specify a list of strings; then
  1188. each string in the list is inserted as a separate header on a line of
  1189. its own.
  1190. It is often necessary to use ``superfluous'' backslashes when writing
  1191. the strings that you put in this variable. This is to prevent the
  1192. string in the constant from being interpreted as a header itself if the
  1193. Emacs Lisp file containing it is maintained with version control.
  1194. @vindex vc-comment-alist
  1195. Each header is inserted surrounded by tabs, inside comment delimiters,
  1196. on a new line at the start of the buffer. Normally the ordinary comment
  1197. start and comment end strings of the current mode are used, but for
  1198. certain modes, there are special comment delimiters for this purpose;
  1199. the variable @code{vc-comment-alist} specifies them. Each element of
  1200. this list has the form @code{(@var{mode} @var{starter} @var{ender})}.
  1201. @vindex vc-static-header-alist
  1202. The variable @code{vc-static-header-alist} specifies further strings
  1203. to add based on the name of the buffer. Its value should be a list of
  1204. elements of the form @code{(@var{regexp} . @var{format})}. Whenever
  1205. @var{regexp} matches the buffer name, @var{format} is inserted as part
  1206. of the header. A header line is inserted for each element that matches
  1207. the buffer name, and for each string specified by
  1208. @code{vc-header-alist}. The header line is made by processing the
  1209. string from @code{vc-header-alist} with the format taken from the
  1210. element. The default value for @code{vc-static-header-alist} is:
  1211. @example
  1212. @group
  1213. (("\\.c$" .
  1214. "\n#ifndef lint\nstatic char vcid[] = \"\%s\";\n\
  1215. #endif /* lint */\n"))
  1216. @end group
  1217. @end example
  1218. @noindent
  1219. which specifies insertion of a string of this form:
  1220. @example
  1221. @group
  1222. #ifndef lint
  1223. static char vcid[] = "@var{string}";
  1224. #endif /* lint */
  1225. @end group
  1226. @end example
  1227. @node ListDir, Comparing Files, Version Control, Files
  1228. @section Listing a File Directory
  1229. @cindex file directory
  1230. @cindex directory listing
  1231. Files are organized by Unix into @dfn{directories}. A @dfn{directory
  1232. listing} is a list of all the files in a directory. Emacs provides
  1233. directory listings in brief format (file names only) and verbose format
  1234. (sizes, dates, and authors included).
  1235. @table @kbd
  1236. @item C-x C-d @var{dir-or-pattern}
  1237. Print a brief directory listing (@code{list-directory}).
  1238. @item C-u C-x C-d @var{dir-or-pattern}
  1239. Print a verbose directory listing.
  1240. @end table
  1241. @findex list-directory
  1242. @kindex C-x C-d
  1243. To print a directory listing, use @kbd{C-x C-d}
  1244. (@code{list-directory}). This command prompts in the minibuffer for a
  1245. file name which is either a directory to be listed or pattern
  1246. containing wildcards for the files to be listed. For example,
  1247. @example
  1248. C-x C-d /u2/emacs/etc @key{RET}
  1249. @end example
  1250. @noindent
  1251. lists all the files in directory @file{/u2/emacs/etc}. An example of
  1252. specifying a file name pattern is:
  1253. @example
  1254. C-x C-d /u2/emacs/src/*.c @key{RET}
  1255. @end example
  1256. Normally, @kbd{C-x C-d} prints a brief directory listing containing just
  1257. file names. A numeric argument (regardless of value) tells it to print a
  1258. verbose listing (like @code{ls -l}).
  1259. @vindex list-directory-brief-switches
  1260. @vindex list-directory-verbose-switches
  1261. Emacs obtains the text of a directory listing by running @code{ls} in
  1262. an inferior process. Two Emacs variables control the switches passed to
  1263. @code{ls}: @code{list-directory-brief-switches} is a string giving the
  1264. switches to use in brief listings (@code{"-CF"} by default).
  1265. @code{list-directory-verbose-switches} is a string giving the switches
  1266. to use in a verbose listing (@code{"-l"} by default).
  1267. The variable @code{directory-abbrev-alist} is an alist of abbreviations
  1268. for file directories. The list consists of elements of the form
  1269. @code{(FROM . TO)}, each meaning to replace @code{FROM} with @code{TO}
  1270. when it appears in a directory name. This replacement is done when
  1271. setting up the default directory of a newly visited file. Every @code{FROM}
  1272. string should start with `@samp{^}'.
  1273. Use this feature when you have directories which you normally refer to
  1274. via absolute symbolic links. Make @code{TO} the name of the link, and
  1275. @code{FROM} the name it is linked to.
  1276. @node Comparing Files, Dired, ListDir, Files
  1277. @section Comparing Files
  1278. @cindex comparing files
  1279. @findex diff
  1280. @vindex diff-switches
  1281. The command @kbd{M-x diff} compares two files, displaying the
  1282. differences in an Emacs buffer named @samp{*Diff*}. It works by running
  1283. the @code{diff} program, using options taken from the variable
  1284. @code{diff-switches}, whose value should be a string.
  1285. The buffer @samp{*Diff*} has Compilation mode as its major mode, so
  1286. you can use @kbd{C-x `} to visit successive changed locations in the two
  1287. source files. You can also move to a particular hunk of changes and
  1288. type @kbd{C-c C-c} to find the corresponding source location. You can
  1289. also use the other special commands of Compilation mode: @key{SPC} and
  1290. @key{DEL} for scrolling, and @kbd{M-p} and @kbd{M-n} for cursor motion.
  1291. @xref{Compilation}.
  1292. @findex diff-backup
  1293. The command @kbd{M-x diff-backup} compares a specified file with its most
  1294. recent backup. If you specify the name of a backup file,
  1295. @code{diff-backup} compares it with the source file that it is a backup
  1296. of.
  1297. @findex compare-windows
  1298. @cindex comparing files
  1299. The command @kbd{M-x compare-windows} compares the text in the current
  1300. window with that in the next window. Comparison starts at point in each
  1301. window. Point moves forward in each window, a character at a time in each
  1302. window, until the next characters in the two windows are different. Then
  1303. the command is finished. For more information about windows in Emacs,
  1304. @ref{Windows}.
  1305. @vindex compare-ignore-case
  1306. With a numeric argument, @code{compare-windows} ignores changes in
  1307. whitespace. If the variable @code{compare-ignore-case} is
  1308. non-@code{nil}, it ignores differences in case as well.
  1309. @node Dired, Misc File Ops, Comparing Files, Files
  1310. @section Dired, the Directory Editor
  1311. @cindex Dired
  1312. @cindex deletion (of files)
  1313. Dired makes it easy to delete or visit many of the files in a single
  1314. directory at once. It creates an Emacs buffer containing a listing of the
  1315. directory. You can use the normal Emacs commands to move around in this
  1316. buffer and special Dired commands to operate on the files.
  1317. @menu
  1318. * Enter: Dired Enter. How to invoke Dired.
  1319. * Edit: Dired Edit. Editing the Dired buffer.
  1320. * Deletion: Dired Deletion. Deleting files with Dired.
  1321. * Immed: Dired Immed. Other file operations through Dired.
  1322. @end menu
  1323. @node Dired Enter, Dired Edit, Dired, Dired
  1324. @subsection Entering Dired
  1325. @findex dired
  1326. @kindex C-x d
  1327. @vindex dired-listing-switches
  1328. To invoke dired, type @kbd{C-x d} or @kbd{M-x dired}. The command reads a
  1329. directory name or wildcard file name pattern as a minibuffer argument just
  1330. like the @code{list-directory} command, @kbd{C-x C-d}. Where @code{dired}
  1331. differs from @code{list-directory} is in naming the buffer after the
  1332. directory name or the wildcard pattern used for the listing, and putting
  1333. the buffer into Dired mode so that the special commands of Dired are
  1334. available in it. The variable @code{dired-listing-switches} is a string
  1335. used as an argument to @code{ls} in making the directory; this string
  1336. @i{must} contain @samp{-l}.
  1337. @findex dired-other-window
  1338. @kindex C-x 4 d
  1339. To display the Dired buffer in another window rather than in the selected
  1340. window, use @kbd{C-x 4 d} (@code{dired-other-window)} instead of @kbd{C-x d}.
  1341. @node Dired Edit, Dired Deletion, Dired Enter, Dired
  1342. @subsection Editing in Dired
  1343. Once the Dired buffer exists, you can switch freely between it and other
  1344. Emacs buffers. Whenever the Dired buffer is selected, certain special
  1345. commands are provided that operate on files that are listed. The Dired
  1346. buffer is ``read-only'', and inserting text in it is not useful, so
  1347. ordinary printing characters such as @kbd{d} and @kbd{x} are used for Dired
  1348. commands. Most Dired commands operate on the file described by the line
  1349. that point is on. Some commands perform operations immediately; others
  1350. ``flag'' a file to be operated on later.
  1351. Most Dired commands that operate on the current line's file also treat a
  1352. numeric argument as a repeat count, meaning to act on the files of the
  1353. next few lines. A negative argument means to operate on the files of the
  1354. preceding lines, and leave point on the first of those lines.
  1355. All the usual Emacs cursor motion commands are available in Dired
  1356. buffers. Some special purpose commands are also provided. The keys
  1357. @kbd{C-n} and @kbd{C-p} are redefined so that they try to position
  1358. the cursor at the beginning of the filename on the line, rather than
  1359. at the beginning of the line.
  1360. For extra convenience, @key{SPC} and @kbd{n} in Dired are equivalent to
  1361. @kbd{C-n}. @kbd{p} is equivalent to @kbd{C-p}. Moving by lines is done so
  1362. often in Dired that it deserves to be easy to type. @key{DEL} (move up and
  1363. unflag) is often useful simply for moving up.@refill
  1364. The @kbd{g} command in Dired runs @code{revert-buffer} to reinitialize
  1365. the buffer from the actual disk directory and show any changes made in the
  1366. directory by programs other than Dired. All deletion flags in the Dired
  1367. buffer are lost when this is done.
  1368. @node Dired Deletion, Dired Immed, Dired Edit, Dired
  1369. @subsection Deleting Files With Dired
  1370. The primary use of Dired is to flag files for deletion and then delete
  1371. them.
  1372. @table @kbd
  1373. @item d
  1374. Flag this file for deletion.
  1375. @item u
  1376. Remove deletion-flag on this line.
  1377. @item @key{DEL}
  1378. Remove deletion-flag on previous line, moving point to that line.
  1379. @item x
  1380. Delete the files that are flagged for deletion.
  1381. @item #
  1382. Flag all auto-save files (files whose names start and end with @samp{#})
  1383. for deletion (@pxref{Auto Save}).
  1384. @item ~
  1385. Flag all backup files (files whose names end with @samp{~}) for deletion
  1386. (@pxref{Backup}).
  1387. @item .@: @r{(Period)}
  1388. Flag excess numeric backup files for deletion. The oldest and newest
  1389. few backup files of any one file are exempt; the middle ones are flagged.
  1390. @end table
  1391. You can flag a file for deletion by moving to the line describing the
  1392. file and typing @kbd{d} or @kbd{C-d}. The deletion flag is visible as a
  1393. @samp{D} at the beginning of the line. Point is moved to the beginning of
  1394. the next line, so that repeated @kbd{d} commands flag successive files.
  1395. The files are flagged for deletion rather than deleted immediately to
  1396. avoid the danger of deleting a file accidentally. Until you direct Dired
  1397. to delete the flagged files, you can remove deletion flags using the
  1398. commands @kbd{u} and @key{DEL}. @kbd{u} works just like @kbd{d}, but
  1399. removes flags rather than making flags. @key{DEL} moves upward, removing
  1400. flags; it is like @kbd{u} with numeric argument automatically negated.
  1401. To delete the flagged files, type @kbd{x}. This command first displays a
  1402. list of all the file names flagged for deletion, and requests confirmation
  1403. with @kbd{yes}. Once you confirm, all the flagged files are deleted, and their
  1404. lines are deleted from the text of the Dired buffer. The shortened Dired
  1405. buffer remains selected. If you answer @kbd{no} or quit with @kbd{C-g}, you
  1406. return immediately to Dired, with the deletion flags still present and no
  1407. files actually deleted.
  1408. The @kbd{#}, @kbd{~}, and @kbd{.} commands flag many files for
  1409. deletion, based on their names. These commands are useful precisely
  1410. because they do not actually delete any files; you can remove the
  1411. deletion flags from any flagged files that you really wish to keep.@refill
  1412. @kbd{#} flags for deletion all files that appear to have been made by
  1413. auto-saving (that is, files whose names begin and end with @samp{#}).
  1414. @kbd{~} flags for deletion all files that appear to have been made as
  1415. backups for files that were edited (that is, files whose names end with
  1416. @samp{~}).
  1417. @vindex dired-kept-versions
  1418. @kbd{.} (Period) flags just some of the backup files for deletion: only
  1419. numeric backups that are not among the oldest few nor the newest few
  1420. backups of any one file. Normally @code{dired-kept-versions} (not
  1421. @code{kept-new-versions}; that applies only when saving) specifies the
  1422. number of newest versions of each file to keep, and
  1423. @code{kept-old-versions} specifies the number of oldest versions to keep.
  1424. Period with a positive numeric argument, as in @kbd{C-u 3 .}, specifies the
  1425. number of newest versions to keep, overriding @code{dired-kept-versions}.
  1426. A negative numeric argument overrides @code{kept-old-versions}, using minus
  1427. the value of the argument to specify the number of oldest versions of each
  1428. file to keep.@refill
  1429. @node Dired Immed, , Dired Deletion, Dired
  1430. @subsection Immediate File Operations in Dired
  1431. Some file operations in Dired take place immediately when they are
  1432. requested.
  1433. @table @kbd
  1434. @item C
  1435. Copies the file described on the current line. You must supply a file name
  1436. to copy to, using the minibuffer.
  1437. @item f
  1438. Visits the file described on the current line. It is just like typing
  1439. @kbd{C-x C-f} and supplying that file name. If the file on this line is a
  1440. subdirectory, @kbd{f} actually causes Dired to be invoked on that
  1441. subdirectory. @xref{Visiting}.
  1442. @item o
  1443. Like @kbd{f}, but uses another window to display the file's buffer. The
  1444. Dired buffer remains visible in the first window. This is like using
  1445. @kbd{C-x 4 C-f} to visit the file. @xref{Windows}.
  1446. @item R
  1447. Renames the file described on the current line. You must supply a file
  1448. name to rename to, using the minibuffer.
  1449. @item v
  1450. Views the file described on this line using @kbd{M-x view-file}. Viewing a
  1451. file is like visiting it, but is slanted toward moving around in the file
  1452. conveniently and does not allow changing the file. @xref{Misc File
  1453. Ops,View File}. Viewing a file that is a directory runs Dired on that
  1454. directory.@refill
  1455. @end table
  1456. @node Misc File Ops, , Dired, Files
  1457. @section Miscellaneous File Operations
  1458. Emacs has commands for performing many other operations on files.
  1459. All operate on one file; they do not accept wildcard file names.
  1460. @findex add-name-to-file
  1461. You can use the command @kbd{M-x add-name-to-file} to add a name to an
  1462. existing file without removing the old name. The new name must belong
  1463. on the file system that the file is on.
  1464. @findex append-to-file
  1465. @kbd{M-x append-to-file} adds the text of the region to the end of the
  1466. specified file.
  1467. @findex copy-file
  1468. @cindex copying files
  1469. @kbd{M-x copy-file} reads the file @var{old} and writes a new file
  1470. named @var{new} with the same contents. Confirmation is required if a
  1471. file named @var{new} already exists, because copying overwrites the old
  1472. contents of the file @var{new}.
  1473. @findex delete-file
  1474. @cindex deletion (of files)
  1475. @kbd{M-x delete-file} deletes a specified file, like the @code{rm}
  1476. command in the shell. If you are deleting many files in one directory, it
  1477. may be more convenient to use Dired (@pxref{Dired}).
  1478. @findex insert-file
  1479. @kbd{M-x insert-file} inserts a copy of the contents of a specified
  1480. file into the current buffer at point, leaving point unchanged before the
  1481. contents and the mark after them. @xref{Mark}.
  1482. @findex make-symbolic-link
  1483. @kbd{M-x make-symbolic-link} reads two file names @var{old} and
  1484. @var{linkname}, and then creates a symbolic link named @var{linkname}
  1485. and pointing at @var{old}. Future attempts to open file
  1486. @var{linkname} will then refer to the file named @var{old} at the time
  1487. the opening is done, or will result in an error if the name @var{old} is
  1488. not in use at that time. Confirmation is required if you create the
  1489. link while @var{linkname} is in use. Note that not all systems support
  1490. symbolic links.
  1491. @findex rename-file
  1492. @kbd{M-x rename-file} reads two file names @var{old} and @var{new} using
  1493. the minibuffer, then renames file @var{old} as @var{new}. If a file named
  1494. @var{new} already exists, you must confirm with @kbd{yes} or renaming is not
  1495. done; this is because renaming causes the previous meaning of the
  1496. name @var{new} to be lost. If @var{old} and @var{new} are on different
  1497. file systems, the file @var{old} is copied and deleted.
  1498. @findex view-file
  1499. @cindex viewing
  1500. @kbd{M-x view-file} allows you to scan or read a file by sequential
  1501. screenfuls. It reads a file name argument using the minibuffer. After
  1502. reading the file into an Emacs buffer, @code{view-file} reads and displays
  1503. one windowful. You can then type @key{SPC} to scroll forward one window,
  1504. or @key{DEL} to scroll backward. Various other commands are provided for
  1505. moving around in the file, but none for changing it; type @kbd{C-h} while
  1506. viewing a file for a list of them. Most commands are the default Emacs
  1507. cursor motion commands. To exit from viewing, type @kbd{C-c}.