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