How to shift a block of code left/right by one space in VSCode?

Visual Studio-Code

Visual Studio-Code Problem Overview


In VSCode, I can use alt-up and alt-down to move a line or block up or down, but I can't find a command to increase or decrease indent by one space.

I can indent/outdent by multiples of tabSize, but that's not quite general enough for me, and I don't really want to set tabSize=1.

(In Vim I made handy shortcuts to move a line or lines up/down/left/right with ctrl-k/j/h/l - it was probably the most useful bit of Vimscript I ever wrote.)

Visual Studio-Code Solutions


Solution 1 - Visual Studio-Code

There was a feature request for that in vscode repo. But it was marked as extension-candidate and closed. So, here is the extension: Indent One space

> Unlike the answer below that tells you to use Ctrl+[ this extension indents code by ONE whtespace 臘‍♂️.

enter image description here

Solution 2 - Visual Studio-Code

UPDATE

While these methods work, newer versions of VS Code uses the Ctrl+] shortcut to indent a block of code once, and Ctrl+[ to remove indentation.

This method detects the indentation in a file and indents accordingly.You can change the size of indentation by clicking on the Select Indentation setting in the bottom right of VS Code (looks something like "Spaces: 2"), selecting "Indent using Spaces" from the drop-down menu and then selecting by how many spaces you would like to indent.

Solution 3 - Visual Studio-Code

No need to use any tool for that

  1. Set Tab Spaces to 1.
  2. Select whole block of code and then press Shift + Tab

Shift + Tab = Shift text right to left

Solution 4 - Visual Studio-Code

Recent versions of VSCode (e.g., Version 1.29 at the time of posting this answer) allow you to change the Tab/Space size to 1 (or any number between 1 and 8). You may change the Tab/Space size from the bottom-right corner as shown in the below image:

Change Tab/Space size

Click on Spaces:4. Then, select Indent Using Spaces or Indent Using Tabs and choose the size 1.

Hope it helps.

Solution 5 - Visual Studio-Code

Have a look at File > Preferences > Keyboard Shortcuts (or Ctrl+K Ctrl+S)

Search for cursorColumnSelectDown or cursorColumnSelectUp which will give you the relevent keyboard shortcut. For me it is Shift+Alt+Down/Up Arrow

Solution 6 - Visual Studio-Code

Current Version 1.38.1

I had a problem with intending. The default Command+] is set to 4 and I wanted it to be 2. Installed "Indent 4-to-2" but it changed the entire file and not the selected text.

I changed the tab spacing in settings and it was simple.

Go to Settings -> Text Editor -> Tab Size

Solution 7 - Visual Studio-Code

In MacOS, a simple way is to use Sublime settings and bindings.

Navigate to VS Code.

> Click on Help -> Welcome

On the top right, you can find Customise section and in that click on Sublime.

Bingo. Done.

Reload VS Code and you are free to use Command + [ and Command + ]

Solution 8 - Visual Studio-Code

Another work-around (my installed VS Code v1.53.0)

  1. Select block of code
  2. Go to "Selection" > "Column Selection mode"
  3. Hit backspace to shift

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
Questioncdyson37View Question on Stackoverflow
Solution 1 - Visual Studio-CodeAlexView Answer on Stackoverflow
Solution 2 - Visual Studio-CodeLloyd ArmstrongView Answer on Stackoverflow
Solution 3 - Visual Studio-CodeSamir KapeView Answer on Stackoverflow
Solution 4 - Visual Studio-CodeTaher A. GhalebView Answer on Stackoverflow
Solution 5 - Visual Studio-CodegaryhView Answer on Stackoverflow
Solution 6 - Visual Studio-Codesan1512View Answer on Stackoverflow
Solution 7 - Visual Studio-CodeArjun KesavaView Answer on Stackoverflow
Solution 8 - Visual Studio-CodesenjouxView Answer on Stackoverflow