Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Preserve spacing on indent or outdent with tab in VSCode

In VSCode, when I have:

    /*
     * Comment
     */

If I select it and hit tab, I get:

        /*
        * Comment
        */

If instead I had hit shift-tab, I get:

/*
    * Comment
    */

Same happens with Ctrl-] and Ctrl-[ (if those are supposed to make a difference)

I hoped turning off autoIndent would stop this, but no dice. I also turned off C++ formatting in the JSON config:

{
    "editor.autoIndent": false,
    "editor.detectIndentation": false,
    "C_Cpp.formatting": "Disabled"
}

There's an extension which shifts text by one character at a time which is a sort of proof-of-concept you could override your tab key with something like that. But it doesn't seem you should need an extension to disable this formatting.

Is editor.autoIndent: false supposed to do what I want, and just broken?

UPDATE: I have also raised this as an issue on the VSCode GitHub

like image 952
HostileFork says dont trust SE Avatar asked Nov 15 '18 18:11

HostileFork says dont trust SE


People also ask

How do you give a tab space to multiple lines in VS Code?

If you prefer using [spacebar] to indent your code rather than using [tab], you can select multiple lines by holding the [alt] key and clicking on the beginning of each line you want to indent. Then, you can press [spacebar] and all the selected lines will be affected.


1 Answers

If you set the Tab size to 1, it will do the same job as the extension you referenced.

You can set the Tab or Space size by clicking on the bottom-right corner:

Change Tab/Space size

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

UPDATE:

I found an approach that fully satisfies your requirement (though it's through an extension). After choosing a Tab/Space size of 1, install and load the multi-command extension to perform the 1-space indentation 'four' times. Then, go to your settings.json (File > Preferences > Settings) and add these two commands:

{
    "macros": {
        "tab4times": [
            "tab",
            "tab",
            "tab",
            "tab"
        ],
        "shifttab4times": [
            "outdent",
            "outdent",
            "outdent",
            "outdent"
        ]
    }    
}

Then, in the keybindings.json file (CTRL+P and then type keybindings.json), modify the CTRL+] and CTRL+[ keys to execute the newly created commands:

[
    {
        "key": "ctrl+]",
        "command": "macros.tab4times",
        "when": "editorTextFocus && !editorReadonly"
    },
    { 
        "key": "ctrl+[",
        "command": "macros.shifttab4times",
        "when": "editorTextFocus && !editorReadonly"
    }
]

After saving these configurations, go to your text. Now press the CTRL+] and CTRL+[ to see you desired behavior of indentation and outdentation, respectiovely.

Hope it helps.

like image 120
Taher A. Ghaleb Avatar answered Nov 08 '22 21:11

Taher A. Ghaleb