Vim Tips Wiki
(Change <tt> to <code>, perhaps also minor tweak.)
(Added reference to recover plugin, and a trick how to move .swo swap file to .swp swapfile.)
Line 54: Line 54:
 
*{{help|CTRL-W_l}}
 
*{{help|CTRL-W_l}}
 
*{{help|:diffthis}}
 
*{{help|:diffthis}}
  +
  +
==See also==
 
*{{help|:DiffOrig}},
  +
*Christian Brabandt's {{script|id=3068|text=recovery}} plugin has a very good solution for recovering and diffing swapfile.
   
 
==Comments==
 
==Comments==
  +
After recovery a file from a swapfile, and deleting the swapfile you will usually use swapname .swo (or similar). When something will go wrong again, vim will not detect the .swo swapfile on startup. It is useful to restart vim, or do the following trick:
See also {{help|:DiffOrig}}.
 
  +
<pre>
  +
:set swf!|set swf!
  +
</pre>
  +
In this way vim will delete the .swo swapfile and make a new one ending with.swp (you can check this with :swapname command). Now you are completely safe.
   
 
----
 
----

Revision as of 16:48, 23 September 2012

Tip 1517 Printable Monobook Previous Next

created 2007 · complexity intermediate · author Richard Bronosky · version 7.0


When opening a file (in this example geocode.py) in Vim, I regularly encounter messages like:

E325: ATTENTION
Found a swap file by the name ".geocode.py.swp"
          owned by: rbronosky   dated: Fri Sep  7 17:17:37 2007
         file name: geocode.py
          modified: YES
         user name: rbronosky
        process ID: 6490
While opening file "geocode.py"
             dated: Fri Sep  7 17:17:04 2007
(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 geocode.py"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file ".geocode.py.swp"
    to avoid this message.
Swap file ".geocode.py.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:

This is the result of not properly closing an open buffer, usually because of a lost ssh connection. If there were unsaved changes, they can be recovered from this swap file. In order to know if this swap file is of value to me, I need to do some investigating. I have developed a system for resolving this quickly with as few keystrokes as possible.

  1. r # at the prompt hit "r" to recover the swap file
  2. :sav! /tmp/%
  3. :vs
  4. :diffthis
  5. CTRL-W_l
  6. :bp
  7. e # at the prompt hit "e" to edit anyway
  8. :diffthis

The result will be a vertically split screen with the swap file on the left and the regular file on the right. You will be in diff mode and if the files are identical they will both be folded into one line.

Sure, this would make a good script, but I am a big fan of "learn to do it by hand". That way you can do it on any system, and you can use each of the little steps to aid your daily vimming.

If you want to know more about the commands used, use :help, for example:

See also

Comments

After recovery a file from a swapfile, and deleting the swapfile you will usually use swapname .swo (or similar). When something will go wrong again, vim will not detect the .swo swapfile on startup. It is useful to restart vim, or do the following trick:

:set swf!|set swf!

In this way vim will delete the .swo swapfile and make a new one ending with.swp (you can check this with :swapname command). Now you are completely safe.


This is a useful trick, but it would be better if it was automated. Also in the situation where the recovered swapfile turns out to be identical to the real file, there is no need for the diffing. I use a shellscript to help deal with swapfiles, before starting Vim:

# Expects variables realfile, swapfile, recoveryfile.
vim -r "$swapfile" -c ":wq! $recoveryfile" && rm "$swapfile"
if cmp "$recoveryfile" "$realfile"
then rm "$recoveryfile"
else vimdiff "$recoveryfile" "$realfile"
fi