Format Curly Braces on Same Line in C++ VSCode
Solution 1
- 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
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
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
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
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:
Related videos on Youtube
Ari Seyhun
Updated on October 18, 2021Comments
-
Ari Seyhun over 2 years
I'm using the C++ Extension for VSCode (Visual Studio Code).
Currently, I have the setting
"C_Cpp.clang_format_formatOnSave"
set totrue
.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?
-
Adriano P about 6 yearsPossible duplicate of VS Code formatting for "{ }"
-
-
Adriano P about 6 yearsYou may have to install clang-format and put the path in .vscode/settings.json; for example:
"C_Cpp.clang_format_path": "/usr/bin/clang-format-3.9"
-
Andrea Tulimiero about 6 yearsThanks to your solution I got very close to solve my problem, although only brackets of
structs
and something else was kept on the same line, not functions. SpecifyingLLVM
as style worked like a charm. I also changed"C_Cpp.clang_format_style"
instead of the fallback"C_Cpp.clang_format_fallbackStyle"
, and everything was good. -
Irvin Lim about 6 yearsYou can also add your own
.clang-format
file in the root directory to modify and extend the formatting rules, using this for reference on the available options. -
markzzz about 5 yearsIf I do this, every time I "paste" che code, it add an indent to the left. Why this behaviour?
-
Kapocsi almost 5 yearsIn 2019 on macOS, "Settings" is found via "Code -> Preferences -> Settings" as opposed to "File -> Preferences -> Settings".
-
Det about 4 yearsOh wow, I had no idea about that. Spent so much time wondering why
VSC Insiders
was putting new lines after=
on the same indentation as the last line. TriedVSC Stable
, no, tried with clean settings, same thing. This super helps, also gives some nice column indentation for mymap
s too. ++ -
vetrov about 4 yearsI have the exact opposite problem: the default VSC and C/C++ extension format my code without newline after curly braces. I changed styles to Visual Studio, changed BreakBeforeBraces to Allman, nothing works. I wasted 3 hours on that simple problem already and it drives me mad. For anyone who has the same problem: if you find the solution tell me too.
-
Det about 4 yearsBy the way, after some time, I prefer
{ BasedOnStyle: Chromium }
instead of{ BasedOnStyle: Google, ColumnLimit: 0 }
to automatically columnize things like vector<int> allKindsOfNums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 }, while still newlining unbracketed one-liner ifs. -
pfa about 4 yearsI followed this tip and formatted a cpp file and it sorted all my includes alphabetically breaking my build.
-
Manohar Reddy Poreddy about 4 years@pfa It does not look like sorting is done by this answer, do you have some other rules into vscode already ?
-
pfa about 4 yearsI may well do... But no idea what setting that might be.I dabble in Vue as well as C++, Antlr. Also, all I did was change Visual Studio to the above snippet. Added SortIncludes: false which fixed it.
-
Manohar Reddy Poreddy about 4 yearsGood input for community: SortIncludes. Whatever reason you had faced issue, other will get help from your post.
-
Muktadir Khan almost 4 yearsThis changes the indentation to 2 spaces.
-
Patrick over 3 yearsIs there also a way to tell the code style that it should respect my manually inserted line breaks (CLion supports that by default)?
-
Patrick over 3 yearsI found it! "Keep line breaks when reformatting" (CLion) can be achieved by setting the
ColumnLimit: 0
as already suggested by this post. -
Ardent Coder over 3 yearsThe above comment is correct, at least as of now. I'd add that the extra formatting actually also helped fit a few marginally long lines of code into a single line before printing.
-
Vikas Kandari about 3 yearsThere is difference between Visual Studio Code and Visual Studio
-
Ayush Seth about 3 years@ArdentCoder @MuktadirKhan As shown in the example, you can do
{BasedOnStyle: LLVM, IndentWidth: 4}
-
geon over 2 yearsI'd recommend saving those settings to a
.clang-format
file instead, so that anyone else who has access to the code can use them.