简体   繁体   English

退出vim时自动运行脚本?

[英]Automatically run script when exiting vim?

I'm trying to keep track of my hours for work, and I have to keep very specific details of which files I was editing and when. 我正在努力跟踪我的工作时间,我必须详细记录我正在编辑哪些文件以及何时编辑。 In my .vimrc script, I have: 在我的.vimrc脚本中,我有:

:silent !clock_in_script.sh %:p

This clock_in_script.sh takes the location of the file as an argument and just writes the name of the file and time to a log. 此clock_in_script.sh将文件的位置作为参数,并将文件名和时间写入日志。

Is there a way to similarly invoke a script when I exit vim? 当我退出vim时有没有办法类似地调用脚本? If not, is there a way I can write the name of the file I'm editing and the time to a logfile from vim? 如果没有,有没有办法可以编写我正在编辑的文件的名称和从vim到日志文件的时间?

Thank you in advance! 先感谢您!

there is an event VimLeave (:h VimLeave) is for the requirement you described. 有一个事件VimLeave (:h VimLeave)是你所描述的要求。

However, this may not work for your needs. 但是,这可能无法满足您的需求。 Because you may open many files in one vim instance. 因为您可以在一个vim实例中打开许多文件。 I use vim always in this way. 我总是以这种方式使用vim。 In this case, you can only save the status of current buffer/file. 在这种情况下,您只能保存当前缓冲区/文件的状态。

You can :h event to check out the events vim's autocommand supports. 您可以:h event检查vim的autocommand支持的事件。 Also consider to write a small function to loop all buffers and call your command. 还要考虑编写一个小函数来循环所有缓冲区并调用命令。 or modify your shell script to accept more than one filename as parameter, so that you can call the script only once. 或修改您的shell脚本以接受多个文件名作为参数,以便您只能调用一次脚本。

Thank you to those of you who suggested autocmd events. 感谢那些建议autocmd事件的人。 The events that I have found to be most appropriate are BufWinEnter and BufWinLeave . 我发现最合适的事件是BufWinEnterBufWinLeave Here's exactly what I put in my vim: 这正是我放在我的vim中的内容:

:autocmd BufWinEnter * !clock_edit %:p
:autocmd BufWinLeave * !clock_close %:p

clock_edit bash script: clock_edit bash脚本:

#!/bin/bash
echo "`date`:: opened file $1" >> ~/.clock_log

clock_close: clock_close:

#!/bin/bash
echo "`date`:: closed file $1" >> ~/.

For example, when I open config.txt , vim will invoke clock_edit /node/proj/config.txt . 例如,当我打开config.txt ,vim将调用clock_edit /node/proj/config.txt If I open several files as tabs, each one I open will invoke clock_open and each one I close will invoke clock_close, with the appropriate filename and path passed as arguments. 如果我打开几个文件作为选项卡,我打开的每个文件将调用clock_open,我关闭的每个文件将调用clock_close,并将相应的文件名和路径作为参数传递。 Even better, it doesn't repeat the process when I switch tabs or window focus, just when I open and close the files. 更好的是,当我打开和关闭文件时切换标签或窗口焦点时,它不会重复此过程。

Thank you for all your help! 谢谢你的帮助!

edit: autocmd is a useful way of binding vim commands to events ( such as saving a file ) in vim. 编辑:autocmd是一种将vim命令绑定到vim中的事件(如保存文件)的有用方法。 For more info, read about autocmd here . 有关更多信息,请在此处阅读autocmd

Not sure exactly what you want to do, but you could maybe concoct something with autocommands, and the BufRead and BufWrite parameters. 不确定你想要做什么,但你可以用自动命令,BufRead和BufWrite参数来编写东西。 See here and also here . 看到这里也是这里

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

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