vimpre-commit.com

Pre-commit results in vim


In Vim 8.1, I can run :!pre-commit run --files % to lint and auto-format the current file. Is there some way I can turn that (or any way of calling pre-commit) into in-editor highlighting, like https://github.com/dense-analysis/ale does?

In the past I've run ale in vim with pip-installed mypy, black, and yamllint and brew- or apt-installed shellcheck, delaying shell pre-commit runs until after editing. The downsides to that are in addition to the delayed feedback, this only brings a subset of the hooks into my editor (end-of-file-fixer is missing for example) and I haven't configured all of the twice-installed hooks as language: system, leaving the possibility open for in-editor and out-of-editor runs to diverge on versions and arguments. I could of course switch hooks over to language: system but I'm hoping for a way to go with the flow of using .pre-commit-config.yaml as the single-source-of-truth for versions and arguments, while surfacing results in-editor.


Solution

  • Excluding the highlighting part, this bare approach works:

    autocmd BufWritePost * silent! execute '! if [ -e $(git rev-parse --show-toplevel)/.pre-commit-config.yaml >/dev/null 2>&1 ]; then pre-commit run --files %; fi' | redraw!