Removing junk .swp files

1487 views
Skip to first unread message

BPJ

unread,
May 1, 2013, 1:49:06 PM5/1/13
to vim_use
Is there any easy/automatized way to remove junk (as in not
associated with any file currently open in (g)vim) .swp files
in the current directory and its subdirectories?
A plugin perhaps?

/bpj

Mike Hume

unread,
May 1, 2013, 3:11:08 PM5/1/13
to vim...@googlegroups.com
You could setup a tmp directory for where swp files are stores.

set directory=~/.vim/tmp/swap

If you have swp files littered about, run this command to recursively find and delete them:

find . -type d -name .swp | xargs rm -rf

~Mike
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> --- You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>

Ben Fritz

unread,
May 1, 2013, 3:12:58 PM5/1/13
to vim...@googlegroups.com, b...@melroch.se

Such .swp files should never exist if you always exit Vim normally.

Only when Vim crashes or gets terminated abnormally will you have such files hanging around.

Having them stick around is how recovery works.

One of the "swap exists" choices that will appear the next time you edit that file, is to delete the swap file. If Vim is not editing that file, under what circumstances should Vim delete them? You risk losing the ability to recover files if Vim scans the current directory for any swap files on startup or something.

On Unix-like systems, swap files should be hidden by default, so you won't see them cluttering up anything unless you deliberately show swap files.

On Windows systems, check out my autohide plugin to accomplish the same:

http://www.vim.org/scripts/script.php?script_id=4505

Mike Hume

unread,
May 1, 2013, 3:13:11 PM5/1/13
to vim...@googlegroups.com
Scratch the part about -type d

~Mike

On May 1, 2013, at 10:49 AM, BPJ <b...@melroch.se> wrote:

Ben Fritz

unread,
May 1, 2013, 3:16:14 PM5/1/13
to vim...@googlegroups.com, b...@melroch.se
On Wednesday, May 1, 2013 2:12:58 PM UTC-5, Ben Fritz wrote:
>
> Such .swp files should never exist if you always exit Vim normally.
>
> Only when Vim crashes or gets terminated abnormally will you have such files hanging around.
>

Also there may be swap files associated with a DIFFERENT Vim instance, especially if you or others edit files on a network location using Vim from multiple computers.

Deleting them automatically might interfere with other Vim instances. I think you will always want a confirmation step at least before deleting swap files.

Edward Beckett

unread,
May 1, 2013, 3:44:23 PM5/1/13
to vim...@googlegroups.com

mike's suggestion is I similar to what I use... and it works quite well... I also prepend the abs path to the file to avoid name collisions...

--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php

--- You received this message because you are subscribed to the Google Groups "vim_use" group.
To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+unsubscribe@googlegroups.com.

tooth pik

unread,
May 1, 2013, 4:37:31 PM5/1/13
to vim...@googlegroups.com
On Wed, May 01, 2013 at 12:11:08PM -0700, Mike Hume wrote:
> You could setup a tmp directory for where swp files are stores.

> set directory=~/.vim/tmp/swap

> If you have swp files littered about, run this command to
> recursively find and delete them:

> find . -type d -name .swp | xargs rm -rf

you're going to need an asterisk in front of that '.swp'

Edward Beckett

unread,
May 1, 2013, 5:10:09 PM5/1/13
to vim...@googlegroups.com

And if you pass the -d argument to the find method it will never match swp files :-)

BPJ

unread,
May 1, 2013, 5:40:24 PM5/1/13
to vim...@googlegroups.com
I know all that. But you may get junk swap files hanging around
if you inadvertently compress a directory containing files which
are currently open in Vim at compression time. Stupid, but there
you have it!

/bpj

George Dinwiddie

unread,
May 2, 2013, 8:09:17 PM5/2/13
to vim...@googlegroups.com
Mike, BPJ

On 5/1/13 3:11 PM, Mike Hume wrote:
> You could setup a tmp directory for where swp files are stores.
>
> set directory=~/.vim/tmp/swap
>
> If you have swp files littered about, run this command to recursively find and delete them:
>
> find . -type d -name .swp | xargs rm -rf

That "rm -rf" can be a bit destructive when run on a directory.

I suggest

find . -name "*.swp" -exec rm {} ";"

This won't check to see if the file is in use, so do it when vim isn't
running.

- George

>
> ~Mike
>
> On May 1, 2013, at 10:49 AM, BPJ <b...@melroch.se> wrote:
>
>> Is there any easy/automatized way to remove junk (as in not associated with any file currently open in (g)vim) .swp files
>> in the current directory and its subdirectories?
>> A plugin perhaps?
>>
>> /bpj
>>
>> --
>> --
>> You received this message from the "vim_use" maillist.
>> Do not top-post! Type your reply below the text you are replying to.
>> For more information, visit http://www.vim.org/maillist.php
>>
>> --- You received this message because you are subscribed to the Google Groups "vim_use" group.
>> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
>> For more options, visit https://groups.google.com/groups/opt_out.
>>
>>
>

--
----------------------------------------------------------------------
* George Dinwiddie * http://blog.gdinwiddie.com
Software Development http://www.idiacomputing.com
Consultant and Coach http://www.agilemaryland.org
----------------------------------------------------------------------

Paul

unread,
May 8, 2013, 5:56:03 AM5/8/13
to vim...@googlegroups.com
On Wednesday, 01 May, 2013 at 20:11:08 BST, Mike Hume wrote:
>You could setup a tmp directory for where swp files are stores.
>
>set directory=~/.vim/tmp/swap
>
>If you have swp files littered about, run this command to recursively find and delete them:
>
>find . -type d -name .swp | xargs rm -rf

I don't recommend this, because like George Dinwiddie says, it doesn't check to see if any swap files are in use, but this is a more efficient command:

find ~ -type f -name \*.swp -delete

Gary Johnson

unread,
May 8, 2013, 1:05:15 PM5/8/13
to vim...@googlegroups.com
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.

Regards,
Gary

Adrian Luff

unread,
May 9, 2013, 11:40:45 AM5/9/13
to vim...@googlegroups.com
Assuming you have the following in your .vimrc:
set directory=$HOME/.vim/swap

You can easily use find and metadata from the file system to remove files that have not been accessed in more than 30 days:

find ~/.vim/swap -type f -atime +30 -name \*.sw? -exec rm -f {} \;

Using xargs doesn't correctly handle files with spaces in their name by default in my environment. I'm not sure of the portability of fixes for this so I stuck with -exec.

-Adrian

Reply all
Reply to author
Forward
0 new messages