6

I am editing in vi

I tried to copy a small part of my paper into a temp file

.,.+40!temp.  

But I wrote

.,.+40!.  

So this removed everything in the important things in the file (other than those 40 lines). Is there any way that I could retrieve the file that I had originally had (about 30 pages) or would I have to start from scratch?

X Tian
  • 10,413
  • 2
  • 33
  • 48
Guest
  • 61
  • 1
  • 2
    Is the `vi` session still open? – X Tian Oct 15 '15 at 17:45
  • Duh. I just received two comments: thank you very much! I lost my contact with the website. Yes, if I had still been on the editing page I would have no trouble. But the machine "decided" that I was finished editing this paper (so I wound up with the "bad one" – Guest Oct 15 '15 at 19:05

3 Answers3

4

I am not familiar with the command you've used, but if you have not exited from vi, the U command should undo the change.

Otherwise, in decreasing order of desirability:

  • In some cases a previous version of the file will be saved as filename.ext~ (probably it's not your case or you'd not even have posted, but still worth a try).

  • Previous versions of the file and its swap might be recoverable using extundelete, if your filesystem allows it (ext3 or ext4). If only the swap is recoverable, strings can be used to extract large swaths of text. You'll still need to reassemble them, but it's a far cry from rewriting all from scratch.

  • Otherwise, it could be possible to recover large parts of text if you remember some key words or phrases. This may be quite a lengthy operation. You can sift the whole partition to strings via dd and send the output via ssh to another machine, so as not to risk overwriting the local disk's free space (if you have another partition available, by all means use it).

  • Similarly, if the system was not rebooted, you have root access, and the /proc/kcore pseudo-file is of reasonable size, you can extract raw text from it with strings and dump it to a file. Then grep will be your friend. Possibly, other ways of accessing memory are available to you.

LSerni
  • 4,305
  • 14
  • 20
1

If you haven't saved anything yet, press the UNDO key "U".

You can also try and check the swap file.  It should be named yourfilename.swp or .yourfilename.swp, in the same directory or /var/tmp.  Be sure to use ls -la in case the name begins with ..

Make a copy and open it to check its contents.

0

If the session in vi is still open, the command :e! will restore the file immediately to the last saved version (if you just opened it and edited, it will recover the same version that you saw when you opened the file). U (undo) will work even if you saved changes, but you have to undo every action until you reach your desired version (I don't remember right now if there is a limit of undo actions... or how many they are)

dave_alcarin
  • 664
  • 1
  • 4
  • 14