This is related to, but not solved by the question “How can I recover the original file from a .swp file?”.

I had two newly-created files a.txt and b.txt open in buffers. The active buffer was b.txt, and I accidentally deleted a.txt, and then switched to the buffer a.txt. Unfortunately, I also have set autoread and set autowrite, so by reloading the buffer I emptied it.

I tried simply undoing the deletion in Vim by pressing u, but to no avail. Is there a way to recover the content of the deleted file from .a.txt.swp? The solution to the linked question doesn’t work here, presumably because of autowrite: the file is not restored.

In case it’s important, I also have set undofile. Also, since the file was newly created in this Vim session, the .swp file should, in principle, have contained the complete set of edits made to the file.

  • It is wrong to assume the full file contents exists in a .swp file. The unfortunate reality is that it does not. To slightly over-simplify, a Vim .swp file only contains the necessary information to "rebuild" the changes in the file since the last write. If the original file is gone, not enough information exists for the .swp file to be effective. – Heptite Aug 13 '14 at 2:11
  • @Heptite That’s what I would assume as well but it seems to be wrong: in the linked question, the .swp file is enough to retrieve the deleted file. The only difference is that I have auto{read,write} active. Furthermore, since I have infinite undo active, there is no reason in principle why my file shouldn’t be recoverable: the state of the file before the last operation (= the deletion) should have been persisted to the undo file. – Konrad Rudolph Aug 13 '14 at 6:41
  • There's only one answer that suggests that, and it contradicts itself. It needs to be reworded. And an undo file also does not contain the entire file contents. You may be able to recover some data by creating a blank file with the old filename and "recovering" it but you'll never get all of it. – Heptite Aug 13 '14 at 19:43
  • @Heptite Thanks for the explanation. Shame. Well, my system actually creates routine backups of all files, I was just unlucky enough to create, and then delete, the file just inside the one-hour window until the update would have been made. ;-) – Konrad Rudolph Aug 13 '14 at 20:10

You might be able to recover the content from the undofile. But you need to recompile Vim with the undorecover patch.

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.