*recover.txt*   For Vim version 5.5a.  Last change: 1999 May 05


		  VIM REFERENCE MANUAL    by Bram Moolenaar


Recovery after a crash					*crash-recovery*

You have spent several hours typing in that text that has to be finished
next 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 store
the contents of the file.  Mostly you can recover your work with one command:
	vim -r filename

1. The swap file	|swap-file|
2. Recovery		|recovery|

==============================================================================
1. The swap file					*swap-file*

Vim stores the things you changed in a swap file.  Using the original file
you 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* *:swapname*

The name of the swap file is normally the same as the file you are editing,
with the extension ".swp".
- On Unix, a '.' is prepended to swap file names in the same directory as the
  edited file.  This avoids that the swap file shows up in a directory
  listing.
- On MS-DOS machines and when the 'shortname' option is on, any '.' in the
  original file name is replaced with '_'.
- If this file already exists (e.g., when you are recovering from a crash) a
  warning is given and another extension is used, ".swo", ".swn", etc.
- An existing file will never be overwritten.
- The swap file is deleted as soon as Vim stops editing the file.

Technical: The replacement of '.' with '_' is done to avoid problems with
	   MS-DOS compatible filesystems (e.g., crossdos, multidos).  If Vim
	   is able to detect that the file is on an MS-DOS-like filesystem, a
	   flag is set that has the same effect as the 'shortname' option.
	   This flag is reset when you start editing another file.

	   If the ".swp" file name already exists, the last character is
	   decremented until there is no file with that name or ".swa" is
	   reached.  In the last case, no swap file is created.

