I tried opening the .swp file using
cat but it displays junk.
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.
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.
$ 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.
After reading Vegar Westerlund and Heptite comments, I wanted to know at what point vim needs the original file (or a backup) to recover from a .swp file. Here's what did:
I opened a 975 lines file, edited it on line 949 (creating a
.swp file) and killed the process, then deleted the original.
$ vim Original_File asked if I wanted to recover from the .swp » > yes; only the first 68 lines and the last 34 lines (starting 8 lines above my edit) were actually recovered.
I then repeated the test with smaller files: From 20 up to 200 lines, the .swp file contained 100% of the original content. But at 300 lines, only the first 68 and the 18 last lines (starting 2 lines above my edit) were included in the .swp.
As a conclusion it's good to know that vim always save the file's "header" and "bottom" in the .swp files. Maybe there's a setting to control how much lines should a .swp contains?
I tried 2 options practically: 1.open original file and try recover option Didn’t recover file completely, it recovered partly though 2. I did: ls -a and opened .swp and selected recover option It recovered to a great extent, only few lines were missing and shown as ???LINES MISSING.
Note as early u do recovery more contents u would be able to recover.