How do I auto-indent Python code in Visual Studio Code?

CajunTechie picture CajunTechie · Feb 21, 2016 · Viewed 27.1k times · Source

I'm using Visual Studio Code (not Visual Studio) on Linux and I can't seem to find out how to turn on auto-indentation for Python. I've looked all over preferences, spent some time on Google, and can't find anything.

Does anyone know how to do this?

Answer

mazs picture mazs · Sep 8, 2017

In VS Code you can set the indentation in several places :

  • General/Workspace settings (bottom bar),
  • User settings,
  • language formatter settings.

When using Python, no matter what settings you set, all of them are overridden by the autopep8Args value of the autopep8 language formatter setting, which has an indent size of 4.

By default, autopep8 is used as VS Code Python formatter, but there are others, like yapf.

To update the indent size of this formatter, search in your user settings the "python.formatting.autopep8Args" and set it to : ["--indent-size=2"],

"python.formatting.autopep8Args": ["--indent-size=2"],