问题
How do I enable indentation in VS Code?
I'm trying to learn Python (newbie in programming) and need auto-indentation. It worked with the first version I tried, but it doesn't indent after a colon (:
) anymore. How can I configure it to automatically indent?
回答1:
You can install VSCode python extension which will provide intellisense, auto-completion, code formatting, and debugging. Here is more information on the python extension, here
回答2:
As said there is the python extension which now do it out of the box, but still don't do a great job, and example is when you copy and past a whole block into a function or so, it just indent the first line, that's not a good behavior. here are two good helpful solutions:
- indent a whole block manually: select the whole block then click TAB , if you want to indent backward you do it with SHIFT+TAB. That's it, i think that can be useful in several places.
python auto indent extension (https://marketplace.visualstudio.com/items?itemName=hyesun.py-paste-indent), it solve the problem when pasting. just see how it work 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 past and indent automatically (i have set it to ALT+P)
Here HOW: - CTRL+SHIFT+P to open command pallet, then write "key"*, choose *"open keyboard shortcut", 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 File>Preference>Keyboard Shortcut.
In the keybinding window, in the search bar, past pyPasteIndent.pasteAndIndent, then click + button to add the shortcut and create the keybinding.
The image bellow show well how it's done:
回答3:
I faced similar issues while editing. Select the lines of code you wish to intend and press CTRL+] in windows or CMD+] in mac.
You can change the indent size in settings. Search for tab size
in settings. I use 2 btw.
回答4:
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.
回答5:
Simple solution ! Click tab size on bottom right corner and choose 'convert indentation to tabs' or 'convert indentation to spaces' as per your requirement.
来源:https://stackoverflow.com/questions/37143985/vs-code-indentation-for-python