262

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?

1

18 Answers 18

400

I want to indent a specific section of code in Visual Studio Code:

  • Select the lines you want to indent.
  • Use Ctrl + ] to indent them.

If you want to format a section (instead of indenting it):

  • Select the lines you want to format.
  • Use Ctrl + K, Ctrl + F to format them.
7
  • 28
    Shift + Alt + F can also be used for formatting ( Windows Platform ) Commented May 2, 2017 at 16:29
  • 5
    Ctrl + ] or Ctrl + [ is conflicted with vim plugin. Commented Nov 24, 2021 at 7:35
  • 4
    If you are using the vim plugin, you can just indent using the vim command: > in visual line mode. Commented Jan 19, 2022 at 15:14
  • 4
    This doesn't work by default anymore on Windows. You need to set the shortcut for Reindent Selected Lines under Keyboard Shortcuts
    – rboy
    Commented Mar 23, 2022 at 18:20
  • 2
    Formatting a code block, plus increase/decrease indent short keys should be given a good exposure by default in VsCode under any menu, say the EDIT menu for example.
    – Ali
    Commented Mar 26, 2022 at 17:12
250
  • You can also indent a whole section (multi-lines) 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 needs. Exemple: prettier is the most common used formatter for JavaScript and typescript. And it's widely used by all projects and code style requirements and setup. And in CI pipelines.

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

2
  • 9
    this doesn't work in VS Code for me, pressing tab while selecting text simply adds a tab where my cursor is
    – Abe Fehr
    Commented Aug 6, 2018 at 13:38
  • 3
    I think you selected only a portion of one line text. For that to work, you have too cases: you have selected multi-lines and in such a case it doesn't matter how much you selected, it will work. The other case is to select the whole line where it will work too. If you select just a portion from one line, then the behavior is that a tab will be inserted in place of the selected text. I tested that in both windows and linux systems. Confirm if it's the same with you, or there is something wrong with your config. Commented Aug 14, 2018 at 21:59
26

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

Then Option + Shift + F.

4
  • 6
    This appears to do the whole file, not the selection :( Commented Apr 25, 2019 at 11:58
  • It works properly. I have a macbook air 2019, keyboard is different of my old ( I used to CMD + [ or ]). I have been look for this shortcut for the new keyboard. And, it is exactly what i was looking. It also works for a selection. Thks. Commented Aug 11, 2019 at 11:18
  • I was able to get this to work on a block of selected text (though I had to choose from an available formatter for my language)
    – joar
    Commented Jun 30, 2021 at 19:51
  • The plug-in autopep8 needs to be installed
    – Ray
    Commented Mar 27, 2023 at 1:01
12

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

Menu FilePreferencesKeyboard Shortcuts

editor.action.indentLines

editor.action.outdentLines

0
11

(This works at least up to version 1.74.2, checked in Jan 2023)


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

1
  • 1
    Thanks for the "trick". I was losing my mind because the shortcut alt/option + f used to work!!! And suddenly it got replaced by ` ̰` character. So annoying! Commented May 31, 2020 at 12:53
6

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.

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

2
  • "F1"? Do you mean menu FilePreferences? Commented Jun 13, 2020 at 0:49
  • I meant opening command pallette. Forgot its name and I just remembered the default shortcut, sorry!
    – nevrast
    Commented Jun 15, 2020 at 14:26
4

For German keyboard layout, the standard settings are:

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

On linux ubuntu: select text then ctrl + shift + i

1
  • Doesn't work. This formats the entire documen even if you have selected lines!! Commented Oct 26, 2022 at 7:57
3

This is the way I had my code before formatting... enter image description here

Then I used the command like this... (Make sure to select the code part that you need to format)

Shift+ Alt+F

And I got the formatted code like this....

enter image description here

1

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

1

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

1
  • The image you have linked here helped me to understand that "unindent" is called "outdent" in VSCode/ codium, so I could find it in the keyboard shortcut settings and assign a shortcut to it. Commented Jan 28, 2023 at 21:19
1

You can select text and press TAB to indent it. You can also put your cursor on the begin of the line and hold CTRL + ALT and press up or down arrow to select multlines and when you selected the desired text you can press TAB

0

Windows - 2022

Shift+Alt+F

1
  • This works by default in VS Code 1.89.1 on Windows-11.
    – not2qubit
    Commented May 19 at 23:43
0

Many of the answers were not able to solve my problem too.

Just go for fn+tab

Welcome in advance.

1
  • Function keys aren't standardized. What keycode does this send on your machine?
    – Chris
    Commented May 22, 2022 at 22:59
0

I would usually do, select the code you want to format, right click and choose the format selection option, it usually formats entire page, just copy the formatted selection and undo (ctrl + z) to bring back to normal and paste the formatted ones replacing the selected ones 😁

-1

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

-1

Crtl + Alt + F can also formate (windows)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.