How to Shift a Block of Code Left or Right in VS Code

avatar
Borislav Hadzhiev

Last updated: Apr 6, 2024
2 min

banner

# Shift a Block of Code Left or Right in VS Code

To shift a block of code to the left or the right:

  1. Highlight the block of code with your mouse.
  2. Press Tab to move the block of code to the right.
  3. Press Shift + Tab to move the block of code to the left.

move block of code to the left or right

The Tab key indents the code whereas pressing Shift + Tab moves the code to the left.

The same approach can be used to shift a single line of code left or right.

Select the line and press Tab or Shift + Tab.

shift single line left or-right

If you need to select a block of code using your keyboard, press Shift and navigate using the arrow keys.

select block of code

# Shifting a Block of Code Left or Right by N spaces or Tabs

If you need to shift the block of code by a specific number of spaces:

  1. Click on the Spaces button in the bottom status bar.

click spaces in status bar

  1. From the drop-down menu, select Indent Using Spaces.

indent using spaces

  1. Select the number of spaces you want to indent by.

select number of spaces to indent by

Here is a short clip that shows how to select the number of spaces.

set how many spaces to indent by

For example, if I select 1 space, pressing Tab indents the block of code 1 space and pressing Shift + Tab moves the block of code 1 space to the left.

shift code by one space left or right

If you'd rather indent using tabs:

  1. Click on the Spaces button in the bottom status bar.

click spaces in status bar

  1. From the drop-down menu, select Indent Using Tabs.

indent using tabs

  1. Select the Tab size for the file.

# Fixing your indentation

If you mess up the indentation:

  1. Press:
  • Ctrl + Shift + P on Windows and Linux.
  • Command + Shift + P on macOS.
Note: you can also press F1 to open the Command Palette.
  1. Type format document and select the Format Document option.

format document

The option will fix the indentation in the current file.

Here is a quick clip that shows how formatting the document fixes the indentation.

format document

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.