How to install or upgrade Black in Vim 8 and format when saving?

python

This method of installing Black uses the built-in plugin management in Vim 8+.

sudo rm -rf ~/.vim/black
mkdir -p ~/.vim/pack/start/black/plugin
curl https://raw.githubusercontent.com/psf/black/21.6b0/plugin/black.vim -o ~/.vim/pack/start/black/plugin/black.vim

Next time you open Vim, Black will install.

Add the following to ~/.vimrc to format with Black every time you save.

autocmd BufWritePre *.py execute ':Black'