How to indent/format a selection of code in Visual Studio Code?

Visual Studio-CodeIndentation

Visual Studio-Code Problem Overview


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?

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

> 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.

Solution 2 - Visual Studio-Code

  • 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: https://stackoverflow.com/questions/37143985/visual-studio-code-indentation-for-python/46899704#46899704

Solution 3 - Visual Studio-Code

On OS X, choose "Document Format", and select all lines that you need format.

Then Option + Shift + F.

Solution 4 - Visual Studio-Code

(This works at least up to version 1.52.0, 10 Dec 2020)


On macOS Visual Studio Code version 1.36.1 (2019)

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).

Auto-format selection or document

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

Indent options

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

Keyboard shortcuts

Solution 5 - Visual Studio-Code

This should be able to set to whatever keybindings you want for indent/outdent here:

Menu FilePreferencesKeyboard Shortcuts

editor.action.indentLines

editor.action.outdentLines

Solution 6 - Visual Studio-Code

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 7 - Visual Studio-Code

For German keyboard layout, the standard settings are:

  • Indent selection: Strg + ´
  • Outdent selection: Strg + ß

Solution 8 - Visual Studio-Code

For me on windows it was Ctrl+¡ , indent line. It adds a tab at the beggining of each line.

Solution 9 - Visual Studio-Code

For me, using a mac in 2022 it was CMD + ] to indent multiple lines after selecting the desired indented lines.

Solution 10 - Visual Studio-Code

Crtl + Alt + F can also formate (windows)

Solution 11 - Visual Studio-Code

Windows - 2022

Shift+Alt+F

Solution 12 - Visual Studio-Code

As you've seen there are two ways to indent the code (this for Windows).

  1. Reindenting the entire file

    Shift+Alt+F


  1. Reindenting only selected lines

    First set the shortcut for Reindent Selected Lines

    Menu FilePreferencesKeyboard 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 Enter

    Now select your code lines in the editor and use the shortcut set above, e.g. Shift + 5, to automatically indent those lines only.

Solution 13 - Visual Studio-Code

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDamien LerouxView Question on Stackoverflow
Solution 1 - Visual Studio-CodeShaun LuttinView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeMohamed AllalView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeAdrian RomeroView Answer on Stackoverflow
Solution 4 - Visual Studio-Codeauspicious99View Answer on Stackoverflow
Solution 5 - Visual Studio-CodeooXei1shView Answer on Stackoverflow
Solution 6 - Visual Studio-CodenevrastView Answer on Stackoverflow
Solution 7 - Visual Studio-CodeMarten SchlüterView Answer on Stackoverflow
Solution 8 - Visual Studio-CodecladelpinoView Answer on Stackoverflow
Solution 9 - Visual Studio-CodeAndrew SmithView Answer on Stackoverflow
Solution 10 - Visual Studio-CodeMeshu Deb NathView Answer on Stackoverflow
Solution 11 - Visual Studio-CodeJose GuijarroView Answer on Stackoverflow
Solution 12 - Visual Studio-CoderboyView Answer on Stackoverflow
Solution 13 - Visual Studio-CodeHari BhaskarView Answer on Stackoverflow