What's the easiest way to delete Vim swapfiles I've already recovered from?

Solution 1:

A slightly simpler way

From the terminal emulator:

$ vim filename

Then inside vim choose recover, and if you want to delete the swap you write (save) and then call this command:

:e          // no argument needed. It assumes the current buffer.

...and choose delete this time.

A benefit to this approach is that it won't automatically overwrite a working file, if the recovery turned out corrupt (though admittedly I've never had that happen before).

There are also relevant tips on this question on Stack Overflow (where I found this tip).

Solution 2:

To Clean out ALL vim Swap Files in a Directory:

If you are sure you don’t need any vim swap files in a directory tree and want to get rid of them, you can use the following command in the directory while vim is not running (not even in another window or even a different login session):

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

Some inspiration and thoughts came from Removing junk .swp files at Google Groups.  This will delete all files whose names end with .swk, .swl, .swm, .swn, .swo, or .swp in the current directory tree.

This might not be what you want, for several reasons:

  • As stated, it searches the current directory tree ; i.e., the current directory and all subdirectories, recursively.  That goes beyond what the question asks for, and may be considered to be overkill.
  • As stated, it will delete all files whose names end with .swk, .swl, .swm, .swn, .swo, or .swp, and not just .swp.  Gary Johnson says,

    Also, not all swap files end in .swp.  If Vim needs to create a swap file and one ending in .swp already exists, Vim will use the extension .swo for the new one and .swn after that.  I think it just continues backwards through the alphabet.  So using something like
      \*.sw[nop] 
    

    or even

      \*.sw? 
    

    would be more thorough.

    The original author of this post says,

    'klmnop' may be overkill, but that usually ensures I get all of them.

  • On the other hand, it might be underkill.  rouble suggests

    find . -type f \( -name ".*.s[a-v][a-z]" -o -name ".*.sw[a-p]" \) -delete
    

    which matches all (lower-case) three-letter extensions ranging from .saa to .swp.  He adds, “Be careful though, while this is a more complete solution, it will take out any .svg image files and .swf adobe flash files.  You may want to modify the regex if you work with those files.”  It also matches .sav, .snd (sound data), .sql, .src, .srt (SubRip video subtitle format), and many other common extensions.

  • It might be overkill.  As stated, it will delete all files whose names end with .swk, .swl, .swm, .swn, .swo, or .swp.  But, apparently, vim swap files are commonly “dot” files.  If you edit hello.cpp, the swap file might be .hello.cpp.swp.  As shown (but not explained) in rouble’s answer, it is safer to delete only files whose names begin with a dot; e.g., with

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

Solution 3:

Type ls -a which lists ALL the files in the directory

type rm .whatever.your.swp is and press enter

Its that simple.

Any file that shows with . in front is a hidden file and is not normally seen.

Remember that changes are immediate and permanent so be careful.

Solution 4:

Btw. some plugins do that automatically for you: autoswap.vim

from the description:

  1. Is file already open in another Vim session in some other window?
  2. If so, swap to the window where we are editing that file.
  3. Otherwise, if swapfile is older than file itself, just get rid of it.
  4. Otherwise, open file read-only so we can have a look at it and may save it.