简体   繁体   中英

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?

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 (eg 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM