在 :wq - vimscript 上执行命令的 autocmd 事件?
autocmd event to execute a command on :wq - vimscript?
我想用:wq
退出vim时执行system("cp /home/currently_opened_file.txt /somewhere/else")
。是否有 autocmd 事件?或者有其他方法吗?
更新:
OP 在评论中指出,此组合完全符合要求(仅在 :wq
上执行命令)。
:autocmd BufWritePost * :autocmd VimLeave * :!cp % /somewhere/else
原回答:
您可以挂钩 BufWritePost
事件。这将 运行 上的命令每次 写入,而不仅仅是当您使用 :wq
离开文件时。
:autocmd BufWritePost * :!cp % /somewhere/else
我想你可以尝试挂钩 BufDelete
事件 (在从缓冲区列表中删除缓冲区之前),但这似乎会有问题,因为缓冲区不仅仅用于文件编辑器。它们还用于快速列表、帮助查看器等。
有些事件会在您戒烟时发生,这可能是一种选择。
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
查看完整列表。
另请注意,您可以限制匹配事件的内容。例如,如果您只希望 HTML 文件和 CSS 文件发生这种情况,您可以使用:
:autocmd QuitPre *.html,*.css :!cp % /somewhere/else
我想您需要进行试验,看看什么对您有用。
您似乎需要将文件的写入自动级联到另一个位置。我的 DuplicateWrite plugin 提供了舒适的命令来设置这样的。 (插件页面有指向其他插件的链接。)
:DuplicateWrite /somewhere/else
我想用:wq
退出vim时执行system("cp /home/currently_opened_file.txt /somewhere/else")
。是否有 autocmd 事件?或者有其他方法吗?
更新:
OP 在评论中指出,此组合完全符合要求(仅在 :wq
上执行命令)。
:autocmd BufWritePost * :autocmd VimLeave * :!cp % /somewhere/else
原回答:
您可以挂钩 BufWritePost
事件。这将 运行 上的命令每次 写入,而不仅仅是当您使用 :wq
离开文件时。
:autocmd BufWritePost * :!cp % /somewhere/else
我想你可以尝试挂钩 BufDelete
事件 (在从缓冲区列表中删除缓冲区之前),但这似乎会有问题,因为缓冲区不仅仅用于文件编辑器。它们还用于快速列表、帮助查看器等。
有些事件会在您戒烟时发生,这可能是一种选择。
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
查看完整列表。
另请注意,您可以限制匹配事件的内容。例如,如果您只希望 HTML 文件和 CSS 文件发生这种情况,您可以使用:
:autocmd QuitPre *.html,*.css :!cp % /somewhere/else
我想您需要进行试验,看看什么对您有用。
您似乎需要将文件的写入自动级联到另一个位置。我的 DuplicateWrite plugin 提供了舒适的命令来设置这样的。 (插件页面有指向其他插件的链接。)
:DuplicateWrite /somewhere/else