usr_11.txt ForVim version 9.2. Last change: 2026 Feb 14 VIM USER MANUALbyBramMoolenaar Recovering froma crashDid your computer crash? And you just spent hours editing? Don't panic! Vimstores enough information to be able to restore most of your work. Thischapter shows you how to get your work back and explains how the swap fileisused.11.1 Basicrecovery11.2 Whereis the swap file?11.3 Crashed or not?11.4 Further reading Next chapter:usr_12.txt Clever tricks Previous chapter:usr_10.txt Making big changesTable of contents:usr_toc.txt==============================================================================11.1 BasicrecoveryIn most cases recoveringa fileis quite simple, assuming you know which fileyou were editing (and the harddiskis still working). Start Vim on the file,with the "-r" argument added:vim -r help.txtVim will read the swap file (used to store text you were editing) and may readbits and pieces of the original file. If Vim recovered your changes you willsee thesemessages (with different file names, of course):Using swap file ".help.txt.swp"Original file "~/vim/runtime/doc/help.txt"Recovery completed. You should check if everything is OK.(You might want to write out this file under another nameand run diff with the original file to check for changes)You may want to delete the .swp file now.To be on the safe side, write this file under another name::write help.txt.recoveredCompare the file with the original file to check if you ended up with what youexpected. Vimdiffis very useful for this08.7. For example::write help.txt.recovered:edit #:diffsp help.txtWatch out for the original file to containa more recent version (you savedthe file just before the computer crashed). And check that no lines aremissing (something went wrong that Vim could not recover). If Vim produces warningmessages when recovering, read them carefully.Thisis rare though.If therecovery resulted in text thatis exactly the sameas the filecontents, you will get this message:Using swap file ".help.txt.swp"Original file "~/vim/runtime/doc/help.txt"Recovery completed. Buffer contents equals file contents.You may want to delete the .swp file now.This usually happens if you already recovered your changes, or you wrote thefile after making changes. Itis safe to delete the swap file now.Itis normal that the last few changes can not be recovered. Vim flushes thechanges to disk when you don't type for about four seconds, or after typingabout two hundred characters. Thisis set with the'updatetime' and'updatecount' options. Thus when Vim didn't geta chance to save itself whenthe system went down, the changes after the last flush will be lost.If you were editing withouta file name, give an emptystringas argument:vim -r ""Youmust be in the right directory, otherwise Vim can't find the swap file.==============================================================================11.2 Whereis the swap file?Vim can store the swap file in several places. Normallyitis in the samedirectoryas the original file. To find it, change to the directory of thefile, and use:vim -rVim willlist the swap files thatit can find. It will also look in otherdirectories where the swap file for files in the current directory may belocated. It will not find swap files in any other directories though,itdoesn't search the directory tree. The output could look like this:Swap files found: In current directory:1. .main.c.swp owned by: mool dated: Tue May 29 21:00:25 2001 file name: ~mool/vim/vim6/src/main.c modified: YES user name: mool host name: masaka.moolenaar.netprocess ID: 12525 In directory ~/tmp: -- none -- In directory /var/tmp: -- none -- In directory /tmp: -- none --If there are several swap files that look like they may be the one you want touse,alistis given of these swap files and you are requested to enter thenumber of the one you want to use. Carefully lookat the dates to decidewhich one you want to use. Incase you don't know which one to use, just try them one by one and checkthe resulting files if they are what you expected.USING A SPECIFIC SWAP FILEIf you know which swap file needs to be used, you can recover by giving theswap file name. Vim will then find out the name of the original file fromthe swap file.Example:vim -r .help.txt.swoThisis also handy when the swap fileis in another directory than expected.Vim recognizes files with thepattern "*.s[uvw][a-z]"as swap files.If this still does not work, see what file names Vim reports and rename thefiles accordingly. Check the'directory' option to see where Vim may haveput the swap file.Note:Vim tries to find the swap file by searching the directories in the'dir' option, looking for files that match "filename.sw?". Ifwildcard expansion doesn't work (e.g., when the'shell' optionisinvalid), Vim doesa desperate try to find the file "filename.swp".If that fails too, you will have to give the name of the swapfileitself to be able to recover the file.==============================================================================11.3 Crashed or not?ATTENTIONE325Vim tries to protect you from doing stupid things. Suppose you innocentlystart editinga file, expecting the contents of the file to show up. Instead,Vim producesa very long message:E325: ATTENTIONFound a swap file by the name ".main.c.swp" owned by: mool dated: Tue May 29 21:09:28 2001 file name: ~mool/vim/vim6/src/main.c modified: no user name: mool host name: masaka.moolenaar.netprocess ID: 12559 (still running)While opening file "main.c" dated: Tue May 29 19:46:12 2001(1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution.(2) An edit session for this file crashed. If this is the case, use ":recover" or "vim -r main.c" to recover the changes (see ":help recovery"). If you did this already, delete the swap file ".main.c.swp" to avoid this message.You get this message, because, whenstarting to edita file, Vim checks ifaswap file already exists for that file. If thereis one, theremust besomething wrong. It may be one of these two situations.1. Another edit sessionis active on this file. Look in the message for the line with "process ID". It might look like this:process ID: 12559 (still running) The text "(still running)" indicates that the process editing this file runs on the same computer. When working ona non-Unix system you will not get this extra hint. When editinga file overa network, you may not see the hint, because the process might be running on another computer. In those two cases youmust find out what the situationis yourself. If thereis another Vim editing the same file, continuing to edit will result in two versions of the same file. The one thatis written last will overwrite the other one, resulting in loss of changes. You better quit this Vim.2. The swap file might be the result froma previous crash of Vim or the computer. Check the dates mentioned in the message. If the date of the swap fileis newer than the file you were editing, and this line appears:modified: YES Then you very likely havea crashed edit session thatis worth recovering. If the date of the fileis newer than the date of the swap file, then eitherit was changed after the crash (perhaps you recoveredit earlier, but didn't delete the swap file?), or else the file was saved before the crash but after the last write of the swap file (then you're lucky: you don't even need that old swap file). Vim will warn you for this with this extra line: NEWER than swap file!NOTE that in the following situation Vim knows the swap fileis not useful andwill automatically delete it:- The fileisa valid swap file (Magic numberis correct).- The flag that the file was modifiedis not set.- The processis not running.You can programmatically deal with this situation with theFileChangedShellautocommand event.UNREADABLE SWAP FILESometimes the line[cannot be read]will appear under the name of the swap file. This can be good or bad,depending on circumstances.Itis good ifa previous editing session crashed without having made anychanges to the file. Thena directory listing of the swap file will showthatit has zero bytes. You may deleteit and proceed.Itis slightly bad if you don't have read permission for the swap file. Youmay want toview the file read-only, or quit. On multi-user systems, if youyourself did the last changes undera different login name,a logoutfollowed bya login under that other name might cure the "read error". Orelse you might want to find out who last edited (oris editing) the file andhavea talk with them.Itis very bad ifit means thereisa physical read error on the diskcontaining the swap file. Fortunately, this almost never happens.You may want toview the file read-onlyat first (if you can), to see theextent of the changes that were "forgotten". If you are the one in charge ofthat file, be prepared toredo your last changes.WHAT TO DO?swap-exists-choicesIf dialogs are supported you will be asked to select one of six choices: Swap file ".main.c.swp" already exists! [O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete it:O Open the file readonly. Use this when you just want toview the file and don't need to recover it. You might want to use this when you know someone elseis editing the file, but you just want to look init and not make changes.E Edit the file anyway. Use this with caution! If the fileis being edited in another Vim, you mightend up with two versions of the file. Vim will try to warn you when this happens, but better be safe than sorry.R Recover the file from the swap file. Use this if you know that the swap file contains changes that you want to recover.Q Quit. This avoidsstarting to edit the file. Use this if thereis another Vim editing the same file. When you just started Vim, this will exit Vim. Whenstarting Vim with files in several windows, Vim quits only if thereisa swap file for the first one. When using an edit command, the file will not be loaded and you are taken back to the previously edited file.A Abort. Like Quit, but also abort further commands. Thisis useful when loadingascript that edits several files, suchasa session with multiple windows.D Delete the swap file. Use this when you are sure you no longer need it. For example, whenit doesn't contain changes, or when the file itselfis newer than the swap file. OnUnix this choiceis only offered when the process that created the swap file does not appear to be running.If youdo not get thedialog (you are runninga version of Vim that does notsupport it), you will have todoit manually. To recover the file, use thiscommand::recoverVim cannot always detect thata swap file already exists fora file. Thisisthecase when the other edit session puts the swap files in another directoryor when the path name for the fileis different when editingit on differentmachines. Therefore, don't rely on Vim always warning you.If you really don't want to see this message, you can add the 'A' flag to the'shortmess' option. But it's very unusual that you need this.For remarks aboutencryption and the swap file, see:recover-crypt.For programmatic access to the swap file, seeswapinfo().==============================================================================11.4 Further readingswap-file An explanation about where the swap file will be created andwhat its name is.:preserve Manually flushing the swap file to disk.:swapname See the name of the swap file for the current file.'updatecount'Number of key strokes after which the swap fileis flushed todisk.'updatetime'Timeout after which the swap fileis flushed to disk.'swapsync'Whether the diskis synced when the swap fileis flushed.'directory'List of directory names where to store the swap file.'maxmem'Limit for memory usage beforewriting text to the swap file.'maxmemtot'Same, but for all files in total.==============================================================================Next chapter:usr_12.txt Clever tricksCopyright: seemanual-copyright vim:tw=78:ts=8:noet:ft=help:norl: