Introduction
When Vim crashes or is killed unexpectedly, it leaves behind a .swp (swap) file. On the next edit attempt, Vim warns about the existing swap file. If recovery was interrupted or the swap file was not properly resolved, the error persists:
E325: ATTENTION
Found a swap file by the name ".myfile.txt.swp"
owned by: user dated: Mon Apr 08 14:30:00 2026
file name: ~user/myfile.txt
modified: YES
user name: user host name: myhost
process ID: 12345 (STILL RUNNING)Symptoms
- Vim shows "Found a swap file" warning every time you open a file
- Recovery attempt fails with "swap file already exists"
- Cannot save changes due to swap file conflict
- Multiple swap files (
.swo,.swn) from repeated interrupted recoveries - Error persists after deleting the
.swpfile because Vim recreates it
Common Causes
- Vim process killed (SSH disconnect, terminal close, OOM kill) while editing
- Recovery started but not completed (user pressed Quit instead of Recover)
- Multiple Vim instances editing the same file simultaneously
- Network file system (NFS) causing swap file state confusion
- Swap file not cleaned up after successful recovery
Step-by-Step Fix
- 1.Recover the file from the swap file:
- 2.```bash
- 3.vim -r myfile.txt
- 4.
` - 5.Inside Vim, review the recovered content. If it looks correct, save it:
- 6.```vim
- 7.:w myfile.txt.recovered
- 8.:q
- 9.
` - 10.Compare and merge the recovered file with the original:
- 11.```bash
- 12.diff myfile.txt myfile.txt.recovered
- 13.
` - 14.If the recovered version is better, replace the original:
- 15.```bash
- 16.mv myfile.txt.recovered myfile.txt
- 17.
` - 18.Delete the swap file after recovery:
- 19.```bash
- 20.rm .myfile.txt.swp
- 21.rm .myfile.txt.swo .myfile.txt.swn 2>/dev/null
- 22.
` - 23.If the process is still running, find and kill it first:
- 24.```bash
- 25.ps aux | grep vim | grep myfile.txt
- 26.kill -9 12345 # Replace with actual PID
- 27.
` - 28.Then proceed with recovery and swap file deletion.
- 29.Configure Vim to reduce swap file issues. Add to
~/.vimrc: - 30.```vim
- 31." Store all swap files in one directory
- 32.set directory=~/.vim/swap//
" Create the swap directory " Run: mkdir -p ~/.vim/swap ``` This keeps swap files out of your working directories and makes cleanup easier.
Prevention
- Always exit Vim properly with
:qor:wqinstead of closing the terminal - Use
tmuxorscreento protect Vim sessions from SSH disconnections - Configure a central swap directory to keep swap files organized
- Use
:set swapfile(default) to enable crash recovery - do not disable it - Before deleting a swap file, always verify the file content is correct
- Run
vim -rwithout arguments to list all available recovery files - Add swap file patterns to your
.gitignore:*.swp *.swo *.swn *~