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.
Recover when the original file is gone
You can recover straight from the swap even if the original was deleted or renamed. Point Vim at the swap file itself:
vim -r .yourfile.txt.swpVim replays the swap into a fresh buffer. Immediately :saveas yourfile.txt to write it back out, because the recovered buffer is not yet attached to a real file on disk. If you are unsure which file a stray swap belongs to, open it with :recover and read the first lines — the content usually makes the owner obvious.
.swp, .swo, .swn — more than one swap
If a swap already exists when Vim needs another, it does not overwrite it — it steps the extension down: .swp → .swo → .swn, and so on. That means you can have several generations of unsaved changes side by side. List them with ls -a and recover each in turn (vim -r .yourfile.txt.swo) to find the newest good copy. The one Vim offers by default is usually the most recent, but when a crash loop created many, compare them with :diffsplit before deleting any.
Read a swap by hand if recovery fails
If vim -r reports the swap is unreadable or for a different Vim version, the swap still contains your text as mostly-plain blocks. Run strings .yourfile.txt.swp | less to pull the readable lines out — it will not be perfectly ordered, but it rescues the actual words when nothing else works. This is the last resort before accepting the changes are lost.
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). To keep stray swaps out of your project folders for good, set a central dir in ~/.vimrc: set directory=~/.vim/swap// (the trailing // makes Vim use the full path in the name so two files never collide).
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.
Can I recover if I already deleted the original file?
Yes. Run 'vim -r .yourfile.txt.swp' pointing at the swap itself, then ':saveas yourfile.txt' to write it back, because the recovered buffer is not yet attached to a file on disk.
What is the difference between .swp, .swo and .swn files?
They are successive swap generations. When a swap already exists Vim steps the extension down (.swp then .swo then .swn), so several can hold different unsaved versions at once.
vim -r shows nothing useful. What now?
Read the swap by hand: 'strings .yourfile.txt.swp | less' pulls the readable text out of the swap even when Vim's recovery refuses to run. It is unordered but rescues the actual words.
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.
Other file types explained
- What Is a .asv File? (Recover Your MATLAB Autosave)
- What Is a .ldb File? (Access Lock File That Will Not Delete)
- What Is a .dwl / .dwl2 File? (AutoCAD Lock File You Can Delete)
- What Is a .idlk File? (InDesign Lock File — Safe to Delete)
- What Is a .SWN File?
- What Is a .SWO File?
- What Is a .SWP File?
- What Is a .un~ File? (Vim Undo File)