I use MacVim (snapshot 51) to edit a the Python/HTML/etc files in moderately sized project. Every few months my system locks up or crashes, and Vim leaves a plethora of .*.swp files around. When I go to re-open Vim after the crash and edit any of those files, I am told "Swap file ''.xyz.swp'' already exists!" with the options of [Abort] [Quit] [Delete it] [Recover] [Edit anyway] and [Open Read-Only].

This recovery option would make sense if there were changes to the Vim buffer that hadn't been saved at the time of the crash. However, at the time of the crash these files were opened in the background of Vim, and unchanged.

If possible, how can I configure Vim to not ask this superfluous question when opening files for which there are no changes between the .swp file and the actual file, e.g. by one of:

  1. Automatically comparing the changes in the .swp file to the file, and if there are no changes then just opening the file without prompting;

  2. Not keeping a .swp file for files that have no changes.

  3. Any other means of systematically avoiding this pointless prompt (but I'd prefer not to avoid the prompt in cases where, of course, there were unsaved changes).

I have over fifty .swp files to clean up at the moment, and the pointless prompt is a frustration I'd be grateful to avoid.

Thank you for reading.


  • "Every few months"? If your system only crashes every few months and you get a lot of .swp files, you must be exiting Vim the wrong way. Use :q, don't just Ctrl+Z. – mcandre Jun 21 '10 at 17:56
  • @mcandre: Hmm. I'm using :q, but the swap files nevertheless seem to be hanging around for buffers in the background. Perhaps that's more my problem. I don't see anything in ~/.vimrc that would keep buffers around ... (Incidentally, what does Ctrl+Z do? It doesn't seem to do anything in MacVim) – Brian M. Hunt Jun 21 '10 at 18:56
  • Ctrl+Z kills a process that's been launched from a shell (Terminal). Since you're using MacVim it doesn't apply to your situation, but you might try it from your Terminal sometime… – bchang Jan 30 '11 at 17:35
  • Actually, Ctrl-Z doesn't kill anything. It suspends the process. (But not in GUI Vims.) That lets you get back to the shell, do some things, and bring Vim back up again. If you kill the shell in the meantime (say, by closing the terminal window) you will kill Vim. – Peeja Sep 27 '12 at 14:42

I haven't tested it, but what about this solution presented in vim's official wiki?

Swap file "..." already exists! - so diff it

Also in the situation where the recovered swapfile turns out to be identical to the real file, there is no need for the diffing.

# Expects variables realfile, swapfile, recoveryfile.
vim -r "$swapfile" -c ":wq! $recoveryfile" && rm "$swapfile"
if cmp "$recoveryfile" "$realfile"
then rm "$recoveryfile"
else vimdiff "$recoveryfile" "$realfile"

There's a script for this problem as well in Stackoverflow.

This will remove any swap files that are up-to-date with the real files. Any that don't match are brought up in a vimdiff window so I can merge in my unsaved changes.


While not exactly what you want, this can mitigate the issue:

" Setup backup location and enable
set backup
set backupdir=$HOME/temp/vim_backups/       "where to put those backups
set directory=$HOME/temp/vim_swp/           "this is for swp files      

Create those directories, and vim with write the ~ and .swp files to a central location. Much easier to clean up this way!

Also, check out recover.vim - if a .swp file is detected, it will prompt to run a diff on the on-disk version and the swp, so you can see what's changed. Much more helpful this way IMO.


Vim automatically creates .swp files as a backup for exactly that situation: the system crashes. To disable .swp file creation, follow these instructions.

1st result for Google: vim disable .swp.

  • @mcandre: Thanks. Disabling swap is a worst case solution, and given the (albeit low probability) high value of potential loss from not using swap files I probably won't do this. It's the pointless recovery prompt that I want to avoid. – Brian M. Hunt Jun 21 '10 at 16:58
  • You could write a login script that finds and moves all .swp files to a backup directory (or even deletes them). – mcandre Jun 21 '10 at 17:55
  • @Mcandre: Thanks for the suggestion. That could work, and I've thought of that, but it wouldn't tell me if there were any unsaved changes in the swap files. – Brian M. Hunt Jun 21 '10 at 18:52
  • Then add a "diff" to the script to compare the files. – mcandre Jun 21 '10 at 19:05
  • @mcandre: What would you diff? Is there a way to extract the content of the Vim swap file for a comparison via diff? – Brian M. Hunt Jun 21 '10 at 20:42

Reading the documentation for the "swap-file" feature in Vim by running the Vim command :help swap should help you with a good many things about how Vim handles swap file creation, etc.

  • While :help swap should help with a good many things, in this case it does not. Nor does Robbins et al's "Learning Vi & Vim Editors". If either did, I would not have needed to post the question. ;) – Brian M. Hunt Jun 21 '10 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.