Autoformat Python file with Black after saving in Vim

Edit

Published on March 29, 2022

Author: Meet Rajesh Gor

#vim #python

If you are like me who writes Python very badly, it has empty lines with whitespaces, no proper format in assigning variables, not formatted according to PEP 8 standards, and you use Vim as your text editor then my friend you need a autocmd badly for it.

Install Black in Python

Install the black package in python globally or locally as per your preferences.


pip install black

OR with pipx


pipx install black

For a detailed guide about running packages with pipx head toward my article on pipx.

Set up Autocmd in Vim

We can set up a autocmd. What is a autocmd? It is about running commands when certain events occur like running a command when a file is saved, a buffer is opened or closed, and so on. What we want is to run the black command from the shell when the current file is saved.

So, we can create a autocmd as follows:


vimscript
autocmd BufWritePost * !black %

Now, this will run when any type of file is saved, so we will make it specific to python by adding a *.py to add in the autocmd.


vimscript
autocmd BufWritePost *.py !black %

This works, but it gives a prompt after the command has been executed, to run the command silently we can simply add the silent keyword before the execution of the command from the shell.


vimscript
autocmd BufWritePost *.py silent !black %

This looks perfect!

But still, we need to add a auto-group(augroup) that groups the autocmds and by adding autocmd! it will clear all the commands from the group.


vimscript
augroup python_format
    autocmd!
    autocmd BufWritePost *.py silent !black %
augroup end

We can now add it to the vimrc to work all the time.

Using pipx

If you have used pipx to install black, you need to setup the autocmd a bit differently.


vimscript
autocmd BufWritePost *.py silent !pipx run black %

It might be a bit slower than running with global installation, but it is a neat way to run python package.

So, that's it we can now write clean and safe python code without breaking a sweat in Vim. Happy Coding :)

<a class='prev' href='/sqlite-inline-custom-separator'>

    <svg width="50px" height="50px" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M13.5 8.25L9.75 12L13.5 15.75" stroke="var(--prevnext-color-angle)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"> </path>
    </svg>
    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>prev</p>
        <p class='prevnext-title'>SQLite importing CSV with custom separator</p>
    </div>
</a>

<a class='next' href='/golang-test-output-json'>

    <div class='prevnext-text'>
        <p class='prevnext-subtitle'>next</p>
        <p class='prevnext-title'>Golang: Test Output JSON</p>
    </div>
    <svg width="50px" height="50px" viewbox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
        <path d="M10.5 15.75L14.25 12L10.5 8.25" stroke="var(--prevnext-color-angle)" stroke-width="1.5" stroke-linecap="round" stroke-linejoin="round"></path>
    </svg>
</a>