Longwood University Department of Computer Science Advanced Computing Lab

19 Sept 2018


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

Vim Swapfile

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 the swap file comes up, vim presents you with several choices.

If you are -certain- that the file on the disk is the correct one and you don't need the "autosaved" information in the swapfile, you can simply type "D" to delete the swapfile. The message will go away and you can continue working. In some circumstances, vim may not show this option (for instance, if the file is still open in another window or on another computer). In that case, you can delete the file using the Linux "rm" command (see below).

If you want to view the swapfile to see what is in it, you can type "R" to recover the file. This doesn't actually replace the file on disk -- it simply loads the contents of the swapfile into vim so you can view it. If you decide the version in the swapfile is the one you want to keep, you can type :wq to save it over the version on the disk. The next time you open vim, you will still get the swapfile message, but it will contain the phrase "NEWER than swapfile". When you see this, you should choose the "D" option to delete the swap file.

If instead you decide to keep the version on disk instead, you can type :q! to quit and then choose the "D" option to delete the swapfile when you next open vim.

If you want to see the version on disk, you can type "E" to "Edit Anway". This doesn't remove the swapfile, but does let you see what is currently saved to disk.

Using the Linux rm command

If you need to remove a swapfile and can't use the "D" option, you can remove the file by hand using the "rm" command. To do that, you first need to know the name of the swapfile. The swapfile's name is derived from the name of the file you are editing. Typically, vim will add a dot to the beginning of the filename and add ".swp" to the end. For example, if you were editing a file named index.html, the vim swapfile would be named .index.html.swp. The dot at the beginning makes the swapfile a hidden file, so it won't show up when you type "ls" unless you type something like "ls -ald .*". To remove the file, you would type "rm" followed by the name of the swapfile. For example, you might type:

rm .index.html.swp

This would remove the swapfile for index.html. Be careful with this command! The "rm" deletes files permanently. It doesn't "move them to a trashcan" where they can be recovered. If you delete the wrong file, it is not always easy (or even possible) to get it back.

It is possible that vim can create multiple swap files for the same file. In this case, vim may use .swo, .swn, and so forth, instead of .swp. You can use ls -ald .* to see them and remove them.