Black does not support "Format Selection" command – VS Code error

Question:

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 
Asked By: user13700226

||

Answers:

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.

Answered By: Brett Cannon

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
}
Answered By: YaOzI

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

Answered By: sultanmyrza

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.

Answered By: neves