How can I recover the original file from a .swp file?

Solution 1:

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.

Solution 2:

You don't need to have the 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 to recover it 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 you're really out of luck.

Solution 3:

1 Open the swap file .file.swp in vim

2 :recover will recover your file