This is a stripped down version of the official Black plugin for Vim that does not handle external dependencies for you.
Black
is the uncompromising Python code formatter created by Łukasz Langa.
That repository also features a Vim plugin
that provides the Black
command.
It formats the current file
according to the settings found in
either your Vim configuration files
or a toml
file inside your project.
The problem with having a Vim plugin in the Black repository
is that Vim plugin managers clone the entire project
which may not necessarily be what you want.
It also installs Black automatically
in a virtual environment inside your .vim
directory.
So I decided to make a "fork"
that does not do any of that and assumes
you already installed Black in a virtual environment compatible
with the python interpreter used by your Vim.
Just assign g:black_virtualenv
to the virtual environment directory containing Black.
For example:
let g:black_virtualenv = '~/.virtualenvs/black'
Consider using pipx to install Black in a dedicated virtual environment.
pipx install black
:[range]Black
to format the selected lines:BlackVersion
to get the currently used version of Black
This plugin exposes two internal mappings (see :h <Plug>
).
A good place to put the following mappings is in .vim/after/ftplugin/python.vim
.
Add an operator to apply Black on :h motion
.
For example, try <space>bip
.
nmap <buffer> <space>b <Plug>(Blackify)
Apply Black on the current line:
nmap <buffer> <space>bb <Plug>(BlackifyLine)
Your pyproject.toml config file will be discovered automatically. Failing to do that, the plugin will try using the following options.
g:black_fast
(defaults to0
)g:black_linelength
(defaults to88
)g:black_string_normalization
(defaults to1
)