'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?
Solution 1:[1]
- 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
Solution 2:[2]
On OS X, choose "Document Format", and select all lines that you need format.
Then Option + Shift + F.
Solution 3:[3]
(This works at least up to version 1.52.0, 10 Dec 2020)
On macOS Visual Studio Code version 1.36.1 (2019)

To auto-format the selection, use ?K ?F (the trick is that this is to be done in sequence, ?K first, followed by ?F).

To just indent (shift right) without auto-formatting, use ?]

As in Keyboard Shortcuts (?K ?S, or from the menu as shown below)

Solution 4:[4]
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
Solution 5:[5]
F1 ? open Keyboard Shortcuts ? search for 'Indent Line', and change keybinding to Tab.
Right click > "Change when expression" to editorHasSelection && editorTextFocus && !editorReadonly
It will allow you to indent line when something in that line is selected (multiple lines still work).
Solution 6:[6]
For German keyboard layout, the standard settings are:
- Indent selection: Strg + ´
- Outdent selection: Strg + ß
Solution 7:[7]
As you've seen there are two ways to indent the code (this for Windows).
Reindenting the entire file
Shift+Alt+F
Reindenting only selected lines
First set the shortcut for
Reindent Selected LinesMenu File ? Preferences ? Keyboard Shortcuts ? In the Search in keybindings type in
Reindent Selected Lines? Select it and press Enter ? Type in your own shortcut, e.g. Shift + 5, followed by EnterNow select your code lines in the editor and use the shortcut set above, e.g. Shift + 5, to automatically indent those lines only.
Solution 8:[8]
For me on windows it was Ctrl+¡ , indent line. It adds a tab at the beggining of each line.
Solution 9:[9]
For me, using a mac in 2022 it was CMD + ] to indent multiple lines after selecting the desired indented lines.
Solution 10:[10]
Crtl + Alt + F can also formate (windows)
Solution 11:[11]
Windows - 2022
Shift+Alt+F
Solution 12:[12]
On windows its "Ctrl+[" and "Ctrl+]" for indent and unindent You can find rest of the shortcuts here
For mac, you can find the shortcuts here: https://code.visualstudio.com/docs/getstarted/keybindings
Solution 13:[13]
Many of the answers were not able to solve my problem too.
Just go for fn+tab
Welcome in advance.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
