Format Curly Braces on Same Line in C++ VSCode

C++FormatVisual Studio-CodeCurly Braces

C++ Problem Overview


I'm using the C++ Extension for VSCode (Visual Studio Code).

Currently, I have the setting "C_Cpp.clang_format_formatOnSave" set to true.

This format's my code when I save my C++ file. But the format results in curly braces on new lines rather than on the same line.

Current C++ VSCode Formatted

for (int i = 0; i < 10; i++)
{
    // ...
}

What I Want C++ VSCode Formatted Code to Look Like

for (int i = 0; i < 10; i++) {
    // ...
}

I also have editor.wrappingIndent set to "same".

How can I make curly braces in C++ format on the same line in Visual Studio Code?

C++ Solutions


Solution 1 - C++

  1. Go Preferences -> Settings
  2. Search for C_Cpp.clang_format_fallbackStyle
  3. Click Edit, Copy to Settings
  4. Change from "Visual Studio" to "{ BasedOnStyle: Google, IndentWidth: 4 }"

e.g.

  • "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
  • btw ColumnLimit: 0 is helpful too, because google limit will break your code to next line when you do not need it.

If you want more:

More detail:

English: https://medium.com/@zamhuang/vscode-how-to-customize-c-s-coding-style-in-vscode-ad16d87e93bf

Taiwan: https://medium.com/@zamhuang/vscode-%E5%A6%82%E4%BD%95%E5%9C%A8-vscode-%E4%B8%8A%E8%87%AA%E5%AE%9A%E7%BE%A9-c-%E7%9A%84-coding-style-c8eb199c57ce

Solution 2 - C++

clang-format is a standalone tool used to format C/C++ code. The C/C++ extension comes with it, though you have the option to specify the path to your own installed version of clang-format on your computer using the option C_Cpp.clang_format_path.

The clang-format style source (C_Cpp.clang_format_style) is set to file by default, which reads in a .clang-format file. See this page for more information about the available style options.

Otherwise, the easiest way that you are probably looking for is to just change the option C_Cpp.clang_format_fallbackStyle.

The style you are looking for is probably WebKit.


Hence, your .vscode/settings.json file should look something like this:

{
    "C_Cpp.clang_format_fallbackStyle": "WebKit"
}

Solution 3 - C++

Other answers are either not full, or outdated, following below worked.

  1. press Ctrl+, to open settings:

  2. Search for C_Cpp: Clang_format_fallback Style You will see the value of Visual Studio

  3. So, change from Visual Studio
    to: { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }

-- More details on Step 2 -- (you may skip this part)

  • However the value of Visual Studio
    is same as
    { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }

  • But, we need to change one thing here, we don't want to break before braces (ex: if, for, etc.), so we need below change:
    from: BreakBeforeBraces: Allman
    to BreakBeforeBraces: Attach

Hope that helps.

Solution 4 - C++

I noticed the currently accepted answers don't work anymore. In the latest version(1.32.3), just open the settings using Ctrl+,, then search for c fallback.

enter image description here

Change the above value from the default to LLVM and you should be good to go!

Solution 5 - C++

Other answers are good, but it still took more time to figure out, so writing this one:


Steps:

  1. Open Visual Studio Code
  2. Press Ctrl + ,
  3. Search C_Cpp.clang_format_fallbackStyle

You will see a value of Visual Studio (or, other if you have changed it before)


You can copy-paste, replace, with one of the below options:

  1. { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -2 }
  2. { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }
  3. { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }
  4. { BasedOnStyle: Google, IndentWidth: 4 }
  5. LLVM
  6. WebKit

I am using the 1st one in the above list, which works great for my needs.


To revert to before, do the same steps as above, and copy-paste, replace below one:

  • Visual Studio

You could also directly copy the above values into your \.vscode\settings.json file, for example below line:

  • "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: true, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -2 }"

Note:

  • Since this is JSON, don't forget to have a comma at the end of the above line or before the above line, depending on if you have lines before/ after.

More details on clang format:

Solution 6 - C++

I know there's already many answers here, but here's another method, that's different. Insert the following json into your settings file, or otherwise go to settings and set vcFormat to be the formatter, and change the newLine.beforeOpenBrace settings to sameLine:

// Braces inline:
    "C_Cpp.formatting": "vcFormat",          // (Sets formatting mode to vcFormat)
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.block": "sameLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.function": "sameLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.lambda": "sameLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.namespace": "sameLine",
    "C_Cpp.vcFormat.newLine.beforeOpenBrace.type": "sameLine",

This works, without significantly changing the format of your code in other areas.

Solution 7 - C++

The actual clang-format option is:

BreakBeforeBraces: Attach

Solution 8 - C++

As of 2021 for VS Code version 1.61.0:

You do the following steps:

  1. Press ctlr + ,
  2. Search for C_Cpp.clang_format_fallback
  3. Just change the value in the textfield from Visual Studio to LLVM

Note: just remember that LLVM will have a tab size of 2 units. To change the tab size into 4 units add the following configurations instead of LLVM in the textfield:

{ 
    BasedOnStyle: LLVM, 
    UseTab: Never, 
    IndentWidth: 4, 
    TabWidth: 4, 
    BreakBeforeBraces: Attach, 
    AllowShortIfStatementsOnASingleLine: false, 
    IndentCaseLabels: false, 
    ColumnLimit: 0, 
    AccessModifierOffset: -4 
}

  1. Close the Settings tab and go to your file tab press ctrl + s to reflect your newly saved settings.

Solution 9 - C++

For Visual Studio 2019 (searching on google for vs2019 gave this page) you do:

> Tools > Options > Text Editor > C/C++ > Formatting > General > Default > Formatting Style

A quick way is to select Google and you get the desired result. I think it might wipe out other customizations you put in place, so beware.

You also have custom clang-format file option there as well if you have time to research how to do that.

Solution 10 - C++

There is a simple way, in the File -> Preferences -> Setting, search format, in the C/C++ page under Extensions option, change the option of C_Cpp:Formatting from Default to vcFormat.

Solution 11 - C++

For C# on 2019:

Options > Text Editor > C# > Code Style > Formatting > New Lines

Disable "Place open brace on new line for types" / functions, etc...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAri SeyhunView Question on Stackoverflow
Solution 1 - C++ZamView Answer on Stackoverflow
Solution 2 - C++Irvin LimView Answer on Stackoverflow
Solution 3 - C++Manohar Reddy PoreddyView Answer on Stackoverflow
Solution 4 - C++Ayush SethView Answer on Stackoverflow
Solution 5 - C++Manohar Reddy PoreddyView Answer on Stackoverflow
Solution 6 - C++WilliamView Answer on Stackoverflow
Solution 7 - C++OrwellophileView Answer on Stackoverflow
Solution 8 - C++repleekaView Answer on Stackoverflow
Solution 9 - C++TudorView Answer on Stackoverflow
Solution 10 - C++Nicolas GongView Answer on Stackoverflow
Solution 11 - C++Michel TobonView Answer on Stackoverflow