All patches and comments are welcome. Please squash your changes to logical
commits before using git-format-patch and git-send-email to
patches@git.madduck.net.
If you'd read over the Git project's submission guidelines and adhered to them,
I'd be especially grateful.
5 Options include the following:
7 - [wbolster/emacs-python-black](https://github.com/wbolster/emacs-python-black)
8 - [proofit404/blacken](https://github.com/pythonic-emacs/blacken)
9 - [Elpy](https://github.com/jorgenschaefer/elpy).
11 ## PyCharm/IntelliJ IDEA
13 There are three different ways you can use _Black_ from PyCharm:
15 1. As local server using the BlackConnect plugin
19 The first option is the simplest to set up and formats the fastest (by spinning up
20 {doc}`Black's HTTP server </usage_and_configuration/black_as_a_server>`, avoiding the
21 startup cost on subsequent formats), but if you would prefer to not install a
22 third-party plugin or blackd's extra dependencies, the other two are also great options.
26 1. Install _Black_ with the `d` extra.
29 $ pip install 'black[d]'
33 [BlackConnect IntelliJ IDEs plugin](https://plugins.jetbrains.com/plugin/14321-blackconnect).
35 1. Open plugin configuration in PyCharm/IntelliJ IDEA
39 `PyCharm -> Preferences -> Tools -> BlackConnect`
41 On Windows / Linux / BSD:
43 `File -> Settings -> Tools -> BlackConnect`
45 1. In `Local Instance (shared between projects)` section:
47 1. Check `Start local blackd instance when plugin loads`.
48 1. Press the `Detect` button near `Path` input. The plugin should detect the `blackd`
51 1. In `Trigger Settings` section check `Trigger on code reformat` to enable code
52 reformatting with _Black_.
54 1. Format the currently opened file by selecting `Code -> Reformat Code` or using a
57 1. Optionally, to run _Black_ on every file save:
59 - In `Trigger Settings` section of plugin configuration check
60 `Trigger when saving changed files`.
70 1. Locate your `black` installation folder.
72 On macOS / Linux / BSD:
76 /usr/local/bin/black # possible location
83 %LocalAppData%\Programs\Python\Python36-32\Scripts\black.exe # possible location
86 Note that if you are using a virtual environment detected by PyCharm, this is an
87 unneeded step. In this case the path to `black` is `$PyInterpreterDirectory$/black`.
89 1. Open External tools in PyCharm/IntelliJ IDEA
93 `PyCharm -> Preferences -> Tools -> External Tools`
95 On Windows / Linux / BSD:
97 `File -> Settings -> Tools -> External Tools`
99 1. Click the + icon to add a new external tool with the following values:
102 - Description: Black is the uncompromising Python code formatter.
103 - Program: \<install_location_from_step_2>
104 - Arguments: `"$FilePath$"`
106 1. Format the currently opened file by selecting `Tools -> External Tools -> black`.
108 - Alternatively, you can set a keyboard shortcut by navigating to
109 `Preferences or Settings -> Keymap -> External Tools -> External Tools - Black`.
119 1. Locate your `black` installation folder.
121 On macOS / Linux / BSD:
125 /usr/local/bin/black # possible location
132 %LocalAppData%\Programs\Python\Python36-32\Scripts\black.exe # possible location
135 Note that if you are using a virtual environment detected by PyCharm, this is an
136 unneeded step. In this case the path to `black` is `$PyInterpreterDirectory$/black`.
138 1. Make sure you have the
139 [File Watchers](https://plugins.jetbrains.com/plugin/7177-file-watchers) plugin
141 1. Go to `Preferences or Settings -> Tools -> File Watchers` and click `+` to add a new
145 - Scope: Project Files
146 - Program: \<install_location_from_step_2>
147 - Arguments: `$FilePath$`
148 - Output paths to refresh: `$FilePath$`
149 - Working directory: `$ProjectFileDir$`
151 - In Advanced Options
152 - Uncheck "Auto-save edited files to trigger the watcher"
153 - Uncheck "Trigger the watcher on external changes"
157 Wing IDE supports `black` via **Preference Settings** for system wide settings and
158 **Project Properties** for per-project or workspace specific settings, as explained in
159 the Wing documentation on
160 [Auto-Reformatting](https://wingware.com/doc/edit/auto-reformatting). The detailed
165 - Wing IDE version 8.0+
173 - Make sure it runs from the command line, e.g.
179 ### Preference Settings
181 If you want Wing IDE to always reformat with `black` for every project, follow these
184 1. In menubar navigate to `Edit -> Preferences -> Editor -> Reformatting`.
186 1. Set **Auto-Reformat** from `disable` (default) to `Line after edit` or
187 `Whole files before save`.
189 1. Set **Reformatter** from `PEP8` (default) to `Black`.
191 ### Project Properties
193 If you want to just reformat for a specific project and not intervene with Wing IDE
194 global setting, follow these steps:
196 1. In menubar navigate to `Project -> Project Properties -> Options`.
198 1. Set **Auto-Reformat** from `Use Preferences setting` (default) to `Line after edit`
199 or `Whole files before save`.
201 1. Set **Reformatter** from `Use Preferences setting` (default) to `Black`.
207 Commands and shortcuts:
209 - `:Black` to format the entire file (ranges not supported);
210 - you can optionally pass `target_version=<version>` with the same values as in the
212 - `:BlackUpgrade` to upgrade _Black_ inside the virtualenv;
213 - `:BlackVersion` to get the current version of _Black_ in use.
217 - `g:black_fast` (defaults to `0`)
218 - `g:black_linelength` (defaults to `88`)
219 - `g:black_skip_string_normalization` (defaults to `0`)
220 - `g:black_skip_magic_trailing_comma` (defaults to `0`)
221 - `g:black_virtualenv` (defaults to `~/.vim/black` or `~/.local/share/nvim/black`)
222 - `g:black_use_virtualenv` (defaults to `1`)
223 - `g:black_target_version` (defaults to `""`)
224 - `g:black_quiet` (defaults to `0`)
225 - `g:black_preview` (defaults to `0`)
229 This plugin **requires Vim 7.0+ built with Python 3.7+ support**. It needs Python 3.7 to
230 be able to run _Black_ inside the Vim process which is much faster than calling an
235 To install with [vim-plug](https://github.com/junegunn/vim-plug):
237 _Black_'s `stable` branch tracks official version updates, and can be used to simply
238 follow the most recent stable version.
241 Plug 'psf/black', { 'branch': 'stable' }
244 Another option which is a bit more explicit and offers more control is to use
245 `vim-plug`'s `tag` option with a shell wildcard. This will resolve to the latest tag
246 which matches the given pattern.
248 The following matches all stable versions (see the
249 [Release Process](../contributing/release_process.md) section for documentation of
250 version scheme used by Black):
253 Plug 'psf/black', { 'tag': '*.*.*' }
256 and the following demonstrates pinning to a specific year's stable style (2022 in this
260 Plug 'psf/black', { 'tag': '22.*.*' }
265 or with [Vundle](https://github.com/VundleVim/Vundle.vim):
271 and execute the following in a terminal:
274 $ cd ~/.vim/bundle/black
275 $ git checkout origin/stable -b stable
280 On Arch Linux, the plugin is shipped with the
281 [`python-black`](https://archlinux.org/packages/community/any/python-black/) package, so
282 you can start using it in Vim after install with no additional setup.
284 ##### Vim 8 Native Plugin Management
286 or you can copy the plugin files from
287 [plugin/black.vim](https://github.com/psf/black/blob/stable/plugin/black.vim) and
288 [autoload/black.vim](https://github.com/psf/black/blob/stable/autoload/black.vim).
291 mkdir -p ~/.vim/pack/python/start/black/plugin
292 mkdir -p ~/.vim/pack/python/start/black/autoload
293 curl https://raw.githubusercontent.com/psf/black/stable/plugin/black.vim -o ~/.vim/pack/python/start/black/plugin/black.vim
294 curl https://raw.githubusercontent.com/psf/black/stable/autoload/black.vim -o ~/.vim/pack/python/start/black/autoload/black.vim
297 Let me know if this requires any changes to work with Vim 8's builtin `packadd`, or
302 On first run, the plugin creates its own virtualenv using the right Python version and
303 automatically installs _Black_. You can upgrade it later by calling `:BlackUpgrade` and
306 If you need to do anything special to make your virtualenv work and install _Black_ (for
307 example you want to run a version from main), create a virtualenv manually and point
308 `g:black_virtualenv` to it. The plugin will use it.
310 If you would prefer to use the system installation of _Black_ rather than a virtualenv,
311 then add this to your vimrc:
314 let g:black_use_virtualenv = 0
317 Note that the `:BlackUpgrade` command is only usable and useful with a virtualenv, so
318 when the virtualenv is not in use, `:BlackUpgrade` is disabled. If you need to upgrade
319 the system installation of _Black_, then use your system package manager or pip--
320 whatever tool you used to install _Black_ originally.
322 To run _Black_ on save, add the following lines to `.vimrc` or `init.vim`:
325 augroup black_on_save
327 autocmd BufWritePre *.py Black
331 To run _Black_ on a key press (e.g. F9 below), add this:
334 nnoremap <F9> :Black<CR>
339 1. Install [`ale`](https://github.com/dense-analysis/ale)
343 1. Add this to your vimrc:
346 let g:ale_fixers = {}
347 let g:ale_fixers.python = ['black']
352 gedit is the default text editor of the GNOME, Unix like Operating Systems. Open gedit
359 1. `Go to edit > preferences > plugins`
360 1. Search for `external tools` and activate it.
361 1. In `Tools menu -> Manage external tools`
362 1. Add a new tool using `+` button.
363 1. Copy the below content to the code window.
367 Name=$GEDIT_CURRENT_DOCUMENT_NAME
371 - Set a keyboard shortcut if you like, Ex. `ctrl-B`
374 - Output: `Display in bottom pane` if you like.
375 - Change the name of the tool if you like.
377 Use your keyboard shortcut or `Tools -> External Tools` to use your new tool. When you
378 close and reopen your File, _Black_ will be done with its job.
380 ## Visual Studio Code
383 [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python)
384 ([instructions](https://code.visualstudio.com/docs/python/editing#_formatting)).
386 - Alternatively the pre-release
387 [Black Formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter)
388 extension can be used which runs a [Language Server Protocol](https://langserver.org/)
389 server for Black. Formatting is much more responsive using this extension, **but the
390 minimum supported version of Black is 22.3.0**.
394 Use [sublack plugin](https://github.com/jgirardet/sublack).
398 If your editor supports the [Language Server Protocol](https://langserver.org/) (Atom,
399 Sublime Text, Visual Studio Code and many more), you can use the
400 [Python LSP Server](https://github.com/python-lsp/python-lsp-server) with the
401 [python-lsp-black](https://github.com/python-lsp/python-lsp-black) plugin.
405 Use [python-black](https://atom.io/packages/python-black) or
406 [formatters-python](https://atom.io/packages/formatters-python).
408 ## Gradle (the build tool)
410 Use the [Spotless](https://github.com/diffplug/spotless/tree/main/plugin-gradle) plugin.
414 Add the following hook to your kakrc, then run _Black_ with `:format`.
417 hook global WinSetOption filetype=python %{
418 set-option window formatcmd 'black -q -'
424 Use [Thonny-black-code-format](https://github.com/Franccisco/thonny-black-code-format).