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

A slightly simpler way:

From the terminal emulator:

vim filename

Then choose recover, and if you want to delete the swap you write (save) and then do this from within vim:

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

...and choose delete this time.

I just found out about this, and I think I'll do that from now on. Then I won't accidentally overwrite a working file either, if the recovery turned out corrupt (though I've never tried that before).

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


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
    

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.

Tags:

Vim

Swap File