Vim swapfiles

updated 27 Sep 2022

Occasionally when you try to edit a file with Vim, you will get a message that looks like this:

Vim Swapfile

This page will tell you how to interpret what you're seeing and (perhaps more importantly) how to resolve it.


One reason this message can pop up is that you are editing the file in two different windows or over the network from two different hosts. In that case, the correct solution is usually to simply quit one of the two running sessions and resume your work in the other. This can also occur if you press CTRL-Z and background your session (either on purpose or on accident). In this case, you can simply type "fg" to return the session to the foreground.

However, this message can sometimes occur even if you don't have the file open some place else. The most common reason that happens is that you have been editing a file remotely and you either lose your network connection or close your terminal without properly quitting vim.

The reason this message comes up is that every time you edit a file, vim creates something called a "swapfile". A swapfile is a bit like an autosave file -- it keeps track of the current state of your document -- but it also stores additional information about your editing session (such as your undo/redo history). When vim closes normally, it deletes the swapfile. But if vim is interrupted, it can close or be terminated before removing the swapfile. This is a very good thing -- it can sometimes keep you from losing hours of unsaved work -- but it does mean that you need to know how to resolve an undeleted swap file.

Resolving an undeleted swapfile

When you see the message that a swap file is found, there is a lot of text and several choices. There are three important details to look for:

The above image is marked up with where to look for the answers to these questions in this diagram:

modified: no; STILL RUNNING

and a different swap file situation is shown here, also marked with what to look for:

modified: YES; NEWER than swap file

The following "script" will help you deal with various swapfile situations.

Is the original vim "STILL RUNNING"?

If the edit session is marked as still running, first check if you have it open in another PuTTY or terminal window. If so, just return to that! Press Q to "(Q)uit" the vim swap file recovery.

If it's not in another window, it's possible you accidentally pressed Ctrl-Z and this "suspended" your vim session—it's still running, but not visible anywhere. If you're pretty sure that happened, running "fg" (for "foreground") at the command line in the terminal window where you suspended the editor might bring it back. Problem solved! Press Q to "(Q)uit" the vim swap file recovery.

But if neither of those apply, perhaps the network connection died on you or you forgot to close the editor before you closed your laptop or something else like that happened, and if the editor's still running, you need to stop it. Make a note of the process ID number right before the "STILL RUNNING" (in the above diagram it's 18699). Press Q to "(Q)uit" the vim swap file recovery for now, then use the "kill" command with the process ID to end your previous vim session, e.g.

  kill 18699
Then, run vim on the file again. If it's still "STILL RUNNING", try
  kill -9 18699
to "really" kill the vim session. The next time you run vim on the file, you might still have a swap file recovery dialog but it won't be STILL RUNNING. (Or, if the file wasn't modified, killing the process may have automatically resolved the swapfile, with no further work needed.)

Is the file "NEWER than swap file"?

This means that at some point after the swapfile was last updated, you edited the file and saved things. Maybe that's because you previously recovered from this swapfile, or because you ignored the swapfile dialog and edited it anyway; but regardless, it means that if you make use of this swapfile, you'll actually lose some of your more recent work.

Usually, when the file is "NEWER than swap file", you want to ignore the swap file. Press D to "(D)elete" the swapfile and use your more recent work.

Is the file modified (and neither of the other rules apply)?

Awesome! This means vim has rescued some of your work from being lost by a network connection problem or some similar issue. Here are the steps to follow through on that:

1. Recover. Press R to "(R)ecover" your edits, using the swap file. This will move you into the regular vim editor, hopefully with your most recent version of the file.

2. Check the file and save it. Look around the file and confirm that it's the version you want, then ":wq" to save it and quit. Don't do any further editing just yet.

3. Get rid of the swap file. Using the swapfile to recover your work doesn't automatically delete it, but if the recovery was successful, it's fine to delete it. Run vim on the file again and you should still get a swapfile dialog, but this time warning that the file is "NEWER than swap file". Press D to "(D)elete" the swapfile.

Now you should be good to continue editing. The swapfile has done its job, and you've removed it.