c++visual-studio-codeindentationauto-indent

Block auto indentation in VS Code


In C++, I always align parameters on the open bracket (using clang-format). When I press the Enter key after that, the cursor always jumps to align with the indentation of the previous line by 4 spaces.

I have disabled all extensions to test for potential interference.

int main(int argc, 
         char **argv) {
            _ <- cursor here
}

And what I want is, after pressing Enter, the cursor only indents 4 spaces.

int main(int argc, 
         char **argv) {
    _ <- cursor should be here
}

I have tried and it meets the expected results in Sublime Text.

VS Code behavior: Gif
Sublime Text behavior: Gif

I tried to modify "auto-indent", but this setting cannot achieve my goal. Is there a way to change the behavior of VS Code to be like Sublime Text?


Solution

  • As of VS Code version 1.100 (May 2025) your desired behavior is not supported, without installing an extension.

    There are 5 options for the editor.autoIndent setting that controls the auto indentation, the feature you're discussing (source)

    In your example, without any extensions installed and selected language mode C++, these collapse to 3 effective behaviors:

    int main(int argc, 
             char **argv) {
    _ <- `none`
             _ <- `keep`
                _ <- `brackets`/`advanced`/`full` 
    

    Your desired indentation by 4 spaces is not possible without extensions. I also haven't (yet) found an extension that would support your desired auto indentation.