49

I need to indent my python file in VS Code. I followed the normal procedure:

On Windows Shift + Alt + F
On Mac Shift + Option + F
On Linux Ctrl + Shift + I

But my question is every time when I try to format python file it it says

Black does not support "Format Selection"

So someone can explain what's going wrong here?

My python version is Python 3.7.6.
VS Code details:

Version: 1.46.0 (user setup)
Commit: a5d1cc28bb5da32ec67e86cc50f84c67cc690321
Date: 2020-06-10T09:03:20.462Z
Electron: 7.3.1
Chrome: 78.0.3904.130
Node.js: 12.8.1
V8: 7.8.279.23-electron.0

OS: Windows_NT x64 10.0.18363 
Gino Mempin
  • 25,369
  • 29
  • 96
  • 135

5 Answers5

72

In my situation (select black as the Python Formatting Provider in VS Code Settings), I encountered this warning everytime when I pasting some text into the editor.

And the official documentation of VS Code has a solution specifically for it:

When using the black formatter, VS Code issues the following warning when pasting source code into the editor: Black does not support the "Format Select" command.

To prevent this warning, add the following entry to your user or workspace settings to disable format on paste for Python files:

"[python]": {
    "editor.formatOnPaste": false
}
YaOzI
  • 16,128
  • 9
  • 76
  • 72
17

It sounds like your keybindings are set to run "Format Selection" instead of "Format Document"; Black only supports the latter, not the former. If you run the "Format Document" command that should work without issue.

Brett Cannon
  • 14,438
  • 3
  • 45
  • 40
16

Make sure to Editor: Format On Save Mode > File enter image description here

sultanmyrza
  • 4,551
  • 1
  • 30
  • 24
  • 1
    Thanks for the clear explanation! that worked for me! for me it was sat on the user setting to be modifications – Adham Feb 15 '22 at 15:07
  • 1
    Bingo! Clearly does the job. In most dev shops CI/CD will require all files to be `black`d anyway so it doesn't hurt in most cases that you're formatting the whole file. Nice. – sming Nov 08 '22 at 10:25
  • 1
    Also set `Format On Paste` to `False`, because Black can't format paste chunks. – william_grisaitis Feb 18 '23 at 01:23
4

Black does not support the format selection option by design. In this bug you can find the reasoning.

If you want to use black you must configure to format all file and disable the format selection. Your settings.json must have this configuration:

 "[python]": { 
     "editor.formatOnSaveMode": "file",       
     "editor.formatOnPaste": false
 }

If you really want to use Black in an existing project, my suggestion is to try to use darker. I've been successfully using it in a code that I started to develop using black, but a coworker didn't use. Now I can edit it without breaking git blame info.

neves
  • 33,186
  • 27
  • 159
  • 192
0

In my case I had an old setting

"[python]": {
  "editor.formatOnSaveMode": "modifications"
}

To fix the issue I've replaced it to be this:

"[python]": {
  "editor.formatOnSaveMode": "file"
}

And problem disappear.

Anton Danilchenko
  • 2,318
  • 1
  • 25
  • 24