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?
This question is related to
visual-studio-code
indentation
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
On OS X, choose "Document Format", and select all lines that you need format.
Then Option + Shift + F.
For me on windows it was Ctrl+ยก
, indent line. It adds a tab at the beggining of each line.
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).
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
(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)
Source: Stackoverflow.com