Black: Documentation for running Black in vim on save?

Created on 25 May 2018  路  8Comments  路  Source: psf/black

I've Googled vigorously for "how to run a vim command on save", but I haven't been able to figure this out. I've also searched this repository. Could you provide a blurb under the "vim" section?

Most helpful comment

I was able to get it to work in neovim by using the vim plugin provided here and putting this in my init.vim file (.vimrc if you're using vim)

autocmd BufWritePost *.py execute ':Black'

All 8 comments

I was able to get it to work in neovim by using the vim plugin provided here and putting this in my init.vim file (.vimrc if you're using vim)

autocmd BufWritePost *.py execute ':Black'

Thanks! Is there any way to elide the output:

"main.py" 2L, 22C written
Reformatted in 0.0049s.
Press ENTER or type command to continue

I'm open to a pull request that mentions that in the README.

@ambv Done.

autocmd BufWritePost *.py silent! execute ':Black' will suppress the output, but it suppresses errors too, which is probably not exactly what you want.

@dgouldin I don't mind that it suppresses errors, since I use syntastic, but the cursor doesn't stay at the same position (in the AST, it might well keep its same offset from the start of the file or whatever). This probably isn't the right issue to address this, but a good on-save story would probably help adoption; vim-go's support for gofmt could be used as a reference here.

I'm not sure how we would signal where the cursor should go after a save but it sounds like a sensible idea. I'd be open to a PR like this.

Same with a PR introducing :BlackQuiet that only outputs errors.

For anyone landing here in the future, the documentation is here: https://black.readthedocs.io/en/stable/editor_integration.html#vim

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Curly-Mo picture Curly-Mo  路  3Comments

decibyte picture decibyte  路  3Comments

quodlibetor picture quodlibetor  路  3Comments

brettcannon picture brettcannon  路  3Comments

layoaster picture layoaster  路  3Comments