Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 670
Asfbar Avatar asked Apr 16 '15 08:04

Asfbar


People also ask

How do I delete a .SWP file in vim?

Make a note of the swap file name (e.g., . after. c. swp ), exit vim, type rm .

Why does vim create SWP files?

vim creates these swap files for two purposes. First, the swap file is created to avoid multiple instances of vim editing the same file. We need this for systems with multiple users or if we try to open an already opened file. Moreover, vim creates the swap files to recover the changes if something crashes.

Is it safe to delete vim swap files?

Get rid of the swap file. Using the swapfile to recover your work doesn't automatically delete it, but if the recovery was successful, it's fine to delete it.


3 Answers

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).

like image 197
Ingo Karkat Avatar answered Oct 06 '22 01:10

Ingo Karkat


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?

like image 38
Marcus Müller Avatar answered Oct 06 '22 00:10

Marcus Müller


You can add

set noswapfile

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

like image 31
Kevin Walker Avatar answered Oct 05 '22 23:10

Kevin Walker