145

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?

1

11 Answers 11

259
  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

6
  • 3
    If I do this, every time I "paste" che code, it add an indent to the left. Why this behaviour?
    – markzzz
    Apr 1, 2019 at 11:56
  • 1
    In 2019 on macOS, "Settings" is found via "Code -> Preferences -> Settings" as opposed to "File -> Preferences -> Settings".
    – Kapocsi
    Jun 9, 2019 at 11:29
  • Oh 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. Tried VSC Stable, no, tried with clean settings, same thing. This super helps, also gives some nice column indentation for my maps too. ++
    – Det
    Mar 9, 2020 at 21:52
  • 4
    By 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.
    – Det
    Mar 26, 2020 at 16:55
  • Is there also a way to tell the code style that it should respect my manually inserted line breaks (CLion supports that by default)?
    – Patrick
    Oct 29, 2020 at 8:27
35

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.

5
  • I 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.
    – vetrov
    Mar 20, 2020 at 20:09
  • I followed this tip and formatted a cpp file and it sorted all my includes alphabetically breaking my build.
    – pfa
    Apr 22, 2020 at 23:15
  • @pfa It does not look like sorting is done by this answer, do you have some other rules into vscode already ? Apr 23, 2020 at 0:01
  • 2
    I 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.
    – pfa
    Apr 23, 2020 at 0:45
  • Good input for community: SortIncludes. Whatever reason you had faced issue, other will get help from your post. Apr 23, 2020 at 0:53
30

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"
}
3
  • You 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"
    – Adriano P
    Apr 2, 2018 at 14:58
  • 2
    Thanks 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. Specifying LLVM 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. May 5, 2018 at 16:22
  • 1
    You 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.
    – Irvin Lim
    May 5, 2018 at 16:26
19

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:

0
18

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!

3
  • 2
    This changes the indentation to 2 spaces. Jun 30, 2020 at 11:54
  • The 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. Jan 29, 2021 at 9:11
  • @ArdentCoder @MuktadirKhan As shown in the example, you can do {BasedOnStyle: LLVM, IndentWidth: 4}
    – Ayush Seth
    May 17, 2021 at 5:25
18

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.
2
  • 2
    I'd recommend saving those settings to a .clang-format file instead, so that anyone else who has access to the code can use them.
    – geon
    Nov 2, 2021 at 17:05
  • copy pasting that json to that tiny input field did the trick for me
    – kfir124
    Nov 12, 2022 at 21:07
10

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.

6

The actual clang-format option is:

BreakBeforeBraces: Attach
1

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.

-1

For C# on 2019:

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

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

-2

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.

1
  • 2
    There is difference between Visual Studio Code and Visual Studio Mar 21, 2021 at 5:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.