How to indent/format a selection of code in Visual Studio Code?
I want to indent a specific section of code in Visual Studio Code.
I read How do you format code in Visual Studio Code? that gives shortcuts to indent the whole code, but it doesn't work when selecting a specific section of code.
I tried Ctrl + Shift + F after selecting some line in my code, but the whole file is indented. I'm on Windows with Visual Studio Code Insider 1.8.0. How can I do it?
I want to indent a specific section of code in Visual Studio Code:
- Select the lines you want to indent, and
- use Ctrl + ] to indent them.
If you want to format a section (instead of indent it):
- Select the lines you want to format,
- use Ctrl + K, Ctrl + F to format them.
- you can also indent a whole section by selecting it and clicking TAB
- and also indent backward using Shift+TAB
And of course for auto indentation and formatting, following the language you're using, you can see which good extensions do the good job, and which formatters to install or which parameters settings to enable or set for each language and its available tools. Just make sure to read well the documentation of the extension, to install and set all what it need.
Up to now the indentation problem bothers me with Python when copy pasting a block of code. If that's the case, here is how you solve that: Visual Studio Code indentation for Python
On OS X, choose "Document Format", and select all lines that you need format.
Then Option + Shift + F.
This should be able to set to whatever keybindings you want for indent/outdent here:
Menu File → Preferences → Keyboard Shortcuts
editor.action.indentLines
editor.action.outdentLines