recover.txt ForVim version 9.1. Last change: 2023 Apr 22VIM REFERENCE MANUAL by Bram MoolenaarRecovery aftera crashcrash-recoveryYou have spent several hours typing in that text that has to be finishednext morning, and then disaster strikes: Your computer crashes.DON'T PANIC!You can recover most of your changes from the files that Vim uses to storethe contents of the file. Mostly you can recover your work with one command:vim-r filename1. The swap fileswap-file2. Recoveryrecovery==============================================================================1. The swap fileswap-fileVim stores the things you changed ina swap file. Using the original fileyou started from plus the swap file you can mostly recover your work.You can see the name of the current swap file being used with the command::sw[apname]:sw:swapnameOr you can use theswapname() function, which also allows for seeing theswap file name of other buffers.The name of the swap fileis normally the sameas the file you are editing,with the extension ".swp".- On Unix,a '.'is prepended to swap file names in the same directoryas the edited file. This avoids that the swap file shows up ina directory listing.- OnMS-Windows machines and when the'shortname' optionis on, any '.' in the original file nameis replaced with '_'.- If this file already exists (e.g., when you are recovering froma crash)a warningis given and another extensionis used, ".swo", ".swn", etc.- An existing file will never be overwritten.- The swap fileis deletedas soonas Vim stops editing the file.Technical: The replacement of '.' with '_'is done to avoid problems withMS-DOS compatible filesystems (e.g., crossdos, multidos). If Vimis able to detect that the fileis on an MS-DOS-like filesystem,a flagis set that has the same effectas the'shortname' option. This flagis reset when you start editing another file.E326 If the ".swp" file name already exists, the last characteris decremented until thereis no file with that name or ".saa"is reached. In the last case, no swap fileis created.By setting the'directory' option you can place the swap file in another placethan where the edited file is.Advantages:- You will not pollute the directories with ".swp" files.- When the'directory'is on another partition, reduce the risk of damaging the file system where the fileis (ina crash).Disadvantages:- You can get name collisions from files with the same name but in different directories (although Vim tries to avoid that by comparing the path name). This will result in bogusATTENTION warning messages.- When you use yourhome directory, and somebody else tries to edit the same file, that user will not see your swap file and will not get theATTENTION warning message.On theAmiga you can also usea recoverable ram disk, but thereis no 100%guarantee that this works. Putting swap files ina normal ram disk (like RAM:on the Amiga) or ina place thatis cleared when rebooting (like /tmp on Unix)makes no sense, you will lose the swap file ina crash.If you want toput swap files ina fixed place,puta command resembling thefollowing ones in your .vimrc::set dir=dh2:tmp(for Amiga):set dir=~/tmp(for Unix):set dir=c:\\tmp(for Win32)Thisis also very handy when editing files on floppy. Of course you will haveto create that "tmp" directory for this to work!For read-only files,a swap fileis not used. Unless the fileis big, causingthe amount of memory used to be higher than given with'maxmem' or'maxmemtot'. And when makinga change toa read-only file, the swap fileiscreated anyway.The'swapfile' option can be reset to avoid creatinga swapfile. And the:noswapfile modifier can be used to not createa swapfile fora new buffer.:nos[wapfile]{command}:nos:noswapfileExecute{command}. Ifit containsa command that loadsa newbuffer,it will be loaded without creatinga swapfile and the'swapfile' option will be reset. Ifa buffer already hadaswapfileitis not removed and'swapfile'is not reset.Detecting an existing swap fileYou can find this in the user manual,section11.3.Updating the swapfileThe swap fileis updated after typing 200 characters or when you have nottyped anything for four seconds. This only happens if the buffer waschanged, not when you only moved around. The reason whyitis not kept up todate all the timeis that this would slow down normal work too much. You canchange the 200 charactercount with the'updatecount' option. You can setthe time with the'updatetime' option. The timeis given in milliseconds.Afterwriting to the swap file Vim syncs the file to disk. This takes sometime, especially on busyUnix systems. If you don't want this you can set the'swapsync' option to an empty string. The risk of losing work becomes biggerthough. On some non-Unix systems (MS-Windows, Amiga) the swap file won't bewrittenat all.If thewriting to the swap fileis not wanted,it can be switched off bysetting the'updatecount' option to 0. The sameis done whenstarting Vimwith the "-n" option. Writing can be switched back on by setting the'updatecount' option to non-zero. Swap files will be created for allbufferswhen doing this. But when setting'updatecount' to zero, the existing swapfiles will not be removed,it will only affect files that will be openedafter this.If you want to make sure that your changes are in the swap file use thiscommand::pre:preserveE313E314:pre[serve]Write all text for the current buffer into its swapfile. The original fileis no longer needed forrecovery. This setsa flag in the current buffer.When the '&' flagis present in'cpoptions' the swapfile will not be deleted for this buffer when Vimexits and the bufferis still loadedcpo-&.A Vim swap file can be recognized by the first six characters: "b0VIM ".After that comes the version number, e.g., "3.0".Links and symbolic linksOnUnixitis possible to have two names for the same file. This can be donewith hard links and with symbolic links (symlinks).For hard links Vim does not know the other name of the file. Therefore, thename of the swapfile will be based on the name you used to edit the file.Thereis no check for editing the same file by the other name too, because Vimcannot find the other swapfile (except for searching all of your harddisk,which would be very slow).For symbolic links Vim resolves the links to find the name of the actual file.The swap file nameis based on that name. Thusit doesn't matter by what nameyou edit the file, the swap file name will normally be the same. However,there are exceptions:- When the directory of the actual fileis not writable the swapfileisput elsewhere.- When the symbolic links somehow createa loop you get anE773 error message and the unmodified file name will be used. You won't be able to save your file normally.==============================================================================2. RecoveryrecoveryE308E311Basic filerecoveryis explained in the user manual:usr_11.txt.Another way todorecoveryis to start Vim and use the ":recover" command.Thisiseasy when you start Vim to edita file and you get the "ATTENTION:Founda swap file..." message. In thiscase the single command ":recover"willdo the work. You can also give the name of the file or the swap file tothe recover command::rec:recoverE305E306E307:rec[over][file]Try to recover[file] from the swap file. If[file]is not given use the file name for the currentbuffer. The current contents of the buffer are lost.This command fails if the buffer was modified.:rec[over]![file]Like ":recover", but any changes in the currentbuffer are lost.E312E309E310E1364Vim has some intelligence about what todo if the swap fileis corrupt insome way. If Vim has doubt about whatit found,it will give an errormessage andinsert lines with "???" in the text. If you see an error messagewhile recovering, search in the file for "???" to see whatis wrong. You maywant to cut and paste to get the text you need.The most common remarkis "???LINES MISSING". This means that Vim cannot readthe text from the original file. This can happen if the system crashed andparts of the original file were not written to disk.Be sure that therecovery was successful before overwriting the originalfile ordeleting the swap file. Itis good practice to write the recoveredfile elsewhere and run'diff' to find out if the changes you want are in therecovered file. Or use:DiffOrig.Once you are sure therecoveryis ok delete the swap file. Otherwise, youwill continue to get warningmessages that the ".swp" file already exists.ENCRYPTION AND THE SWAP FILE:recover-cryptWhen the text fileis encrypted the swap fileis encryptedas well. Thismakesrecoverya bit more complicated. When recovering froma swap file andencryption has been used, you will be asked to enter one or two crypt keys.If the text file does not exist you will only be asked to enter the crypt keyfor the swap file.If the text file does exist,it may be encrypted ina different way than theswap file. You will be asked for the crypt key twice:Need encryption key for "/tmp/tt"Enter encryption key: ******"/tmp/tt" [crypted] 23200L, 522129CUsing swap file "/tmp/.tt.swp"Original file "/tmp/tt"Swap file is encrypted: "/tmp/.tt.swp"If you entered a new crypt key but did not write the text file,enter the new crypt key.If you wrote the text file after changing the crypt key press enterto use the same key for text file and swap fileEnter encryption key:You can be in one of these two situations:1. Theencryption key was not changed, or afterchanging the key the text file was written. You will be prompted for the crypt key twice. The second time you can simply press Enter. That means the same keyis used for the text file and the swap file.2. You entereda newencryption key, but did not save the text file. Vim will then use the new key for the swap file, and the text file will still be encrypted with the old key. At the second prompt enter the new key.Note that afterrecovery the key of the swap file will be used for the textfile. Thus if you write the text file, you need to use that new key. vim:tw=78:ts=8:noet:ft=help:norl: