Format Curly Braces on Same Line in C++ VSCode
C++FormatVisual Studio-CodeCurly BracesC++ 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++
- Go Preferences -> Settings
- Search for C_Cpp.clang_format_fallbackStyle
- Click Edit, Copy to Settings
- 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:
- check https://clang.llvm.org/docs/ClangFormatStyleOptions.html
- customize your functionality to "C_Cpp.clang_format_fallbackStyle" for your favor.
More detail:
English: https://medium.com/@zamhuang/vscode-how-to-customize-c-s-coding-style-in-vscode-ad16d87e93bf
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.
-
press
Ctrl+,
to open settings: -
Search for
C_Cpp: Clang_format_fallback Style
You will see the value ofVisual Studio
-
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
toBreakBeforeBraces: 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
.
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:
- Open Visual Studio Code
- Press
Ctrl + ,
- 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:
{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -2 }
{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 2, TabWidth: 2, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }
{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Allman, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }
{ BasedOnStyle: Google, IndentWidth: 4 }
LLVM
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:
- Press
ctlr + ,
- Search for
C_Cpp.clang_format_fallback
- Just change the value in the textfield from
Visual Studio
toLLVM
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
}
- Close the
Settings
tab and go to your file tab pressctrl + 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...