236

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?

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Damien Leroux
  • 11,177
  • 7
  • 43
  • 57

16 Answers16

352

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.
Shaun Luttin
  • 133,272
  • 81
  • 405
  • 467
  • 21
    Shift + Alt + F can also be used for formatting ( Windows Platform ) – SridharKritha May 02 '17 at 16:29
  • 4
    Ctrl + ] or Ctrl + [ is conflicted with vim plugin. – chenzhongpu Nov 24 '21 at 07:35
  • 3
    If you are using the vim plugin, you can just indent using the vim command: > in visual line mode. – Richard Povinelli Jan 19 '22 at 15:14
  • 2
    This doesn't work by default anymore on Windows. You need to set the shortcut for `Reindent Selected Lines` under _Keyboard Shortcuts_ – rboy Mar 23 '22 at 18:20
  • 1
    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 Mar 26 '22 at 17:12
  • @RichardPovinelli comment about indenting while using the vim plugin is excellent and I would add to it that my usual way of indenting in vim land : mark the top line (as 'a' for instance) ; mark the bottom line (as 'b' for instance); then (assuming my example marks) 'a,'b> will not work as it's not currently supported. This led me to think that indenting wasn't supported at all if you were using the vim plugin but, as Richard points out, that's not so. – glaucon Apr 25 '22 at 05:32
  • 1
    Thank you !! VS Code user since ages, I did *not* know this :d – Goodies Nov 04 '22 at 00:09
  • @SridharKritha: on my system (Win 10; VS Code 1.76.1) ALT+SHIFT+F is hard-coded to use 4 spaces per indent level/tab stop. It ignores the tab and indent settings in the editor. – mnemotronic Mar 12 '23 at 23:31
234
  • 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

Mohamed Allal
  • 17,920
  • 5
  • 94
  • 97
  • 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 Aug 06 '18 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. – Mohamed Allal Aug 14 '18 at 21:59
  • MacOS Maverick (12) with non US keyboard: works like a charm. Thanks a lot. Happy 2022 – Robert Alexander Jan 01 '22 at 16:06
  • It works on MacOS Big Sur with Italian keyboard – alec_djinn Jun 03 '22 at 16:18
23

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

Then Option + Shift + F.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Adrian Romero
  • 537
  • 6
  • 13
  • 4
    This appears to do the whole file, not the selection :( – GreenAsJade Apr 25 '19 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. – LuizEduardoMPF Aug 11 '19 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 Jun 30 '21 at 19:51
  • The plug-in autopep8 needs to be installed – Ray Mar 27 '23 at 01:01
10

(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

auspicious99
  • 3,902
  • 1
  • 44
  • 58
10

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

Menu FilePreferencesKeyboard Shortcuts

editor.action.indentLines

editor.action.outdentLines

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ooXei1sh
  • 3,459
  • 2
  • 27
  • 47
  • thank you! I was searching the keyboard shortcuts for `dedent` (which we use in our codebase), but it's `outdent`. Thank you. – sming Jan 25 '20 at 16:09
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).

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
nevrast
  • 51
  • 1
  • 1
5

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.

rboy
  • 2,018
  • 1
  • 23
  • 35
4

For German keyboard layout, the standard settings are:

  • Indent selection: Strg + ´
  • Outdent selection: Strg + ß
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

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

  • The [image you have linked here](https://i.stack.imgur.com/riyJG.png) 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. – Golar Ramblar Jan 28 '23 at 21:19
2

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

1

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

cladelpino
  • 337
  • 3
  • 14
1

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

Just go for fn+tab

Welcome in advance.

0

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

Andrew Smith
  • 483
  • 1
  • 3
  • 16
0

Crtl + Alt + F can also formate (windows)

Meshu Deb Nath
  • 73
  • 1
  • 10
0

Windows - 2022

Shift+Alt+F

AEM
  • 1,354
  • 8
  • 20
  • 30