我可以使用 ale 或 syntastic 作为预写挂钩而不是 post-write 吗?

Can I use ale or syntastic as a pre-write hook instead of post-write?

我有几个必须始终包含有效 YAML 的 YAML 文件,所以我希望使用(类似)ale or syntastic 来帮助我执行此操作。

看起来 syntastic 的工作方式是在 :w 之后运行跳棋(post-写入)并报告任何错误。 Ale 异步工作,但如果出现错误则不会阻止写入。而且他们都像宣传的那样工作得很好。

但是如果文件没有通过某种预写挂钩中的检查器,我可以使用检查器来防止使用 :w:wq 将文件写入磁盘吗时尚?

或者是否有 ale 和 syntastic 的替代品可以做到这一点?

缓冲区仍然必须保留(例如保存到临时文件),以便检查器能够检查它。这是额外的努力,我不确定 Syntastic 是否有类似的东西。

对于ALE,因为它是异步工​​作的,所以可以很容易地做到这一点。剩下要做的就是在出现错误时阻止写入。可以通过 ale#statusline#Count(); you just need to check that in BufWritePre 获取错误并中止写入;这可以通过 :throw 在处理程序中设置异常来完成:

autocmd! BufWritePre <buffer> if ale#statusline#Count(bufnr('')).error > 0 | throw "You have errors in the file; fix them first." | endif

要将此自动应用到 "couple of YAML files",您可以定义自定义 :command(例如 :EnforceNoErrors),让您轻松设置 :autocmd(最好在内部:augroup 然后),或使用另一个 :autocmd 自动进行此设置:

:autocmd BufNewFile,BufRead /path/to/the/*.yaml autocmd! BufWritePre ...