It looks like the Black Python code formatter intentionally does not allow you to configure whether to use 2 or 4 spaces,
which makes me wonder why, I can't imagine this change requiring anything other than a minimal change to the code,
the lack of the explanation to why they are not and won't support it makes me think that it is not a matter of religiously sticking to a subset of PEP8 (which recommended 4 spaces, in 2001) but a matter of enforcing their personal preference on the world.
It looks like the forks that implemented this feature are abandoned or not as maintained as the upstream repository.
So I'd recommend to keep using this plugin and use a simple command that replaces each 4 spaces at the beginning of each line with 2. which its implementation turned out to be suspiciously simple:
import pynvim
import re
@pynvim.plugin
class Iindent(object):
""" Iindent plugin: Black can't stop me from using 2 spaces """
def __init__(self, nvim):
self.nvim = nvim
self.pattern = re.compile(r"^(\s\s\s\s)+")
@pynvim.command("Iindent", nargs="*", range="")
def iindent(self, args, range):
buffer = self.nvim.current.buffer
new_buffer = []
for line in buffer:
x = self.pattern.search(line)
if x:
_, end = x.span()
line = re.sub(self.pattern, " " * int(end / 2), line)
new_buffer += [line]
print(line)
buffer[:] = new_buffer
it uses and depends on pynvim, install it with pip pip install --user pynvim
or better yet install it with your distro's package manager which upgrades it with vim automatically for you, I use Arch so I installed it with Sudo pacman -S python-pynvim
make a directory for this new plugin in the plugged directory which is in your vim config directory:
mkdir -p ~/.vim/plugged/iindent/rplugin/python3
make a new python file in it:
touch ~/.vim/plugged/iindent/rplugin/python3/iindent.py
and add to it the code above, then install with vimplug/your-plugin-manager by adding Plug '~/.vim/plugged/iindent'
to your vim config and running :UpdateRemotePlugins
then change your <leader>f
keybinding command to run this command after Black:
autocmd FileType python nmap <leader>f :Black<CR> :Iindent<CR>
a down-side to this approach is that it adds an extra indentation change to history in-addition-to/after the formatter change, however the upside is that you keep using the maintained plugin and formatter.