简体   繁体   English

使用shell脚本杀死vim进程会留下.swp文件

[英]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: 我开发了一个脚本来杀死处理xxx.log文件的所有'vim'进程:

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

However, the .swp (swap) files remain for each vim instance killed. 但是,每个vim实例都会保留.swp(swap)文件。 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 ; -9kill删除; 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). 这会将TERM (inate)信号发送给Vim,允许它清理并删除交换文件(至少它在Ubuntu上为我做了)。

By using -9 / KILL , you don't give the process a chance to clean up. 通过使用-9 / KILL ,您不会给该过程提供清理的机会。 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. 然而,杀死像vim这样的交互式编辑器在很多层面上听起来都是错误的。 You should really consider not doing that. 你应该考虑不这样做。 In which situation is that desirable? 在哪种情况下是可取的?

You can add 你可以加

set noswapfile

to your vimrc. 到你的vimrc。 This will cause swap files to not be created in the first place. 这将导致首先不创建交换文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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