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?
Make a note of the swap file name (e.g., . after. c. swp ), exit vim, type rm .
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.
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.
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).
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?
You can add
set noswapfile
to your vimrc. This will cause swap files to not be created in the first place.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With