How to Recover Unsaved Changes from a Vim .swp File
Vim shows 'E325: swap file already exists' or you found a hidden .swp file. Here is how to recover your work.
Recover the file
- In a terminal, run
vim -r yourfile.txt(recover mode). Vim rebuilds the buffer from the.swp. - Check the contents are the version you want, then save with
:w(or:saveas newnameto be safe). - Once saved and verified, delete the swap file so the warning stops:
rm .yourfile.txt.swp(it is hidden — list withls -a).
The 'swap file already exists' prompt
When you reopen a file Vim may show Recover / Open Read-Only / Delete. Pick (R)ecover first, compare with :diffsplit yourfile.txt to see what changed, then delete the swap once you are satisfied. Only choose (D)elete if you know the swap holds nothing you need.
Where swap files live
By default the .swp sits beside the original as a hidden dotfile. If your Vim uses a central directory it may be in ~/.vim/swap/ or a path set by the directory option (:set directory? shows it).
FAQ
How do I recover a file from a .swp swap file?
Run 'vim -r filename' to recover the buffer, save it with :w, then delete the .swp file to clear the warning.
What does 'E325: ATTENTION swap file already exists' mean?
Vim found a leftover swap, usually from a crash or a second open session. Choose (R)ecover to get unsaved changes, compare, then delete the swap.
Where are Vim .swp files stored?
Normally as a hidden file beside the original; if the 'directory' option is set, in that folder (e.g. ~/.vim/swap/). Check with :set directory?.
Is it safe to delete a .swp file?
Yes, after you recover or confirm you don't need the unsaved changes. Deleting it clears the swap-exists warning.