# Fix Vim Swap File Exists Crash Recovery Error
You open a file in Vim and immediately see:
``` E325: ATTENTION Found a swap file by the name ".myfile.txt.swp" owned by: user dated: Thu Apr 08 09:15:33 2026 file name: ~user/documents/myfile.txt modified: YES user name: user host name: myserver process ID: 12345 While opening file "myfile.txt" dated: Thu Apr 08 10:30:00 2026
(1) Another program may be editing the same file. If this is the case, be careful not to end up with two different instances of the same file when making changes. Quit, or continue with caution. (2) An edit session for this file crashed. If this is the case, use ":recover" or "vim -r myfile.txt" to recover the changes (see ":help recovery"). If you did this already, delete the swap file ".myfile.txt.swp" to avoid this message. ```
This message appears when Vim finds a .swp file in the same directory as the file you are opening.
Understanding When Swap Files Are Created
Vim creates a swap file (.filename.swp) the moment you start editing a file. The swap file stores unsaved changes and serves two purposes:
- 1.Crash recovery: If Vim crashes or your SSH session drops, the swap file contains your unsaved work
- 2.Edit detection: If another Vim instance opens the same file, the swap file warns about concurrent editing
Scenario 1: A Previous Session Crashed
This is the most common case. Your SSH connection dropped, your terminal closed, or Vim segfaulted. Your changes are still in the swap file.
Recover the file:
vim -r myfile.txtOr from within Vim:
:recoverReview the recovered content carefully. If it looks correct, save it:
:w myfile.txtThen delete the swap file to prevent the warning next time:
rm .myfile.txt.swpScenario 2: Another Vim Instance Is Editing the File
You (or a colleague) have the file open in another Vim session. Check:
ps aux | grep vim | grep myfile.txt
fuser myfile.txtIf another Vim process holds the file open, either close that session or use a different approach (like vim -R for read-only viewing).
Scenario 3: Stale Swap File After Network Filesystem Issues
On NFS-mounted directories, swap files can persist after the editing process terminates normally. This happens when the NFS client does not properly clean up temporary files.
If you are certain no one is editing the file and recovery is not needed:
rm .myfile.txt.swpRecovering All Swap Files
To find and recover all unsaved files at once:
```bash # List all swap files in your home directory find ~ -name "*.swp" -type f 2>/dev/null
# Recover all of them vim -r ```
Vim will list all found swap files and let you recover them one by one.
Configuring Swap File Behavior
Change Swap File Location
By default, swap files are created in the same directory as the edited file. This clutters directories and can cause issues with version control. Move them to a central directory:
" In ~/.vimrc
set directory=~/.vim/swap//The double trailing slash tells Vim to use the full file path as the swap file name, preventing collisions when editing files with the same name in different directories.
Create the directory:
mkdir -p ~/.vim/swap
chmod 700 ~/.vim/swapDisable Swap Files (Not Recommended)
You can disable swap files entirely, but you lose crash recovery:
set noswapfileThis is dangerous for production editing. A better approach is to use a central swap directory as shown above.
Automatic Cleanup
Add this to your .vimrc to automatically delete swap files after successful saves:
augroup auto_delete_swap
autocmd!
autocmd BufWritePost * call delete(expand("%:p:h") . "/." . expand("%:t") . ".swp")
augroup ENDRecovering After a Complete System Crash
If the system crashed and you need to recover multiple files:
```bash # Find all swap files find / -name ".*.swp" -type f 2>/dev/null
# For each swap file, recover for swap in $(find ~ -name ".*.swp" -type f 2>/dev/null); do original=$(echo "$swap" | sed 's/\/\.\(.*\)\.swp$/\/\1/') echo "Recovering: $original from $swap" vim -r "$original" << EOF :wq EOF done ```
This batch-recovery approach saves time when you have many unsaved files after a crash.