Cancel restore from swap file in vim

Before making a major change, I accidentally โ€œrestoredโ€ the file from the old and outdated swap file in vim. My changes seem to have disappeared. I tried to explore the undo tree, but large snippets of changes are still missing. In any case, can I cancel the restore operation, or am I doomed?

+7
vim
source share
3 answers

I just tried this myself (with constant undo support, Vim version 7.3.823). The old changes were still showing up (in :undolist , although I usually use a plugin like Gundo or Undotree to visualize it), but when I try to restore, I get

 E438: u_undo: line numbers wrong 

It seems that Vim cannot handle this situation. Please note that you have been warned; recovery clearly warns:

 Recovery completed. You should check if everything is OK. E308: Warning: Original file may have been changed 
+1
source share

The following works for me to restore a file, with canceling history:

  • Suppose you have my_file which has some persistent cancellation history + unsaved changes in .my_file.swp
  • Open the file with vim and press r to get the recovery version
  • Save the recovery version to a temporary location, for example: w / tmp /%
  • Close without saving (: q!). Open the file again and press d to delete the page file.
  • Optional: compare / tmp / my _file and my_file to make sure you want to restore the version
  • Manually add changes from / tmp / my _file (e.g. ggdG and: r / tmp /%)

It can probably be simplified, but it works for me.

PS: This does not give you a history of undoing changes between a file on disk and a restored version of a file that appears as one big change in history.

+7
source share

After accidentally restoring a file, you can simply enter: q! to exit vim without saving the restored changes - this will leave your source file intact and the page file where it is.

The next time you open the file, you will see the same request - press D to delete the swap file, or interrupt it and find it manually (and possibly delete any swap files in the same place)

+2
source share

All Articles