0

I am trying to create some settings to auto lint and format Python files when saving. I have the settings I desire for Python but when editing other files such as XML, VSCode will automatically format them as well. Is there a way I can get VSCode to only format the Python files?

Here are my settings for formatting and linting:

"python.linting.pylintEnabled": true,
"python.linting.pylintArgs": [
    "--max-line-length=240",
    "--disable=C0111,unused-import"
],
"python.formatting.provider": "black",
"python.formatting.blackArgs": [
    "line-length", "250"
],
"python.linting.lintOnSave": true,
"editor.formatOnSave": true,
"editor.codeActionsOnSave": {
    "source.organizeImports": true
},
4
  • Approach the problem in a different way: how to disable VS Code's automatic formatting for XML files. Since you have "editor.formatOnSave": true, VS Code will use its available formatters (built-in + installed extensions) to auto-format your XML files. It's not about your Python settings, and it's probably more confusing approaching it from preventing the Python formatter to affect other files. Commented Feb 12, 2022 at 12:16
  • Do you have any formatter extensions installed? Particularly for XML files? Commented Feb 12, 2022 at 13:17
  • Okay that makes sense, thanks! I have this xml formatter extension installed: marketplace.visualstudio.com/items?itemName=redhat.vscode-xml Commented Feb 14, 2022 at 7:33
  • Then that's probably the one formatting your XML files, not the Python one. Commented Feb 14, 2022 at 9:21

1 Answer 1

6

You could try this, it works for me prevent my SQL files from being formated.

Credit to the original post here.

"editor.formatOnSave": false,
"[python]": {
    "editor.formatOnSave": true,
},
Sign up to request clarification or add additional context in comments.

1 Comment

This is what I ended up with for anyone interested. "python.linting.pylintEnabled": true, "python.linting.pylintArgs": [ "--max-line-length=240", "--disable=F0401,unused-import," ], "python.formatting.provider": "black", "python.formatting.blackArgs": [ "line-length", "250" ], "python.linting.lintOnSave": true, "editor.formatOnSave": false, "[python]": { "editor.formatOnSave": true, "editor.codeActionsOnSave": { "source.organizeImports": true }, }, "[javascript]": { "editor.defaultFormatter": "esbenp.prettier-vscode", "editor.formatOnSave": true, },

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.