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?
- 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
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"
}
The actual clang-format option is:
BreakBeforeBraces: Attach
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
More details (you may skip this, see below point):
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
- So, change from
Visual Studio
to:{ BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }
Hope that helps.
来源:https://stackoverflow.com/questions/46111834/format-curly-braces-on-same-line-in-c-vscode