Ubuntu – How to recover the original file from a .swp file


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

Best 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.