What's Actually Happening

Vim reports swap file exists when opening a file. This happens when a previous Vim session crashed or is still running.

The Error You'll See

```bash $ vim myfile.txt

E325: ATTENTION Found a swap file by the name ".myfile.txt.swp" owned by: user dated: Mon Jan 1 00:00:00 2024 file name: ~user/myfile.txt modified: YES user name: user host name: hostname process ID: 12345 (STILL RUNNING) While opening file "myfile.txt" dated: Mon Jan 1 00:01:00 2024 ```

Why This Happens

  1. 1.Vim crashed
  2. 2.SSH session disconnected
  3. 3.System shutdown
  4. 4.Another Vim instance editing same file
  5. 5.Improper exit

Step 1: Check for Running Vim

bash
ps aux | grep vim

Step 2: Check Swap File

bash
ls -la .myfile.txt.swp

Step 3: Recover Changes

bash
vim -r myfile.txt
# Or in Vim:
:recover

Step 4: Delete Swap File

bash
rm .myfile.txt.swp

Step 5: Compare Versions

bash
vim -d myfile.txt .myfile.txt.swp

Step 6: Kill Running Vim

bash
# If Vim is still running:
kill 12345
# Force kill:
kill -9 12345

Step 7: Disable Swap

vim
" In .vimrc
set noswapfile

Step 8: Change Swap Directory

vim
" In .vimrc
set directory^=~/.vim/swap//

Step 9: List All Swap Files

bash
find . -name "*.swp" -ls
find . -name ".*.swp" -delete

Step 10: Prevent Swap Issues

vim
" Auto-save frequently
set updatecount=100
" Store swap files in central location
set directory=~/.vim/swap//
  • [Fix Vim Config Not Loading](/articles/fix-vim-config-not-loading)
  • [Fix Vim Plugin Not Working](/articles/fix-vim-plugin-not-working)