在 :wq 上执行命令的 autocmd 事件 - vimscript? [英] autocmd event to execute a command on :wq - vimscript?

查看:22
本文介绍了在 :wq 上执行命令的 autocmd 事件 - vimscript?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我用 :wq 退出 vim 时,我想执行 system("cp/home/currently_opened_file.txt/somewhere/else").是否有一个 autocmd 事件?或者有什么其他的方法吗?

I want to execute system("cp /home/currently_opened_file.txt /somewhere/else") when I exit vim with :wq. Is there an autocmd event for that? Or any other way to do it?

推荐答案

更新:

OP 在评论中指出这种组合完全符合预期(仅在 :wq 上执行命令).

The OP noted in comments that this combination did exactly what was wanted (execute the command only on :wq).

:autocmd BufWritePost * :autocmd VimLeave * :!cp % /somewhere/else

原答案:

您可以挂钩 BufWritePost 事件.这将在每次写入时运行该命令,而不仅仅是在您使用 :wq 离开文件时.

You can hook the BufWritePost event. This will run the command on every write, not only when you use :wq to leave the file.

:autocmd BufWritePost * :!cp % /somewhere/else

我想您可以尝试挂钩 BufDelete 事件 (在从缓冲区列表中删除缓冲区之前),但这似乎是有问题的,因为使用了缓冲区不仅仅是文件编辑器.它们还用于快速列表、帮助查看器等.

I suppose you could try hooking the BufDelete event (before deleting a buffer from the buffer list), but that seems like it would be problematic, as buffers are used for more than file editors. They are also used for things like quicklists, the help viewer, etc.

在您戒烟时会发生一些事件,这可能是一种选择.

There are some events that take place when you are quitting, which could be an option.

QuitPre        when using :quit, before deciding whether to quit
VimLeavePre    before exiting Vim, before writing the viminfo file
VimLeave       before exiting Vim, after writing the viminfo file

您可以使用 :help autocmd-events 查看完整列表.

You can see the full list using :help autocmd-events.

另请注意,您可以限制与事件匹配的内容.例如,如果您只希望 HTML 文件和 CSS 文件发生这种情况,您可以使用:

Also note that you can restrict what matches the event. For instance, if you only want this to happen for HTML files and CSS files, you could use this:

:autocmd QuitPre *.html,*.css :!cp % /somewhere/else

我怀疑您需要进行试验,看看什么对您有用.

I suspect you will need to experiment and see what works for you.

这篇关于在 :wq 上执行命令的 autocmd 事件 - vimscript?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