vim

Fork of vim, an acceptable text editor
git clone git://git.laack.co/vim.git
Log | Files | Refs | README | LICENSE

usr_11.txt (13027B)


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