Last Updated: February 25, 2016
·
721
· tonyliaoss

[VIM] Deleting all swap files under a top-level directory

Vim is awesome. But sometimes, when your program crashes you may end up with undeleted *.swp files. This is fine if you only have one or two files. But if you are on a system that crashes every so often, eventually you are going to have a directory filled with these *.swp files. Not very desirable eh?

Well with simple utilities like grep and find, you could use a simple command to remove all *.swp files under your current directory by typing:

$  find -name '.*.sw[a-z]' -exec rm -i {} \;

into the terminal.

The regular expression '.sw[a-z]$' searches for any file that ends with .sw?, where ? is a character between a to z. This is only necessary because sometimes vim creates new swap files when it notices a conflict, and it names these files with the extension .swo, .swn, et cetera.

A slightly more sophisticated bash script:
https://github.com/tonyliaoss/vim_config/tree/master/tools

2 Responses
Add your response

When i tested the find by it self, i found that it matches few things that not all people might want to delete like swf which might be a flash developer/designer exported file, also flash's cached shared objects, and some files called swz inside ~/.adobe, no idea if those are important.
Also i think your command could be reduced to just find and rm
find -name '*.swf' -exec rm {} \;

over 1 year ago ·

Hi coalwater:

Good point. I will fix the regex to only look for hidden files such as .[filename].swp.

over 1 year ago ·