防止在 BufWritePre 期间保存文件 [英] Prevent file saving during BufWritePre
问题描述
我有一个功能可以对当前文件执行有效性检查(以符合我雇主的编码标准).我想在保存之前调用这个函数,即使用 BufWritePre.但是,如果我的检查功能失败,我会阻止保存文件.
I have a function which performs validity checking on the current file (so as to conform to my employer's coding standards). I would like to call this function before saving, i.e. using BufWritePre. However, I would to prevent saving the file if it fails my checking function.
那么,是否有可能突破 BufWritePre 自动命令?
So, is it possible to break out of the BufWritePre autocommand?
我意识到我可以通过重新映射 :write
命令来实现这一点,如图所示 此处,但如果可能的话,我想避免这种情况,因为感觉有点不微妙.
I realise that I could accomplish this by re-mapping the :write
command as illustrated here, but I would like to avoid that if at all possible, as it feels somewhat un-subtle.
预先感谢您的建议.
推荐答案
你可以简单地"强制一个错误:
You can 'simply' force an error:
:autocmd BufWritePre *.txt throw "you may not"
如果您想再次保存 .txt 文件
If you wanted to be able to save .txt files again
:autocmd!
:source $MYVIMRC
这篇关于防止在 BufWritePre 期间保存文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!