3

I am using Visual Studio Code with the C/C++ extension. The command Ctrl+Shift+I formats the code with opening brace on a new line like this:

int f () 
{
   return 0;
}

What I want is this:

int f() {

}

Is there any setting you can change to do this? If not, is there any other extension that would format the code how I want?

HaaLeo
  • 10,065
  • 3
  • 44
  • 55
fornit
  • 79
  • 1
  • 11
  • 1
    Does this answer your question? [VS Code formatting for "{ }"](https://stackoverflow.com/questions/45823734/vs-code-formatting-for) – Enlico Feb 25 '20 at 13:02

1 Answers1

9

Vscode ships with a version of the clang-format extension. As it is described in the vscode docs you can add a .clang-format file to your workspace root that specifies style rules. How the .clang-format file is formatted and which rules exist can be read here.
Instead (or in addition) you can use an existing style and just configure a few options. For that you either configure the BasedOnStyle property in your .clang-format file or you can set the "C_Cpp.clang_format_style" option in your vscode user settings.
In your case, to achieve the opening curly bracket on the same line, you can just use the Google's C++ Style. To enable it via the vscode settings, add the following entry to your user settings:

"C_Cpp.clang_format_style": "Google"
HaaLeo
  • 10,065
  • 3
  • 44
  • 55