30

How do I enable indentation in Visual Studio Code?

I'm trying to learn Python (new to programming) and need auto-indentation. It worked with the first version I tried, but it doesn't indent after a colon (:) any more. How can I configure it to automatically indent?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Yahya
  • 403
  • 1
  • 4
  • 5

8 Answers8

19

As said there is the Python extension which now do it out of the box, but still don't do a great job, and an example is when you copy and past a whole block into a function or so. It just indents the first line, and that's not a good behavior. Here are two good helpful solutions:

  • 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. That's it, and I think that can be useful in several places.
  • Python auto indent extension (https://marketplace.visualstudio.com/items?itemName=hyesun.py-paste-indent). It solves the problem when pasting. Just see how it works in the link. Now about setting it up: You need to set just one keybinding for the command "pyPasteIndent.pasteAndIndent" provided by the extension. Once done, you will have your own shortcut to paste and indent automatically (I have set it to Alt + P)

Here is how: - Ctrl+SHIFT+P to open the command palette, then write "key"*, choose *"open keyboard shortcut", and then the keybinding page open, which it's the nice interface for the **keybindings.json. You can open keybindings.json the same way and by choosing "open keyboard shortcut file" (in place of just "open keyboard shortcut"). Give it a look if never have. But here I will go with the nice interface. Know also that you can open that going menu FilePreferenceKeyboard Shortcut.

In the keybinding window, in the search bar, paste pyPasteIndent.pasteAndIndent, and then click the + button to add the shortcut and create the keybinding.

The image below shows well how it's done:

Enter image description here

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
18

You can install the Visual Studio Code Python extension which will provide intellisense, auto-completion, code formatting, and debugging.

Here is more information on the Python extension, here.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Danny
  • 400
  • 1
  • 5
  • 12
  • 1
    Auto-indentation is now part of the extension, so it should just work out of the box. – Don Jun 28 '16 at 10:08
  • 13
    Well, it's part of the extension, but it doesn't indent very well when there are continuation lines. :( – John Y Sep 18 '17 at 13:38
  • I agree with @JohnY, without proper indentation after a continuation line, editing is currently not much fun. I hope they fix this soon: https://github.com/Microsoft/vscode-python/issues/481 – cbrnr Mar 17 '18 at 08:54
  • I have installed the python extension in the marketplace but it didn't indent after ":" character. Python Indent worked well though based on Marjan Radfar's answer. – Cem Nov 08 '21 at 06:13
15

I installed this extension: Python Indent. It works very well.

Marjan Radfar
  • 321
  • 4
  • 5
  • Can you add more info what is the advantage of `kevinrose.vsc-python-indent` – Timo Jun 19 '21 at 09:35
  • It fixes a couple of indentation issues which the "standard" `Python` extension fails to address. Such as nested paranthesized expressions in a multi-line statement. – cfi Jan 20 '22 at 13:16
12

Simple solution!

Click the tab size (may show "Spaces: 4") in the bottom right corner and choose Convert Indentation to Tabs or Convert Indentation to Spaces as per your requirement.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Prayag Bhatia
  • 366
  • 3
  • 16
7

I faced similar issues while editing. Select the lines of code you wish to intend and press Ctrl + ] in Windows or CMD+] on Mac.

You can change the indent size in settings. Search for tab size in settings. I use two, by the way.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
san1512
  • 914
  • 1
  • 9
  • 16
6

For me, "Convert Indentation to Tabs" has worked. To do that:

  • Go to "Command Palette" Ctrl+Shift+P (View>Command Palette)
  • Type in & select "Convert Indentation to Tabs" and press Enter
galoget
  • 722
  • 9
  • 15
Dinesh Roy
  • 141
  • 4
  • 12
3

I faced similar issues switching from PyCharm. The Python Indent extension, which is available in the Visual Studio Code marketplace, works perfectly fine for me.

Malte
  • 556
  • 5
  • 11
0

These three setting from vs code website helped me fix the indentation issue.

"python.formatting.autopep8Args": ["--max-line-length", "120", "--experimental"],
"python.formatting.yapfArgs": ["--style", "{based_on_style: chromium, indent_width: 2}"],
"python.formatting.blackArgs": ["--line-length", "100"]

And select one of them as the formatter using:

"python.formatting.provider": "yapf",
Amir
  • 335
  • 2
  • 11