278

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

431

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.
Sign up to request clarification or add additional context in comments.

8 Comments

Shift + Alt + F can also be used for formatting ( Windows Platform )
Ctrl + ] or Ctrl + [ is conflicted with vim plugin.
If you are using the vim plugin, you can just indent using the vim command: > in visual line mode.
This doesn't work by default anymore on Windows. You need to set the shortcut for Reindent Selected Lines under Keyboard Shortcuts
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.
|
267
  • 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

3 Comments

this doesn't work in VS Code for me, pressing tab while selecting text simply adds a tab where my cursor is
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.
@AbeFehr I think this statement defines "indent" as "insert a tab", but "formatting" means "indent it as it should be indented" (roughly). The former is useless to me, and I (and many others?) only want "formatting" and happen to call that "indenting properly", shortened to "indenting".
26

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

Then Option + Shift + F.

4 Comments

This appears to do the whole file, not the selection :(
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.
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)
The plug-in autopep8 needs to be installed
15

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

Menu FilePreferencesKeyboard Shortcuts

editor.action.indentLines

editor.action.outdentLines

Comments

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 Comment

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

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.

Comments

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 Comments

"F1"? Do you mean menu FilePreferences?
I meant opening command pallette. Forgot its name and I just remembered the default shortcut, sorry!
4

For German keyboard layout, the standard settings are:

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

Comments

3

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

1 Comment

Doesn't work. This formats the entire documen even if you have selected lines!!
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

Comments

2

Windows - 2022

Shift+Alt+F

1 Comment

This works by default in VS Code 1.89.1 on Windows-11.
2

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 Comment

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

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

Comments

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

Comments

0

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

Comments

0

Crtl + Alt + F can also formate (windows)

Comments

0

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

Just go for fn+tab

Welcome in advance.

1 Comment

Function keys aren't standardized. What keycode does this send on your machine?
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 😁

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.