Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I tried opening the .swp file using vi and cat but it displays junk.

share|improve this question
    
Are you using vi or vim? –  frabjous Oct 28 '10 at 15:13

4 Answers 4

1 Open the swap file .file.swp in vim

2 :recover will recover your file

share|improve this answer
vim -r .swp

This goes into recovery mode and gives you more info. I think this is what you want.

.swX-files are left behind if a session with an unsaved file is killed/crashes/something else bad happens. They are also present during the time an unsaved buffer is open in a Vim session. vim -rreads these temporary files and recreates the content. After you've recovered it, just save it as usual, e.g. :w newfilename.

share|improve this answer
1  
Precisely what I was looking for. For anyone else, this is how you recover as-of-yet unsaved files. The above methods work fine if a file already exists, but you can't open a file with no name. –  Eric Hu Jan 18 '12 at 23:22

You don't need to have to original file to recover the .swp. Just open the file as if it exists. vim will look for the file with the .swp extension and offer to recover it.

e.g.:

$ ls -a
. .. .test.txt.swp
$ vim test.txt
[...]
Swap file ".test.txt.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort:

Just press r for recover and :wq the file

Edit: Note that the .swp file only contains the changes done to the file (see comment). This means that you will need to fetch a recent copy of the file from backup and then use vim to recover the latest changes. If you don't have a backup copy of the file your really out of luck.

share|improve this answer
    
You definitely have to have the original file, without it the data in the .swp file is useless. See ":help swap-file". There are situations where it may appear to work without the original file, but the .swp file only stores change information and requires some starting point to apply that information during recovery. –  Heptite Oct 28 '10 at 17:57
    
You are right, of course. I actually didn't know this about vim. But it is only logical as storing a complete copy of the original file would be a huge waste. Then the answer is: Recover your file from backup, then use vim to recover the latest modifications. –  Vegar Westerlund Nov 2 '10 at 9:27

Open the original file using vim, and choose the recovery option.

share|improve this answer
    
I don't have the original file.I just have it's '.swp' –  zarna Oct 28 '10 at 7:36
1  
That's less important than you think. Open it regardless. –  Ignacio Vazquez-Abrams Oct 28 '10 at 7:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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