1. Overview

In this tutorial, we’ll talk about the swap files of the vim editor. We’ll cover what they are, what their purposes are, and how to manage them.

2. What Are Swap Files in Vim?

While editing a file with vim, we might have seen that the editor creates one file automatically: the swap file. Swap files are buffer files in binary that vim creates as a temporary modified copy of the edited file. Once the edition is completed in vim and the edited file is saved to disk, the contents of the swap file are merged with the edited file and the swap file is deleted. Swap files are not simply auto-save files; they include other bits of information, such as the undo history.

To exemplify this, let’s assume we’re in a directory with a single file. We can check this with the ls command and the -a flag to show all files, including the hidden files, whose name is prepended by a dot:

We can now open the edited file with vim:

$ vim file1

Let’s discuss more in detail the first of the stated purposes of swap files. Let’s consider again the same scenario as before:

If we attempt to open the same file in another terminal, we’ll receive a warning message when opening vim:

$ vim file1
E325: ATTENTION
Found a swap file by the name "~/folder/file1.swp"
          owned by: me   dated: Thu Jan 01 00:00:01 1970
         file name: ~folder/file1
          modified: yes
         user name: me   host name: sierra
        process ID: 63924 (STILL RUNNING)
While opening file "file1"
             dated: Thu Jan 01 00:00:01 1970
(1) Another program may be editing the same file.  If this is the case,
    be careful not to end up with two different instances of the same
    file when making changes.  Quit, or continue with caution.
(2) An edit session for this file crashed.
    If this is the case, use ":recover" or "vim -r file1"
    to recover the changes (see ":help recovery").
    If you did this already, delete the swap file "/home/me/folder/file1.swp"
    to avoid this message.
Swap file "~/folder/file1.swp" already exists!
[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort: 

By means of the swap file, vim knows that there is potentially another program editing the same file. We should think twice before opening the file again, which would create conflicts.

3.2. Recovery From Swap Files

From the warning message of the previous subsection, we can also see the second purpose of swap files: to recover from a crashed session. As the swap file is created from start and is automatically updated with new changes, we should ideally be able to recover most of our changes.

We have different options to handle the swap files from crashed sessions:

[O]pen Read-Only, (E)dit anyway, (R)ecover, (Q)uit, (A)bort

If we don’t want to modify the content of the swap file, there are three options:

  • Open the swap file as a read-only file with O.
  • Quit the current command with Q, allowing vim to continue, when present, the execution of other commands.
  • Abort the execution of vim with A.

To further exemplify the difference between these two last options, for a command like vim file1 file2, the option Q will open file2 while A will exit.

If we want to modify the contents of the swap file, there are two options:

  • Recover the swap file with R. We don’t replace the edited file, but vim loads the content of the swap file instead.
  • Edit the desired file with the E. We preserve the swapfile, but vim opens the file to be edited anyway.

There is another option that only appears sometimes for deleting the swap file:

[O]pen Read-Only, (E)dit anyway, (R)ecover, (D)elete it, (Q)uit, (A)bort

The D option only appears if vim knows that there is no other editor with the file open. If the option is not displayed, and we still want to remove the file, we can use the rm command to get rid of the swap file:

$ rm .file1.swp

Finally, we can also check the contents of an orphaned swap file:

$ vim -r file1.swp

vim reads and recreates the content of the file1.swp. Once we’ve recovered the information, we can see the file as usual (with the :w command).

4. Temporarily Disabling Swap Files

One drawback of swap files is that they may take large chunks of disk storage. When editings large files, the swap file may grow up to an even larger file size. Thus, we might not always want to use swap files.

The -n flag avoids the creation of a swap file when opening a file:

$ vim -n file1.swp

If we’ve already opened the file, we can still override the creation of the swap file for the specific buffer:

:setlocal swapfile

5. Completely Disabling Swap Files

Although not recommended, it is possible to completely disable swap files. We can do so by adding the noswapfile option to the configuration file:

$ touch ~/.vimrc
$ echo "set noswap file" >> ~/.vimrc

There are plugins such as vim-auto-save that can be configured to write to the edited file in every scenario where vim would write to the swap file. This suppresses the need for a swap file.

6. Change Default Storage Location

Finally, if our issue is having multiple files throughout the system, we can store all the swap files in one single directory instead of in the directory of the edited files. We can use an existing temporary/cache directory or create a new one:

$ mkdir ~/.vim/temp

Then, we add to the configuration file the list of directories that vim will use to store the swap files:

$ touch ~/.vimrc
$ echo "set directory^=$HOME/.vim/tmp//" >> ~/.vimrc

We use ^= to prepend this directory to the available list and // at the end to request the use of absolute paths (to avoid collisions between edited files with the same name on different directories).

The drawback of this approach is that vim from different users might not know that another editor has a file open, given that the swap file is not in the same directory as the edited file and users may not have access to other users’ $HOME folder.

7. Conclusion

In this article, we’ve discussed in detail the swap files of vim, their purpose, and how we can manage them in our system.