Asfbar
Asfbar

Reputation: 279

Killing vim processes with a shell script leaves .swp files

I developed a script that will kill all 'vim' processes working on xxx.log files:

ps -ef|grep vim|grep xxx.log|awk '{print $2}'|xargs kill -9

However, the .swp (swap) files remain for each vim instance killed. How can I also delete the swap file in the same script, or other some short solution without searching for the location of the swap etc?

Upvotes: 6

Views: 4252

Answers (3)

Kevin Walker
Kevin Walker

Reputation: 116

You can add

set noswapfile

to your vimrc. This will cause swap files to not be created in the first place.

Upvotes: 2

Ingo Karkat
Ingo Karkat

Reputation: 172698

Drop the -9 from kill; this will send the TERM(inate) signal to Vim, allowing it to clean up and remove the swap file (at least it did for me on Ubuntu).

By using -9 / KILL, you don't give the process a chance to clean up. This should only be used for situations where it's absolutely necessary (e.g. when the process is hung or in an endless loop and doesn't react to external signals any more).

Upvotes: 14

Marcus Müller
Marcus Müller

Reputation: 36452

Since the swap file contains the name you deleted, you should be able to find it with a appropriate action, too.

However, killing an interactive editor like vim sounds very wrong on so many levels. You should really consider not doing that. In which situation is that desirable?

Upvotes: 2

Related Questions