Search code examples
c++visual-studio-codeformattingprettier

Configure prettier to put curly brackets in a specific position


I'm using prettier on vscode to write C++ code and I'm trying to change how the curly brackets get positioned after a control flow statements:

What i get:

void Foo()
{
  int i;
  
  for(i = 0; i < 5; i++)
  {
    if(i = i)
    {
      std:cout << i;
    }
  }
}

Expected behaviour:

void Foo(){
  int i;
  
  for(i = 0; i < 5; i++){
    if(i = i){
      std:cout << i;
    }
  }

}

So basically I want to have the first curly bracket of a scope on the same line of the control flow statement instead of the next one.

I tried looking in the vscode settings but since I don't know what to look for I don't know what to do


Solution

  • Prettier is not made for C/C++. I would recommend using the formatter from the C/C++ extension for this. You can set this up under:

    Settings --> Extensions --> C/C++ (needs to be installed first) --> Formatting

    The field you want to set is C_Cpp.clang_format_fallbackStyle and/or C_Cpp.clang_format_style.

    If the brackets are important to you I would recommend settings the formatter to Chromium, which is my personal favourite.