是否可以让 vim 阻止保存有解析错误的 php 文件?
我使用 vim 并希望它能够阻止我保存有解析错误的 php 文件。如果我想使用例如“php -l <file>”为了实现这一点,.vimrc 中的 autocmd 会是什么样子?
我知道我可以用类似的东西挂接到 BufWritePre
autocmd FileType php autocmd BufWritePre * !php -l %
,但我希望它在 php 命令失败时中止“写入”。
I use vim and would like it to prevent me from saving php files that has parse errors. If I would like to use for instance "php -l <file>" to achieve this, how would the autocmd in .vimrc look like?
I know I can hook into BufWritePre with something like
autocmd FileType php autocmd BufWritePre * !php -l %
but I want it to abort the "write" if the php command fails.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你可以在钩子中抛出一个未捕获的异常,它会中止 write: try
另请注意
:!shell_command %
因为这里 vim 没有进行正确的转义,如果文件名包含特殊符号,如空格、引号、美元、换行符...
代替第二个模式中的*
。您的原始命令没有为 php 文件定义自动命令,而是为所有文件类型定义自动命令(如果文件类型之一是 php)。:autocmd!
s:它们可以防止重复的自动命令。顺便说一句,你想通过在旧版本的文件上运行 php 来实现什么(它是 BufWritePre 事件,所以它发生在文件写入之前)?也许你应该用
:w !php -l </code>、
:w !php -l -
甚至:w !php -l /dev 替换运行 php 的代码/stdin
取决于 php 接受的内容(我的系统上没有并且不想有)。You can throw an uncaught exception in the hook and it will abort write: try
Also note
:!shell_command %
as here vim does not do proper escaping and you will run into troubles if file name contains special symbols like spaces, quotes, dollar, newlines, ...<buffer>
in place of*
in second pattern. Your original command does not define autocommand for php files, it instead defines autocommand for all filetypes if one of the filetypes is php.:autocmd!
s: they prevent duplicating autocommands.By the way, what do you want to achieve by running php on old version of file (it is BufWritePre event, so it happens before file is written)? Maybe you should replace code that runs php with
:w !php -l
,:w !php -l -
or even:w !php -l /dev/stdin
depending on what php accepts (I do not have it on my system and do not want to have).据我所知,VIM 不可能做到这一点。文档也没有说明这种功能,所以我很确定,如果不进行破解,这是无法完成的。问题是你想要实现什么目标?我有一个脚本,可以确保在提交版本控制之前(而不是保存文件时)在 PHP 中没有解析错误。这是一个简单的预提交挂钩,也许可以工作?
如果您确实对保存文件之前的 linting 感兴趣,您可以将“ZZ”和“:w”重写为您自己编写的命令,这反过来会对文件进行 lint(如果是 PHP 文件)并没有错误时保存。我不确定您是否可以重新映射 :w,但它可能会导致比它解决的问题更多的问题。
也许更简单的解决方案是在您自己选择的击键(例如 F5)上创建一个命令,并使其执行您想要的操作?
As far as I know, this isn't possible with VIM. Documentation doesn't state this capability either, so I'm pretty sure that can't be done, without a hack. The question is though what you're trying to accomplish? I have a script that makes sure I have no parse errors in PHP before I commit to version control, instead of when I save the file. That's a simple pre-commit hook, perhaps that something that'll work?
If you're really interested in linting before you save the file, you might be able to rewrite "ZZ" and ":w" to a command you wrote yourself, that in turns lints the file (in case of a PHP file) and saves upon no errors. I'm not sure if you can remap the :w, and it might cause more issues than it solves though.
Perhaps a simpler solution would be to create a command on a keystroke you choose yourself (e.g. F5), and make that do what you want?