Debian Codegraph @

docs/integrations/editors.md

raw
1 # Editor integration
2
3 ## Emacs
4
5 Options include the following:
6
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).
10
11 ## PyCharm/IntelliJ IDEA
12
13 There are several different ways you can use _Black_ from PyCharm:
14
15 1. Using the built-in _Black_ integration (PyCharm 2023.2 and later). This option is the
16 simplest to set up.
17 1. As local server using the BlackConnect plugin. This option formats the fastest. It
18 spins up {doc}`Black's HTTP server </usage_and_configuration/black_as_a_server>`, to
19 avoid the startup cost on subsequent formats.
20 1. As external tool.
21 1. As file watcher.
22
23 ### Built-in _Black_ integration
24
25 1. Install `black`.
26
27 ```console
28 $ pip install black
29 ```
30
31 1. Go to `Preferences or Settings -> Tools -> Black` and configure _Black_ to your
32 liking.
33
34 ### As local server
35
36 1. Install _Black_ with the `d` extra.
37
38 ```console
39 $ pip install 'black[d]'
40 ```
41
42 1. Install
43 [BlackConnect IntelliJ IDEs plugin](https://plugins.jetbrains.com/plugin/14321-blackconnect).
44
45 1. Open plugin configuration in PyCharm/IntelliJ IDEA
46
47 On macOS:
48
49 `PyCharm -> Preferences -> Tools -> BlackConnect`
50
51 On Windows / Linux / BSD:
52
53 `File -> Settings -> Tools -> BlackConnect`
54
55 1. In `Local Instance (shared between projects)` section:
56 1. Check `Start local blackd instance when plugin loads`.
57 1. Press the `Detect` button near `Path` input. The plugin should detect the `blackd`
58 executable.
59
60 1. In `Trigger Settings` section check `Trigger on code reformat` to enable code
61 reformatting with _Black_.
62
63 1. Format the currently opened file by selecting `Code -> Reformat Code` or using a
64 shortcut.
65
66 1. Optionally, to run _Black_ on every file save:
67 - In `Trigger Settings` section of plugin configuration check
68 `Trigger when saving changed files`.
69
70 ### As external tool
71
72 1. Install `black`.
73
74 ```console
75 $ pip install black
76 ```
77
78 1. Locate your `black` installation folder.
79
80 On macOS / Linux / BSD:
81
82 ```console
83 $ which black
84 /usr/local/bin/black # possible location
85 ```
86
87 On Windows:
88
89 ```console
90 $ where black
91 C:\Program Files\Python313\Scripts\black.exe # possible location
92 ```
93
94 Note that if you are using a virtual environment detected by PyCharm, this is an
95 unneeded step. In this case the path to `black` is `$PyInterpreterDirectory$/black`.
96
97 1. Open External tools in PyCharm/IntelliJ IDEA
98
99 On macOS:
100
101 `PyCharm -> Preferences -> Tools -> External Tools`
102
103 On Windows / Linux / BSD:
104
105 `File -> Settings -> Tools -> External Tools`
106
107 1. Click the + icon to add a new external tool with the following values:
108 - Name: Black
109 - Description: Black is the uncompromising Python code formatter.
110 - Program: \<install_location_from_step_2>
111 - Arguments: `"$FilePath$"`
112
113 1. Format the currently opened file by selecting `Tools -> External Tools -> black`.
114 - Alternatively, you can set a keyboard shortcut by navigating to
115 `Preferences or Settings -> Keymap -> External Tools -> External Tools - Black`.
116
117 ### As file watcher
118
119 1. Install `black`.
120
121 ```console
122 $ pip install black
123 ```
124
125 1. Locate your `black` installation folder.
126
127 On macOS / Linux / BSD:
128
129 ```console
130 $ which black
131 /usr/local/bin/black # possible location
132 ```
133
134 On Windows:
135
136 ```console
137 $ where black
138 C:\Program Files\Python313\Scripts\black.exe # possible location
139 ```
140
141 Note that if you are using a virtual environment detected by PyCharm, this is an
142 unneeded step. In this case the path to `black` is `$PyInterpreterDirectory$/black`.
143
144 1. Make sure you have the
145 [File Watchers](https://plugins.jetbrains.com/plugin/7177-file-watchers) plugin
146 installed.
147 1. Go to `Preferences or Settings -> Tools -> File Watchers` and click `+` to add a new
148 watcher:
149 - Name: Black
150 - File type: Python
151 - Scope: Project Files
152 - Program: \<install_location_from_step_2>
153 - Arguments: `$FilePath$`
154 - Output paths to refresh: `$FilePath$`
155 - Working directory: `$ProjectFileDir$`
156
157 - In Advanced Options
158 - Uncheck "Auto-save edited files to trigger the watcher"
159 - Uncheck "Trigger the watcher on external changes"
160
161 ## Wing IDE
162
163 Wing IDE supports `black` via **Preference Settings** for system wide settings and
164 **Project Properties** for per-project or workspace specific settings, as explained in
165 the Wing documentation on
166 [Auto-Reformatting](https://wingware.com/doc/edit/auto-reformatting). The detailed
167 procedure is:
168
169 ### Prerequisites
170
171 - Wing IDE version 8.0+
172
173 - Install `black`.
174
175 ```console
176 $ pip install black
177 ```
178
179 - Make sure it runs from the command line, e.g.
180
181 ```console
182 $ black --help
183 ```
184
185 ### Preference Settings
186
187 If you want Wing IDE to always reformat with `black` for every project, follow these
188 steps:
189
190 1. In menubar navigate to `Edit -> Preferences -> Editor -> Reformatting`.
191
192 1. Set **Auto-Reformat** from `disable` (default) to `Line after edit` or
193 `Whole files before save`.
194
195 1. Set **Reformatter** from `PEP8` (default) to `Black`.
196
197 ### Project Properties
198
199 If you want to just reformat for a specific project and not intervene with Wing IDE
200 global setting, follow these steps:
201
202 1. In menubar navigate to `Project -> Project Properties -> Options`.
203
204 1. Set **Auto-Reformat** from `Use Preferences setting` (default) to `Line after edit`
205 or `Whole files before save`.
206
207 1. Set **Reformatter** from `Use Preferences setting` (default) to `Black`.
208
209 ## Vim
210
211 ### Official plugin
212
213 Commands and shortcuts:
214
215 - `:Black` to format the entire file (ranges not supported);
216 - you can optionally pass `target_version=<version>` with the same values as in the
217 command line.
218 - `:BlackUpgrade` to upgrade _Black_ inside the virtualenv;
219 - `:BlackVersion` to get the current version of _Black_ in use.
220
221 Configuration:
222
223 - `g:black_fast` (defaults to `0`)
224 - `g:black_linelength` (defaults to `88`)
225 - `g:black_skip_string_normalization` (defaults to `0`)
226 - `g:black_skip_magic_trailing_comma` (defaults to `0`)
227 - `g:black_virtualenv` (defaults to `~/.vim/black` or `~/.local/share/nvim/black`)
228 - `g:black_use_virtualenv` (defaults to `1`)
229 - `g:black_target_version` (defaults to `""`)
230 - `g:black_quiet` (defaults to `0`)
231 - `g:black_preview` (defaults to `0`)
232
233 #### Installation
234
235 This plugin **requires Vim 7.0+ built with Python 3.10+ support**. It needs Python 3.10
236 to be able to run _Black_ inside the Vim process which is much faster than calling an
237 external command.
238
239 ##### `vim-plug`
240
241 To install with [vim-plug](https://github.com/junegunn/vim-plug):
242
243 _Black_'s `stable` branch tracks official version updates, and can be used to simply
244 follow the most recent stable version.
245
246 ```vim
247 Plug 'psf/black', { 'branch': 'stable' }
248 ```
249
250 Another option which is a bit more explicit and offers more control is to use
251 `vim-plug`'s `tag` option with a shell wildcard. This will resolve to the latest tag
252 which matches the given pattern.
253
254 The following matches all stable versions (see the
255 [Release Process](../contributing/release_process.md) section for documentation of
256 version scheme used by Black):
257
258 ```vim
259 Plug 'psf/black', { 'tag': '*.*.*' }
260 ```
261
262 and the following demonstrates pinning to a specific year's stable style (2026 in this
263 case):
264
265 ```vim
266 Plug 'psf/black', { 'tag': '26.*.*' }
267 ```
268
269 ##### Vundle
270
271 or with [Vundle](https://github.com/VundleVim/Vundle.vim):
272
273 ```vim
274 Plugin 'psf/black'
275 ```
276
277 and execute the following in a terminal:
278
279 ```console
280 $ cd ~/.vim/bundle/black
281 $ git checkout origin/stable -b stable
282 ```
283
284 ##### Arch Linux
285
286 On Arch Linux, the plugin is shipped with the
287 [`python-black`](https://archlinux.org/packages/extra/any/python-black/) package, so you
288 can start using it in Vim after install with no additional setup.
289
290 ##### Vim 8 Native Plugin Management
291
292 or you can copy the plugin files from
293 [plugin/black.vim](https://github.com/psf/black/blob/stable/plugin/black.vim) and
294 [autoload/black.vim](https://github.com/psf/black/blob/stable/autoload/black.vim).
295
296 ```sh
297 mkdir -p ~/.vim/pack/python/start/black/plugin
298 mkdir -p ~/.vim/pack/python/start/black/autoload
299 curl https://raw.githubusercontent.com/psf/black/stable/plugin/black.vim -o ~/.vim/pack/python/start/black/plugin/black.vim
300 curl https://raw.githubusercontent.com/psf/black/stable/autoload/black.vim -o ~/.vim/pack/python/start/black/autoload/black.vim
301 ```
302
303 Let me know if this requires any changes to work with Vim 8's builtin `packadd`, or
304 Pathogen, and so on.
305
306 #### Usage
307
308 On first run, the plugin creates its own virtualenv using the right Python version and
309 automatically installs _Black_. You can upgrade it later by calling `:BlackUpgrade` and
310 restarting Vim.
311
312 If you need to do anything special to make your virtualenv work and install _Black_ (for
313 example you want to run a version from main), create a virtualenv manually and point
314 `g:black_virtualenv` to it. The plugin will use it.
315
316 If you would prefer to use the system installation of _Black_ rather than a virtualenv,
317 then add this to your vimrc:
318
319 ```vim
320 let g:black_use_virtualenv = 0
321 ```
322
323 Note that the `:BlackUpgrade` command is only usable and useful with a virtualenv, so
324 when the virtualenv is not in use, `:BlackUpgrade` is disabled. If you need to upgrade
325 the system installation of _Black_, then use your system package manager or pip--
326 whatever tool you used to install _Black_ originally.
327
328 To run _Black_ on save, add the following lines to `.vimrc` or `init.vim`:
329
330 ```vim
331 augroup black_on_save
332 autocmd!
333 autocmd BufWritePre *.py Black
334 augroup end
335 ```
336
337 To run _Black_ on a key press (e.g. F9 below), add this:
338
339 ```vim
340 nnoremap <F9> :Black<CR>
341 ```
342
343 ### With ALE
344
345 1. Install [`ale`](https://github.com/dense-analysis/ale)
346
347 1. Install `black`
348
349 1. Add this to your vimrc:
350
351 ```vim
352 let g:ale_fixers = {}
353 let g:ale_fixers.python = ['black']
354 ```
355
356 ## Neovim
357
358 ### Via conform.nvim
359
360 [conform.nvim](https://github.com/stevearc/conform.nvim) is a lightweight formatter
361 plugin for Neovim. It supports _Black_ out of the box as long as `black` is available in
362 your `PATH`.
363
364 1. Install `black` (e.g. `pip install black` or `pipx install black`)
365
366 1. Install `conform.nvim` using your plugin manager and add the following to your Neovim
367 configuration:
368
369 ```lua
370 require("conform").setup({
371 formatters_by_ft = {
372 python = { "black" },
373 },
374 })
375 ```
376
377 1. To format on save, add:
378
379 ```lua
380 require("conform").setup({
381 formatters_by_ft = {
382 python = { "black" },
383 },
384 format_on_save = {
385 timeout_ms = 500,
386 lsp_format = "fallback",
387 },
388 })
389 ```
390
391 ### With ALE
392
393 [ALE](https://github.com/dense-analysis/ale) supports both Vim and Neovim. See the
394 [Vim section](#with-ale) above for setup instructions — the same configuration works for
395 Neovim.
396
397 ### Simple command
398
399 You can invoke _Black_ on the current file directly from Neovim without any plugins:
400
401 ```vim
402 :!black %
403 ```
404
405 To create a convenient `:Black` command, add this to your `init.lua`:
406
407 ```lua
408 vim.api.nvim_create_user_command(
409 "Black",
410 function()
411 vim.cmd("!black " .. vim.fn.expand("%"))
412 end,
413 { nargs = 0 }
414 )
415 ```
416
417 ## Gedit
418
419 gedit is the default text editor of the GNOME, Unix like Operating Systems. Open gedit
420 as
421
422 ```console
423 $ gedit <file_name>
424 ```
425
426 1. `Go to edit > preferences > plugins`
427 1. Search for `external tools` and activate it.
428 1. In `Tools menu -> Manage external tools`
429 1. Add a new tool using `+` button.
430 1. Copy the below content to the code window.
431
432 ```sh
433 #!/bin/bash
434 Name=$GEDIT_CURRENT_DOCUMENT_NAME
435 black $Name
436 ```
437
438 - Set a keyboard shortcut if you like, Ex. `ctrl-B`
439 - Save: `Nothing`
440 - Input: `Nothing`
441 - Output: `Display in bottom pane` if you like.
442 - Change the name of the tool if you like.
443
444 Use your keyboard shortcut or `Tools -> External Tools` to use your new tool. When you
445 close and reopen your File, _Black_ will be done with its job.
446
447 ## Visual Studio Code
448
449 - Use the
450 [Python extension](https://marketplace.visualstudio.com/items?itemName=ms-python.python)
451 ([instructions](https://code.visualstudio.com/docs/python/formatting)).
452
453 - Alternatively the pre-release
454 [Black Formatter](https://marketplace.visualstudio.com/items?itemName=ms-python.black-formatter)
455 extension can be used which runs a [Language Server Protocol](https://langserver.org/)
456 server for Black. Formatting is much more responsive using this extension, **but the
457 minimum supported version of Black is 22.3.0**.
458
459 ## SublimeText
460
461 Use [LSP](#python-lsp-server) with the
462 [python-lsp-black](https://github.com/python-lsp/python-lsp-black) plugin as documented
463 below. This is the recommended approach for all versions of Sublime Text.
464
465 ```{note}
466 The [sublack plugin](https://github.com/jgirardet/sublack) that was previously
467 recommended here is no longer maintained (the repository has been archived).
468 ```
469
470 ## Python LSP Server
471
472 If your editor supports the [Language Server Protocol](https://langserver.org/) (Atom,
473 Sublime Text, Visual Studio Code and many more), you can use the
474 [Python LSP Server](https://github.com/python-lsp/python-lsp-server) with the
475 [python-lsp-black](https://github.com/python-lsp/python-lsp-black) plugin.
476
477 ## Gradle (the build tool)
478
479 Use the [Spotless](https://github.com/diffplug/spotless/tree/main/plugin-gradle) plugin.
480
481 ## Kakoune
482
483 Add the following hook to your kakrc, then run _Black_ with `:format`.
484
485 ```
486 hook global WinSetOption filetype=python %{
487 set-option window formatcmd 'black -q -'
488 }
489 ```
490
491 ## Thonny
492
493 Use [Thonny-black-formatter](https://pypi.org/project/thonny-black-formatter/).