Home > vim > Install black on VIM

Install black on VIM

What is black?

Black is the uncompromising Python code formatter. By using it, you agree to cede control over minutiae of hand-formatting. In return, Black gives you speed, determinism, and freedom from pycodestyle nagging about formatting. You will save time and mental energy for more important matters.

Blackened code looks the same regardless of the project you’re reading. Formatting becomes transparent after a while and you can focus on the content instead.

Black makes code review faster by producing the smallest diffs possible.

From https://github.com/psf/black

Installation steps
1. Deactivate any active virtual environments you may have running.

$ deactivate

Failure to do may lead to the following errors after completing all the installation steps on the github page.

When running :Black

Traceback (most recent call last):
  File "", line 1, in 
NameError: name 'Black' is not defined

opening a *.py file

Error detected while processing ~/.vim/plugged/black/plugin/black.vim:
line  124:
Traceback (most recent call last):
  File "", line 56, in 
ModuleNotFoundError: No module named 'black'

2. Install a plugin manager. Eg Vundle

$ git clone https://github.com/VundleVim/Vundle.vim.git ~/.vim/bundle/Vundle.vim

3. Edit .vimrc to install plugin.
With Vundle

$ less .vimrc
set nocompatible              " be iMproved, required
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()

" let Vundle manage Vundle, required
Plugin 'VundleVim/Vundle.vim'
Plugin 'psf/black'

" All of your Plugins must be added before the following line
call vundle#end()            " required
filetype plugin indent on    " required

" Specify the virtual environment path for black
let g:black_virtualenv='~/.vim/black'
" Run black on save
autocmd BufWritePre *.py execute ':Black'

4. Install the black plugin

$ vim test.py
:PluginInstall

You may get the following error

Done!
Please wait, one time setup for Black.
Creating a virtualenv in /home/vagrant/.vim/black...
(this path can be customized in .vimrc by setting g:black_virtualenv)
The virtual environment was not created successfully because ensurepip is not
available.  On Debian/Ubuntu systems, you need to install the python3-venv
package using the following command.
    apt-get install python3-venv
You may need to use sudo with that command.  After installing the python3-venv
package, recreate your virtual environment.
Failing command: ['/home/vagrant/.vim/black/bin/python3', '-Im', 'ensurepip', '--upgrade', '--default-pip']
Error detected while processing /home/vagrant/.vim/bundle/black/plugin/black.vim:
line  194:
E880: Can't handle SystemExit of python exception in vim
Press ENTER or type command to continue

5. Install python3-venv

$ sudo apt-get install python3-venv

6. Recreate the virtual environment

$ rm -rf .vim/black
$ vim test.py
vim test.py
Please wait, one time setup for Black.
Creating a virtualenv in /home/vagrant/.vim/black...
(this path can be customized in .vimrc by setting g:black_virtualenv)
Installing Black with pip...
DONE! You are all set, thanks for waiting ✨ 🍰 ✨
Pro-tip: to upgrade Black in the future, use the :BlackUpgrade command and restart Vim.
Press ENTER or type command to continue

7. Last step of installation

vagrant@vagrant:~$ cd .vim/bundle/black/
vagrant@vagrant:~/.vim/bundle/black$ git checkout origin/stable -b stable
Branch 'stable' set up to track remote branch 'stable' from 'origin'.
Switched to a new branch 'stable'
vagrant@vagrant:~/.vim/bundle/black$ cd ~

8. Test black
Add the following to test.py and write (:w)

j = [1,
2,
3
]

Black should reformat the file in place and give you

j = [1, 2, 3]

You may get the following error

Error detected while processing BufWrite Auto commands for "*.py":
Traceback (most recent call last):
  File "", line 1, in 
  File "", line 92, in Black
  File "", line 128, in get_configs
"test.py" [New] 5L, 26C written
Press ENTER or type command to continue

Try running :Black inside test.py to see the actual error

Traceback (most recent call last):
  File "", line 1, in 
  File "", line 92, in Black
  File "", line 128, in get_configs
AttributeError: module 'black' has no attribute 'find_pyproject_toml'

The above error may occur if you skipped step 7. You could also run https://github.com/psf/black/pull/1273#issuecomment-594114067

System details
– Ubuntu 18.04.3 LTS
– VIM 8.0 with python3 support
vim test.py
:echo has(‘python3’)
1

Source:
https://github.com/psf/black#installation-and-usage
https://github.com/psf/black/issues/672
https://github.com/psf/black/pull/1273

Categories: vim Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: