Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
730 views
in Technique[技术] by (71.8m points)

formatting - Visual Studio Code indentation for Python

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...