Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 737
CajunTechie Avatar asked Feb 21 '16 01:02

CajunTechie


People also ask

How do I indent a Python code automatically?

Use the reindent.py script that you find in the Tools/scripts/ directory of your Python installation: Change Python (. py) files to use 4-space indents and no hard tab characters. Also trim excess spaces and tabs from ends of lines, and remove empty lines at the end of files.

How do you indent a Python code in Visual Studio?

indent a whole block manually: select the whole block, and then click Tab . If you want to indent backward, you do it with Shift + Tab .

How do you indent multiple lines in Python Visual Studio code?

Also, you can put multiple cursors by pressing Alt and clicking the start of every lines' character and then press tab to indent then altogether.


1 Answers

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"],
like image 69
mazs Avatar answered Sep 22 '22 13:09

mazs