vim

How to Delete Vim Swap Files

While using the Vim editor, it auto-generates temporary files called swap files. These files keep the unsaved data and recover it in case of app crashes. If you save and close the file properly, the swap file is automatically deleted.

The swap files are important but can become a problem if they pile up. They often remain saved even if you have deleted the original file. These files are somewhat difficult to find, they do not appear explicitly in the directory because they are hidden. To delete these files, you need to locate them first locate. In this tutorial, I will be exploring how to find the hidden swap files and different methods to delete these files.

Note: For this guide, I am using macOS (Unix), the commands would be similar for Linux distributions as well. However, the instructions may differ for Windows.

Finding Vim Swap Files

As mentioned earlier, the swap files are hidden and may not be visible when you open the current working directory. In Linux and Unix-like operating systems, the Vim swap files are saved with a dot (.) prefix.

To access the Vim swap files, utilize the ls command with the -a flag to list the hidden files.

ls -a

The hidden file names in Unix-like operating systems start with a dot (.).

To list all swap files in the current directory, use the vim command with the -r flag.

vim -r

To find the swap file name of the currently opened file, use the :swapname command in the Vim editor:

:swapname

Be aware that deleting swap files may cause you to lose unsaved changes, so be sure to save your work before attempting the deleting Vim swap file processes.

Deleting the Vim Swap Files

Now that you know where the swap files are, it is easy to delete them. There are multiple ways to delete the swap files:

1. To delete Vim swap files, navigate to the directory where the files are located. Vim swap files have a .swp extension and are typically named with the pattern .<filename>.swp corresponding to the edited file filename. Use the rm command with the file name or multiple file names separated by spaces to delete these files.

rm .<filename>.swp

Typically, the Vim swap file is generated and stored in the working directory. However, this is not always the case, and it is possible to generate them in a specific directory as well. This method requires you to be in the directory where the swap files are present.

A separate directory for the swap files can be created for that, read the following sections of this guide.

Also note that this command will permanently delete the file, so carefully check the file before deleting it.

2. Open the file in the Vim editor, and press r to recover it. If the file is not corrupted, save it by using the :w command in the NORMAL mode.

Now, use the :e (reopen) command to delete the swap file. Type the command in the NORMAL mode and press Enter.

:e

You will get a prompt at the bottom of the window, press d to clear out the swap file.

It is advantageous to delete the swap file with this approach because it will not overwrite the original file in case the recovery has been corrupted.

3. Type the vim command with the -r flag and the file name. A list of related recovery files will appear.

vim -r <filename>

You will be given options to recover the file, type a number from the given options.

You may get the following warning, which can be different depending on the situation; press Enter to open the file and continue editing.

Check the file, if the file is modified save it and delete the swap file by using the :e command.

:e

4. You can also use the find command to locate and delete swap files. Open the directory where the swap files are and use the command mentioned below:

find -type f -name "*.sw[pnl]" -delete

The f indicates the normal file type, * is the wild card which indicates all the file names with .sw[pnl] extension, and the -delete option is deleting them.

The Vim generates different types of swap files, such as swn, swm, swo, swl, swk, and swp. To delete all the files, you can use the above command by adding more characters in the command, such that .sw[nmolkp].

However, the drawback of this command is that it searches for the files recursively from all the directories and subdirectories. You better use this command if you have a separate directory for the swap files.

Get Rid of Vim Swap File Warnings

You may get swap file warnings while opening a file in the Vim editor for various reasons.

The reasons that cause the Vim swap file warning are listed below:

  • The file is currently open in some other session.
  • The file is remotely edited, and the connection is lost.
  • The terminal is closed without saving the file.
  • The process has been sent to the background using ctrl+z.

If the file is currently opened in some other session, you will get the following warning:

As you can see, the process is still running in some other session, try to quit one session. Because the session is still running, there is no delete option for the Swap file.

Or you can get a warning of a file that you have modified but could not properly save it.

In this situation press r to recover the file, you may get different versions of the swap files select the latest one (or older as preferred), and open it.

Now, type :e command and select d to delete the swap file.

Permanently Disable Vim Swap File Generation

It is not advisable to disable the swap file generations for Vim because they are useful to recover the data in case of a mishap. But if you truly want to eliminate these files to maintain a cleaner system, you can do so by adding the set noswapfile to the vimrc file.

set noswapfile

Create a Separate Directory to Store Vim Swap Files

You should first create a directory where all the Vim swap files will be generated and saved.

mkdir -p ~/.vim/vimswapfiles

Also, create directories to keep the backups and undo history.

mkdir -p ~/.vim/vimbackupfiles

mkdir -p ~/.vim/vimundofiles

Now, set the created directory as a swap file location using the set command.

set directory=~/.vim/vimswapfiles//

set backupdir=~/.vim/vimbackupfiles//

set undodir=~/.vim/vimundofiles//

Double slashes (//) are added at the end of the path to avoid name collisions, as the double slash will create the swaps or backups using the whole path.

Change Vim Swap file Update Settings

The swap is updated every four seconds when there is no typing activity. If you are typing, then the swap file will update after 200 characters.

The reason the swap files are not updated every second is that it may slow down the normal task that you are doing in Vim. However, you can set the time and character count by changing the updatecount and updatetime in the vimrc file.

Conclusion

The swap files are auto-generated files that keep track of the modifications we make while editing the files in Vim and update it every 200 characters. When the file is properly saved and closed, the swap file is deleted automatically.

To delete the unwanted swap files, recover them first and ensure the file is not corrupted. The Vim shows ???MISSING LINES if the file is corrupted. If everything in the file is ok, then delete the swap file by using the :e command and selecting the delete option.

About the author

Sam U

I am a professional graphics designer with over 6 years of experience. Currently doing research in virtual reality, augmented reality and mixed reality.
I hardly watch movies but love to read tech related books and articles.