usr_11.txt 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. *usr_11.txt* Nvim
  2. VIM USER MANUAL - by Bram Moolenaar
  3. Recovering from a crash
  4. Did your computer crash? And you just spent hours editing? Don't panic! Vim
  5. stores enough information to be able to restore most of your work. This
  6. chapter shows you how to get your work back and explains how the swap file is
  7. used.
  8. |11.1| Basic recovery
  9. |11.2| Where is the swap file?
  10. |11.3| Crashed or not?
  11. |11.4| Further reading
  12. Next chapter: |usr_12.txt| Clever tricks
  13. Previous chapter: |usr_10.txt| Making big changes
  14. Table of contents: |usr_toc.txt|
  15. ==============================================================================
  16. *11.1* Basic recovery
  17. In most cases recovering a file is quite simple, assuming you know which file
  18. you were editing (and the harddisk is still working). Start Vim on the file,
  19. with the "-r" argument added: >
  20. vim -r help.txt
  21. Vim will read the swap file (used to store text you were editing) and may read
  22. bits and pieces of the original file. If Vim recovered your changes you will
  23. see these messages (with different file names, of course):
  24. Using swap file ".help.txt.swp" ~
  25. Original file "~/vim/runtime/doc/help.txt" ~
  26. Recovery completed. You should check if everything is OK. ~
  27. (You might want to write out this file under another name ~
  28. and run diff with the original file to check for changes) ~
  29. You may want to delete the .swp file now. ~
  30. To be on the safe side, write this file under another name: >
  31. :write help.txt.recovered
  32. Compare the file with the original file to check if you ended up with what you
  33. expected. Diff mode is very useful for this |08.7|. For example: >
  34. :write help.txt.recovered
  35. :edit #
  36. :diffsp help.txt
  37. Watch out for the original file to contain a more recent version (you saved
  38. the file just before the computer crashed). And check that no lines are
  39. missing (something went wrong that Vim could not recover).
  40. If Vim produces warning messages when recovering, read them carefully.
  41. This is rare though.
  42. If the recovery resulted in text that is exactly the same as the file
  43. contents, you will get this message:
  44. Using swap file ".help.txt.swp" ~
  45. Original file "~/vim/runtime/doc/help.txt" ~
  46. Recovery completed. Buffer contents equals file contents. ~
  47. You may want to delete the .swp file now. ~
  48. This usually happens if you already recovered your changes, or you wrote the
  49. file after making changes. It is safe to delete the swap file now.
  50. It is normal that the last few changes can not be recovered. Vim flushes the
  51. changes to disk when you don't type for about four seconds, or after typing
  52. about two hundred characters. This is set with the 'updatetime' and
  53. 'updatecount' options. Thus when Vim didn't get a chance to save itself when
  54. the system went down, the changes after the last flush will be lost.
  55. If you were editing without a file name, give an empty string as argument: >
  56. vim -r ""
  57. You must be in the right directory, otherwise Vim can't find the swap file.
  58. ==============================================================================
  59. *11.2* Where is the swap file?
  60. Vim can store the swap file in several places. Normally it is in the same
  61. directory as the original file. To find it, change to the directory of the
  62. file, and use: >
  63. vim -r
  64. Vim will list the swap files that it can find. It will also look in other
  65. directories where the swap file for files in the current directory may be
  66. located. It will not find swap files in any other directories though, it
  67. doesn't search the directory tree.
  68. The output could look like this:
  69. Swap files found: ~
  70. In current directory: ~
  71. 1. .main.c.swp ~
  72. owned by: mool dated: Tue May 29 21:00:25 2001 ~
  73. file name: ~mool/vim/vim6/src/main.c ~
  74. modified: YES ~
  75. user name: mool host name: masaka.moolenaar.net ~
  76. process ID: 12525 ~
  77. In directory ~/tmp: ~
  78. -- none -- ~
  79. In directory /var/tmp: ~
  80. -- none -- ~
  81. In directory /tmp: ~
  82. -- none -- ~
  83. If there are several swap files that look like they may be the one you want to
  84. use, a list is given of these swap files and you are requested to enter the
  85. number of the one you want to use. Carefully look at the dates to decide
  86. which one you want to use.
  87. In case you don't know which one to use, just try them one by one and check
  88. the resulting files if they are what you expected.
  89. USING A SPECIFIC SWAP FILE
  90. If you know which swap file needs to be used, you can recover by giving the
  91. swap file name. Vim will then find out the name of the original file from
  92. the swap file.
  93. Example: >
  94. vim -r .help.txt.swo
  95. This is also handy when the swap file is in another directory than expected.
  96. Vim recognizes files with the pattern *.s[uvw][a-z] as swap files.
  97. If this still does not work, see what file names Vim reports and rename the
  98. files accordingly. Check the 'directory' option to see where Vim may have
  99. put the swap file.
  100. Note:
  101. Vim tries to find the swap file by searching the directories in the
  102. 'dir' option, looking for files that match "filename.sw?". If
  103. wildcard expansion doesn't work (e.g., when the 'shell' option is
  104. invalid), Vim does a desperate try to find the file "filename.swp".
  105. If that fails too, you will have to give the name of the swapfile
  106. itself to be able to recover the file.
  107. ==============================================================================
  108. *11.3* Crashed or not? *ATTENTION* *E325*
  109. Vim tries to protect you from doing stupid things. Suppose you innocently
  110. start editing a file, expecting the contents of the file to show up. Instead,
  111. Vim produces a very long message:
  112. E325: ATTENTION ~
  113. Found a swap file by the name ".main.c.swp" ~
  114. owned by: mool dated: Tue May 29 21:09:28 2001 ~
  115. file name: ~mool/vim/vim6/src/main.c ~
  116. modified: no ~
  117. user name: mool host name: masaka.moolenaar.net ~
  118. process ID: 12559 (still running) ~
  119. While opening file "main.c" ~
  120. dated: Tue May 29 19:46:12 2001 ~
  121. ~
  122. (1) Another program may be editing the same file. ~
  123. If this is the case, be careful not to end up with two ~
  124. different instances of the same file when making changes. ~
  125. Quit, or continue with caution. ~
  126. ~
  127. (2) An edit session for this file crashed. ~
  128. If this is the case, use ":recover" or "vim -r main.c" ~
  129. to recover the changes (see ":help recovery"). ~
  130. If you did this already, delete the swap file ".main.c.swp" ~
  131. to avoid this message. ~
  132. You get this message, because, when starting to edit a file, Vim checks if a
  133. swap file already exists for that file. If there is one, there must be
  134. something wrong. It may be one of these two situations.
  135. 1. Another edit session is active on this file. Look in the message for the
  136. line with "process ID". It might look like this:
  137. process ID: 12559 (still running) ~
  138. The text "(still running)" indicates that the process editing this file
  139. runs on the same computer. When working on a non-Unix system you will not
  140. get this extra hint. When editing a file over a network, you may not see
  141. the hint, because the process might be running on another computer. In
  142. those two cases you must find out what the situation is yourself.
  143. If there is another Vim editing the same file, continuing to edit will
  144. result in two versions of the same file. The one that is written last will
  145. overwrite the other one, resulting in loss of changes. You better quit
  146. this Vim.
  147. 2. The swap file might be the result from a previous crash of Vim or the
  148. computer. Check the dates mentioned in the message. If the date of the
  149. swap file is newer than the file you were editing, and this line appears:
  150. modified: YES ~
  151. Then you very likely have a crashed edit session that is worth recovering.
  152. If the date of the file is newer than the date of the swap file, then
  153. either it was changed after the crash (perhaps you recovered it earlier,
  154. but didn't delete the swap file?), or else the file was saved before the
  155. crash but after the last write of the swap file (then you're lucky: you
  156. don't even need that old swap file). Vim will warn you for this with this
  157. extra line:
  158. NEWER than swap file! ~
  159. NOTE that in the following situation Vim knows the swap file is not useful and
  160. will automatically delete it:
  161. - The file is a valid swap file (Magic number is correct).
  162. - The flag that the file was modified is not set.
  163. - The process is not running.
  164. You can programmatically deal with this situation with the |FileChangedShell|
  165. autocommand event.
  166. UNREADABLE SWAP FILE ~
  167. Sometimes the line
  168. [cannot be read] ~
  169. will appear under the name of the swap file. This can be good or bad,
  170. depending on circumstances.
  171. It is good if a previous editing session crashed without having made any
  172. changes to the file. Then a directory listing of the swap file will show
  173. that it has zero bytes. You may delete it and proceed.
  174. It is slightly bad if you don't have read permission for the swap file. You
  175. may want to view the file read-only, or quit. On multi-user systems, if you
  176. yourself did the last changes under a different login name, a logout
  177. followed by a login under that other name might cure the "read error". Or
  178. else you might want to find out who last edited (or is editing) the file and
  179. have a talk with them.
  180. It is very bad if it means there is a physical read error on the disk
  181. containing the swap file. Fortunately, this almost never happens.
  182. You may want to view the file read-only at first (if you can), to see the
  183. extent of the changes that were "forgotten". If you are the one in charge of
  184. that file, be prepared to redo your last changes.
  185. WHAT TO DO? *swap-exists-choices*
  186. If dialogs are supported you will be asked to select one of six choices:
  187. Swap file ".main.c.swp" already exists! ~
  188. [O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete it: ~
  189. O Open the file readonly. Use this when you just want to view the file and
  190. don't need to recover it. You might want to use this when you know someone
  191. else is editing the file, but you just want to look in it and not make
  192. changes.
  193. E Edit the file anyway. Use this with caution! If the file is being edited
  194. in another Vim, you might end up with two versions of the file. Vim will
  195. try to warn you when this happens, but better be safe than sorry.
  196. R Recover the file from the swap file. Use this if you know that the swap
  197. file contains changes that you want to recover.
  198. Q Quit. This avoids starting to edit the file. Use this if there is another
  199. Vim editing the same file.
  200. When you just started Vim, this will exit Vim. When starting Vim with
  201. files in several windows, Vim quits only if there is a swap file for the
  202. first one. When using an edit command, the file will not be loaded and you
  203. are taken back to the previously edited file.
  204. A Abort. Like Quit, but also abort further commands. This is useful when
  205. loading a script that edits several files, such as a session with multiple
  206. windows.
  207. D Delete the swap file. Use this when you are sure you no longer need it.
  208. For example, when it doesn't contain changes, or when the file itself is
  209. newer than the swap file.
  210. On Unix this choice is only offered when the process that created the
  211. swap file does not appear to be running.
  212. If you do not get the dialog (you are running a version of Vim that does not
  213. support it), you will have to do it manually. To recover the file, use this
  214. command: >
  215. :recover
  216. Vim cannot always detect that a swap file already exists for a file. This is
  217. the case when the other edit session puts the swap files in another directory
  218. or when the path name for the file is different when editing it on different
  219. machines. Therefore, don't rely on Vim always warning you.
  220. If you really don't want to see this message, you can add the 'A' flag to the
  221. 'shortmess' option. But it's very unusual that you need this.
  222. For programmatic access to the swap file, see |swapinfo()|.
  223. ==============================================================================
  224. *11.4* Further reading
  225. |swap-file| An explanation about where the swap file will be created and
  226. what its name is.
  227. |:preserve| Manually flushing the swap file to disk.
  228. |:swapname| See the name of the swap file for the current file.
  229. 'updatecount' Number of key strokes after which the swap file is flushed to
  230. disk.
  231. 'updatetime' Timeout after which the swap file is flushed to disk.
  232. 'directory' List of directory names where to store the swap file.
  233. ==============================================================================
  234. Next chapter: |usr_12.txt| Clever tricks
  235. Copyright: see |manual-copyright| vim:tw=78:ts=8:noet:ft=help:norl: