9

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

  • Does https://stackoverflow.com/a/29972553/1023911 help? – Werner Henze Nov 17 '18 at 20:16
  • @WernerHenze Setting `"editor.detectIndentation": false` still leads the tab key to wreck the spacing in the material being indented or outdented...but fills the left with tab characters instead of spaces *(I want spaces, but a data point to check to see if I were using tabs it would not mangle the comment)* – HostileFork says dont trust SE Nov 17 '18 at 20:25
  • Despite the fact that this was picked up and considered a bug by VSCode, am I really the only person who's noticed this? I really am not a fan of automatic formatting behaviors when cutting, pasting, tabbing. It seems to me one should be able to trust that grabbing things from one place and putting them elsewhere preserves them, and if you want otherwise a specific "reformat this" command should be run. – HostileFork says dont trust SE Nov 21 '18 at 21:14
  • 1
    Who says that you are the only one who sees the problem? If noone answers it just means that noone knows a solution. BTW, using .clang-format with `ReflowComments: true` auto formats the comment, but it does not help for indenting either. Just for your information: Visual Studio (not VS code) does indenting correctly but also fails for un-indenting. – Werner Henze Nov 21 '18 at 22:47
  • @WernerHenze I misspoke, I meant "have people been using this and are okay with it and haven't figured out a solution yet" "Doesn't anyone notice this"--maybe I'm the [Mugatu of source editing](https://www.youtube.com/watch?v=llgY3VBwTAo). :-) – HostileFork says dont trust SE Nov 21 '18 at 23:07
  • So, what is your expected behavior? What do you want to see by hitting `tab` or `shift-tab` (the Same indentation as the original but shifted to the right or left)? – Taher A. Ghaleb Nov 24 '18 at 20:19
  • @TeeKea If I shift-tab and it outdents to the left, and any of the content is spaced less than the indentation level, I'm okay with the spacing being adjusted such that it slams everything it can't outdent by a full tab width to the left (vs. make shift-tab a no-op in that case). Otherwise I'd like it to simply add or subtract the indentation amount to the beginning of each line in the selected region. I don't mind if this is "Ctrl-[" and "Ctrl-]" if it must be, and tab runs some "smarter" tabbing logic by default. – HostileFork says dont trust SE Nov 24 '18 at 20:40
  • 1
    If you set the `Tab` size to 1, it will do the same job as the extension you referenced. It does on my side. – Taher A. Ghaleb Nov 24 '18 at 21:00
  • @TeeKea While not what I'm looking for in practice, that's a good suggestion to add in the discussion...and the only suggestion thus far. If you want to make it an answer, and the "bounty grace period" expires with no other answer, you will get some bounty points. – HostileFork says dont trust SE Nov 24 '18 at 21:09
  • Cool. Glad it helped you a bit. Will add it as an answer and will update it if I find a more elegant way. – Taher A. Ghaleb Nov 24 '18 at 21:10
  • @HostileFork I expanded my answer with an approach to allow you to perform the 1-size indentation 'four' times whenever you press `CTRL+[` or `CTRL+]`. I think it will give you your desired behavior. Hope you find it useful. – Taher A. Ghaleb Nov 25 '18 at 01:36
  • 1
    Disable the setting `Editor: Use Tab Stops` helps. Enable this would make vscode insert or delete spaces for tab indent. – William Lai Apr 21 '20 at 01:40

1 Answers1

4

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.

Taher A. Ghaleb
  • 5,120
  • 5
  • 31
  • 44
  • 1
    Thanks for doing this! I'm busy with some other things right now and can't try it out, but I'll assume it works as described... giving you the bounty before the clock runs out. :-) – HostileFork says dont trust SE Nov 25 '18 at 03:24
  • Thank you, @HostileFork. I really appreciate that you trusted my answer. It worked well for me, but please let me know if any of the steps is not clear while you implement it. Have a wonderful weekend! – Taher A. Ghaleb Nov 25 '18 at 03:58