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, 2010 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) Jun 21, 2010 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, 2011 at 17:35
  • 1
    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, 2012 at 14:42
  • @bchang not sure about mac (I assume thread is talking about mac as the vim prog is Macvim), but this sounds alot like [ctrl] + [z] on GNU/Linux. If so: The reason it doesn't work is because it's up to the target to close. It just sends a signal (SIGTERM) to the target. This tells it to terminate, but target decides the implementation, so it can just ignore. Another signal, SIGKILL, doesn't give it the option. It is the result of using pkill command and the like via terminal.
    – Nate T
    Aug 7, 2021 at 1:55

5 Answers 5


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.

  • 1
    @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. Jun 21, 2010 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, 2010 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. Jun 21, 2010 at 18:52
  • Then add a "diff" to the script to compare the files.
    – mcandre
    Jun 21, 2010 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? Jun 21, 2010 at 20:42

I would say that the main reason that it doesn't diff each of the buffers before saving when the machine is crashing is because.. well.. THE MACHINE IS CRASHING!! XD

I know from personal experience that writing a program which runs only as the machine is forcefully shutting itself down, and trying to make it robust (i.e. the result is the same regardless of the environment) is not a simple task.

I am guessing that the developers who first coded this feature -- not sure if it first appeared in Vi or Vim -- felt mostly the same way you do about the subject, at least until they tried coding it.

If environmental constraints are keeping you from making a program "smart", what is the logical choice? You make it safe.

When considering the logic of the 'abort-gracefully' strategy that Vim (and Macvim) uses, it seems like they agreed and adopted a "better safe than sorry" approach. This is common in application programming, especially in languages like C/C++, where developers can access the architecture at a relatively low level.

For a software developer familiar with C, it wouldn't be hard to build a hook that saves only the "worthwhile" files to a separate directory, and use a chron job to periodically auto-delete the contents of the swap directory (location is dependant on your editor; I use Neovim on Linux, so mine are at~/.local/share/nvim/swap/) or via the instructions that @mcandre linked in his/her answer above.

The hard part would be getting the whole program to run before the machine dies. Otherwise, I hope you didn't have anything important on the files toward the end of the queue.


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. ;) Jun 21, 2010 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.