By setting the 'directory' option you can place the swap file in another place
than 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 file is (in a 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 bogus ATTENTION warning messages.
- When you use your home directory, and somebody else tries to edit the same
  file, he will not see your swap file and will not get the ATTENTION waring
  message.
On the Amiga you can also use a recoverable ram disk, but there is no 100%
guarantee that this works.  Putting swap files in a normal ram disk (like RAM:
on the Amiga) or in a place that is cleared when rebooting (like /tmp on Unix)
makes no sense, you will lose the swap file in a crash.

If you want to put swap files in a fixed place, put a command resembling the
following ones in your .vimrc:
	:set dir=dh2:tmp	(for Amiga)
	:set dir=~/tmp		(for Unix)
	:set dir=c:\\tmp	(for MS-DOS and Win32)
This is also very handy when editing files on floppy.  Of course you will have
to create that "tmp" directory for this to work!

For read-only files, a swap file is not used.  Unless the file is big, causing
the amount of memory used to be higher than given with 'maxmem' or
'maxmemtot'.  And when making a change to a read-only file, the swap file is
created anyway.


Detecting an existing swap file ~
							*ATTENTION*
When starting to edit a file, Vim checks if a swap file already exists for
that file.  If there is one, you will get a message indicating that something
is wrong:

	ATTENTION
	Found a swap file by the name "../doc/help.txt.swp"
		     dated: Thu May 16 11:46:31 1996
		 file name: ~mool/vim/vim/doc/help.txt
		 host name: Kibaale
		 user name: mool
		process ID: 211 (still running)
	While opening file "../doc/help.txt"
		     dated: Wed May 15 21:38:40 1996

You are to take one of four actions:

1. Quit editing this file, because another edit session is active on this
   file.  Continuing to edit will result in two versions of the same file.
   The one that is written last will overwrite the other one, resulting in
   loss of changes.  The text "(still running)" indicates that the process
   editing this file runs on the same computer (Unix only).  When working over
   a network you will not see this message, because the process will be
   running on another computer.
2. Proceed with caution.  Making changes to the file is likely to cause
   trouble.  If you are only viewing a file it should be OK.
3. Recover a previously crashed edit session.  See below |recovery|.
4. Delete the swap file, if you don't want to recover the file.

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.

If dialogs are supported you will be asked to select one of five choices:

> Swap file already exists!
> [O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (D)elete it:

O  Open the file readonly.  Use this when you just want to view the file and
   don't need to recover it.
E  Edit the file anyway.  Use this with caution!
R  Recover the file from the swap file.  Use this if you know that the swap
   file contains changes that you want to recover.  See |recovery|.
Q  When just starting Vim: Quit Vim.  When starting Vim with files in several
   windows, Vim quits only if there is a swap file for the first one.  When
   using an edit command: Don't load the file, go back to the previously
   edited file.
D  Delete the swap file.  Use this when you are sure you no longer need it.
   On Unix this choice is only offered when the process that created the swap
   file does not seem to be running.

Vim cannot always detect that a swap file already exists for a file.  This is
the case when the other edit session puts the swap files in another
directory or when the path name for the file is different when editing it on
different machines.


Updating the swapfile ~

The swap file is updated after typing 200 characters or when you have not
typed anything for four seconds.  This only happens if the buffer was
changed, not when you only moved around.  The reason why it is not kept up to
date all the time is that this would slow down normal work too much.  You can
change the 200 character count with the 'updatecount' option.  You can set
the time with the 'updatetime' option.  The time is given in milliseconds.
After writing to the swap file Vim syncs the file to disk.  This takes some
time, especially on busy Unix systems.  If you don't want this you can set the
'swapsync' option to an empty string.  The risk of loosing work becomes bigger
though.  On some non-Unix systems (MS-DOS, Amiga) the swap file won't be
written at all.

If the writing to the swap file is not wanted, it can be switched off by
setting the 'updatecount' option to 0.  The same is done when starting Vim
with the "-n" option.  Writing can be switched back on by setting the
'updatecount' option to non-zero.  Swap files will be created for all buffers
when doing this.  But when setting 'updatecount' to zero, the existing swap
files will not be removed, it will only affect files that will be opened
after this.

If you want to make sure that your changes are in the swap file use this
command:

							*:pre* *:preserve*
:pre[serve]		Write all text for all buffers into swap file.  The
			original file is no longer needed for recovery.  {Vi:
			emergency exit}

A Vim swap file can be recognized by the first six characters: "b0VIM ".
After that comes the version number, e.g., "3.0".

==============================================================================
2. Recovery						*recovery*

In most cases recovery is quite easy: Start Vim on the same file you were
editing when the crash happened, with the "-r" option added.  Vim will read
the ".swp" file and may read bits and pieces of the original file.

Example:
>	vim -r help.txt

If you were editing without a file name, give an empty string as argument:
>	vim -r ""

If there are several swap files that look they may be the one you want to
use, a list is given of these swap files and you are requested to enter the
number of the one you want to use.  In case you don't know which one to use,
just try them one by one and check the resulting files if they are what you
expected.

If you know which swap file needs to be used, you can recover by giving the
swap file name.  Vim will then find out the name of the original file from
the swap file.

Example:
>	Vim -r .help.txt.swo

This is also handy when the swap file is in another directory than expected.
If this still does not work, see what file names Vim reports and rename the
files accordingly.  Check the 'directory' option to see where Vim may have
put 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?".  If wildcard
expansion doesn't work (e.g., when the 'shell' option is invalid), Vim does a
desparate try to find the file "filename.swp".  If that fails too, you will
have to give the name of the swapfile itself to be able to recover the file.

Another way to do recovery is to start Vim and use the ":recover" command.
This is easy when you start Vim to edit a file and you get the "ATTENTION:
Found a swap file ..." message.  In this case the single command ":recover"
will do the work.  You can also give the name of the file or the swap file to
the recover command:
							*:rec* *:recover*
:rec[over] [file]	Try to recover [file] from the swap file.  If [file]
			is not given use the file name for the current
			buffer.  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 current
			buffer are lost.

Vim has some intelligence about what to do if the swap file is corrupt in
some way.  If Vim has doubt about what it found, it will give an error
message and insert lines with "???" in the text.  If you see an error message
while recovering, search in the file for "???" to see what is wrong.  You may
want to cut and paste to get the text you need.

Be sure that the recovery was successful before overwriting the original
file or deleting the swap file.  It is good practice to write the recovered
file elsewhere and run 'diff' to find out if the changes you want are in the
recovered file.

Once you are sure the recovery is ok delete the swap file.  Otherwise, you
will continue to get warning messages that the ".swp" file already exists.

{Vi: recovers in another way and sends mail if there is something to recover}

 vim:tw=78:ts=8:sw=8:
