Feel free to close this as a dupe if you can find a question that deals with this case.

This is NOT about HOW to recover a .swp file in vi.

I'm not sure if I want to recover. I want to preview what I'll be changing if I do, and in a non destructive way.

Unfortunately typing vi .myfile.swp shows me what looks like binary gobblety gook.

To make my current state clear, I believe no one is currently editing the file. I happen to be looking at an overzealous copy of a dir that apparently happened while the file was open.

My current plan is to copy myfile to myfile-temp and then recover myfile. Then inspect for differences.

Is there a better way?

  • I'm also noticing that making a copy of the file and renaming the swp file to match the copy doesn't work. I can open the copy without vi/m offering to recover. Feb 12, 2016 at 16:00
  • The swap file has a struct block0 (C) that is written to the start of the swap. Among the properties of the struct is the file name b0_fname. If you open the swap file in Vim you should be able to locate this field at the top (byte 100+/-) What you need to do is either create a new directory where you copy the swap file and source, or you can edit the swap file IFF you give it a name of same length as original. The latter is more for hack and fun ... Recovering by swap is by default non-destructive for the original file as well as original swap. (Unless :w)
    – Runium
    Feb 12, 2016 at 16:44
  • @Sukminder Thanks! What's compelling me to :w is the need to diff. Feb 12, 2016 at 16:56
  • Then do as in linked answer. :sav another_file_name and then diff original_file_name and another_file_name ... Perhaps play around with some test files first ...
    – Runium
    Feb 12, 2016 at 17:01
  • Vi does not create such a file. It seems that you are talking about vim.
    – schily
    Feb 12, 2016 at 17:02

2 Answers 2


By far, my favorite vim plugin for handling this is Recover.vim. When you edit a file which has an associated .swp, it gives you the option of showing the diff between the two. This is invaluable. For example:

$ vi foo.py
Please choose:
D[i]ff, (O)pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort, (D)elete:
  • 1
    Rather than be required to install something I my not have permission to install I'd prefer an answer describes the simple work around you mentioned in the comments of the question. Feb 14, 2016 at 20:37

There are quite a few functions to help with this, see the relevant page on the Vim wiki.

The article is very long, so I won't copy it all here, but the basic premise is to save to another file and then diff the two.

  • The first entry looks promising. It seems to be saving the swp changes to a new file, so it is non destructive. And even includes doing a diff. However it does a poor job of explaining steps 1-8. Could I talk you into improving your answer? Feb 12, 2016 at 15:46
  • @MagicWindow I made an attempt at improving the solution from the vim wiki link. see my improvements here: vi.stackexchange.com/q/15584/1894 . Mar 15, 2018 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.