blob: f6e63775139ebd7e2dad2ade6a6f4acba97d3716 [file] [log] [blame]
Bram Moolenaar76f3b1a2014-03-27 22:30:07 +01001*recover.txt* For Vim version 7.4. Last change: 2014 Mar 25
Bram Moolenaar071d4272004-06-13 20:20:40 +00002
3
4 VIM REFERENCE MANUAL by Bram Moolenaar
5
6
7Recovery after a crash *crash-recovery*
8
9You have spent several hours typing in that text that has to be finished
10next morning, and then disaster strikes: Your computer crashes.
11
12 DON'T PANIC!
13
14You can recover most of your changes from the files that Vim uses to store
15the contents of the file. Mostly you can recover your work with one command:
16 vim -r filename
17
181. The swap file |swap-file|
192. Recovery |recovery|
20
21==============================================================================
221. The swap file *swap-file*
23
24Vim stores the things you changed in a swap file. Using the original file
25you started from plus the swap file you can mostly recover your work.
26
27You can see the name of the current swap file being used with the command:
28
29 :sw[apname] *:sw* *:swapname*
30
31The name of the swap file is normally the same as the file you are editing,
32with the extension ".swp".
33- On Unix, a '.' is prepended to swap file names in the same directory as the
34 edited file. This avoids that the swap file shows up in a directory
35 listing.
36- On MS-DOS machines and when the 'shortname' option is on, any '.' in the
37 original file name is replaced with '_'.
38- If this file already exists (e.g., when you are recovering from a crash) a
39 warning is given and another extension is used, ".swo", ".swn", etc.
40- An existing file will never be overwritten.
41- The swap file is deleted as soon as Vim stops editing the file.
42
43Technical: The replacement of '.' with '_' is done to avoid problems with
44 MS-DOS compatible filesystems (e.g., crossdos, multidos). If Vim
45 is able to detect that the file is on an MS-DOS-like filesystem, a
46 flag is set that has the same effect as the 'shortname' option.
47 This flag is reset when you start editing another file.
48 *E326*
49 If the ".swp" file name already exists, the last character is
50 decremented until there is no file with that name or ".saa" is
51 reached. In the last case, no swap file is created.
52
53By setting the 'directory' option you can place the swap file in another place
54than where the edited file is.
55Advantages:
56- You will not pollute the directories with ".swp" files.
57- When the 'directory' is on another partition, reduce the risk of damaging
58 the file system where the file is (in a crash).
59Disadvantages:
60- You can get name collisions from files with the same name but in different
61 directories (although Vim tries to avoid that by comparing the path name).
62 This will result in bogus ATTENTION warning messages.
63- When you use your home directory, and somebody else tries to edit the same
Bram Moolenaar69a7cb42004-06-20 12:51:53 +000064 file, he will not see your swap file and will not get the ATTENTION warning
Bram Moolenaar071d4272004-06-13 20:20:40 +000065 message.
66On the Amiga you can also use a recoverable ram disk, but there is no 100%
67guarantee that this works. Putting swap files in a normal ram disk (like RAM:
68on the Amiga) or in a place that is cleared when rebooting (like /tmp on Unix)
69makes no sense, you will lose the swap file in a crash.
70
71If you want to put swap files in a fixed place, put a command resembling the
72following ones in your .vimrc:
73 :set dir=dh2:tmp (for Amiga)
74 :set dir=~/tmp (for Unix)
75 :set dir=c:\\tmp (for MS-DOS and Win32)
76This is also very handy when editing files on floppy. Of course you will have
77to create that "tmp" directory for this to work!
78
79For read-only files, a swap file is not used. Unless the file is big, causing
80the amount of memory used to be higher than given with 'maxmem' or
81'maxmemtot'. And when making a change to a read-only file, the swap file is
82created anyway.
83
Bram Moolenaar5803ae62014-03-23 16:04:02 +010084The 'swapfile' option can be reset to avoid creating a swapfile. And the
85|:noswapfile| modifier can be used to not create a swapfile for a new buffer.
86
Bram Moolenaar76f3b1a2014-03-27 22:30:07 +010087:noswap[file] {command} *:nos* *:noswapfile*
Bram Moolenaar5803ae62014-03-23 16:04:02 +010088 Execute {command}. If it contains a command that loads a new
89 buffer, it will be loaded without creating a swapfile and the
90 'swapfile' option will be reset. If a buffer already had a
91 swapfile it is not removed and 'swapfile' is not reset.
Bram Moolenaar071d4272004-06-13 20:20:40 +000092
93
94Detecting an existing swap file ~
95
96You can find this in the user manual, section |11.3|.
97
98
99Updating the swapfile ~
100
101The swap file is updated after typing 200 characters or when you have not
102typed anything for four seconds. This only happens if the buffer was
103changed, not when you only moved around. The reason why it is not kept up to
104date all the time is that this would slow down normal work too much. You can
105change the 200 character count with the 'updatecount' option. You can set
106the time with the 'updatetime' option. The time is given in milliseconds.
107After writing to the swap file Vim syncs the file to disk. This takes some
108time, especially on busy Unix systems. If you don't want this you can set the
109'swapsync' option to an empty string. The risk of losing work becomes bigger
110though. On some non-Unix systems (MS-DOS, Amiga) the swap file won't be
111written at all.
112
113If the writing to the swap file is not wanted, it can be switched off by
114setting the 'updatecount' option to 0. The same is done when starting Vim
115with the "-n" option. Writing can be switched back on by setting the
116'updatecount' option to non-zero. Swap files will be created for all buffers
117when doing this. But when setting 'updatecount' to zero, the existing swap
118files will not be removed, it will only affect files that will be opened
119after this.
120
121If you want to make sure that your changes are in the swap file use this
122command:
123
124 *:pre* *:preserve* *E313* *E314*
125:pre[serve] Write all text for all buffers into swap file. The
Bram Moolenaar4399ef42005-02-12 14:29:27 +0000126 original file is no longer needed for recovery.
127 This sets a flag in the current buffer. When the '&'
128 flag is present in 'cpoptions' the swap file will not
129 be deleted for this buffer when Vim exits and the
130 buffer is still loaded |cpo-&|.
131 {Vi: might also exit}
Bram Moolenaar071d4272004-06-13 20:20:40 +0000132
133A Vim swap file can be recognized by the first six characters: "b0VIM ".
134After that comes the version number, e.g., "3.0".
135
Bram Moolenaar2c7a29c2005-12-12 22:02:31 +0000136
137Links and symbolic links ~
138
139On Unix it is possible to have two names for the same file. This can be done
140with hard links and with symbolic links (symlinks).
141
142For hard links Vim does not know the other name of the file. Therefore, the
143name of the swapfile will be based on the name you used to edit the file.
144There is no check for editing the same file by the other name too, because Vim
145cannot find the other swapfile (except for searching all of your harddisk,
146which would be very slow).
147
148For symbolic links Vim resolves the links to find the name of the actual file.
149The swap file name is based on that name. Thus it doesn't matter by what name
150you edit the file, the swap file name will normally be the same. However,
151there are exceptions:
152- When the directory of the actual file is not writable the swapfile is put
153 elsewhere.
154- When the symbolic links somehow create a loop you get an *E773* error
155 message and the unmodified file name will be used. You won't be able to
156 save your file normally.
157
Bram Moolenaar071d4272004-06-13 20:20:40 +0000158==============================================================================
1592. Recovery *recovery* *E308* *E311*
160
161Basic file recovery is explained in the user manual: |usr_11.txt|.
162
163Another way to do recovery is to start Vim and use the ":recover" command.
164This is easy when you start Vim to edit a file and you get the "ATTENTION:
165Found a swap file ..." message. In this case the single command ":recover"
166will do the work. You can also give the name of the file or the swap file to
167the recover command:
168 *:rec* *:recover* *E305* *E306* *E307*
169:rec[over] [file] Try to recover [file] from the swap file. If [file]
170 is not given use the file name for the current
171 buffer. The current contents of the buffer are lost.
172 This command fails if the buffer was modified.
173
174:rec[over]! [file] Like ":recover", but any changes in the current
175 buffer are lost.
176
177 *E312* *E309* *E310*
178Vim has some intelligence about what to do if the swap file is corrupt in
179some way. If Vim has doubt about what it found, it will give an error
180message and insert lines with "???" in the text. If you see an error message
181while recovering, search in the file for "???" to see what is wrong. You may
182want to cut and paste to get the text you need.
183
184The most common remark is "???LINES MISSING". This means that Vim cannot read
185the text from the original file. This can happen if the system crashed and
186parts of the original file were not written to disk.
187
188Be sure that the recovery was successful before overwriting the original
189file or deleting the swap file. It is good practice to write the recovered
190file elsewhere and run 'diff' to find out if the changes you want are in the
Bram Moolenaar8f3f58f2010-01-06 20:52:26 +0100191recovered file. Or use |:DiffOrig|.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192
193Once you are sure the recovery is ok delete the swap file. Otherwise, you
194will continue to get warning messages that the ".swp" file already exists.
195
196{Vi: recovers in another way and sends mail if there is something to recover}
197
Bram Moolenaara8ffcbb2010-06-21 06:15:46 +0200198
199ENCRYPTION AND THE SWAP FILE *:recover-crypt*
200
201When the text file is encrypted the swap file is encrypted as well. This
202makes recovery a bit more complicated. When recovering from a swap file and
203encryption has been used, you will be asked to enter one or two crypt keys.
204
205If the text file does not exist you will only be asked to enter the crypt key
206for the swap file.
207
208If the text file does exist, it may be encrypted in a different way than the
209swap file. You will be asked for the crypt key twice:
210
211 Need encryption key for "/tmp/tt" ~
212 Enter encryption key: ****** ~
213 "/tmp/tt" [crypted] 23200L, 522129C ~
214 Using swap file "/tmp/.tt.swp" ~
215 Original file "/tmp/tt" ~
216 Swap file is encrypted: "/tmp/.tt.swp" ~
217 If you entered a new crypt key but did not write the text file, ~
218 enter the new crypt key. ~
219 If you wrote the text file after changing the crypt key press enter ~
220 to use the same key for text file and swap file ~
221 Enter encryption key: ~
222
223You can be in one of these two situations:
224
2251. The encryption key was not changed, or after changing the key the text file
226 was written. You will be prompted for the crypt key twice. The second
227 time you can simply press Enter. That means the same key is used for the
228 text file and the swap file.
2292. You entered a new encryption key, but did not save the text file. Vim will
230 then use the new key for the swap file, and the text file will still be
231 encrypted with the old key. At the second prompt enter the new key.
232
233Note that after recovery the key of the swap file will be used for the text
234file. Thus if you write the text file, you need to use that new key.
235
236
Bram Moolenaar071d4272004-06-13 20:20:40 +0000237 vim:tw=78:ts=8:ft=help:norl: