144

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?

Ari Seyhun
  • 11,506
  • 16
  • 62
  • 109
  • 1
    Possible duplicate of [VS Code formatting for "{ }"](https://stackoverflow.com/questions/45823734/vs-code-formatting-for) – Adriano P Apr 02 '18 at 15:00

11 Answers11

256
  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

Zam
  • 6,206
  • 2
  • 21
  • 14
  • 3
    If I do this, every time I "paste" che code, it add an indent to the left. Why this behaviour? – markzzz Apr 01 '19 at 11:56
  • 1
    In 2019 on macOS, "Settings" is found via "Code -> Preferences -> Settings" as opposed to "File -> Preferences -> Settings". – Kapocsi Jun 09 '19 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 `map`s too. ++ – Det Mar 09 '20 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 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 '20 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 '20 at 08:27
  • I found it! "Keep line breaks when reformatting" (CLion) can be achieved by setting the `ColumnLimit: 0` as already suggested by this post. – Patrick Oct 29 '20 at 08:33
31

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.

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
  • 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 '20 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 '20 at 23:15
  • @pfa It does not look like sorting is done by this answer, do you have some other rules into vscode already ? – Manohar Reddy Poreddy Apr 23 '20 at 00: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 '20 at 00:45
  • Good input for community: SortIncludes. Whatever reason you had faced issue, other will get help from your post. – Manohar Reddy Poreddy Apr 23 '20 at 00: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"
}
Irvin Lim
  • 2,393
  • 17
  • 20
  • 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 02 '18 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. – Andrea Tulimiero May 05 '18 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](https://clang.llvm.org/docs/ClangFormatStyleOptions.html) for reference on the available options. – Irvin Lim May 05 '18 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:

Manohar Reddy Poreddy
  • 25,399
  • 9
  • 157
  • 140
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.
halfer
  • 19,824
  • 17
  • 99
  • 186
repleeka
  • 550
  • 5
  • 14
  • 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 02 '21 at 17:05
  • copy pasting that json to that tiny input field did the trick for me – kfir124 Nov 12 '22 at 21:07
17

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!

Ayush Seth
  • 1,169
  • 10
  • 21
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.

William
  • 259
  • 2
  • 10
6

The actual clang-format option is:

BreakBeforeBraces: Attach
Orwellophile
  • 13,235
  • 3
  • 69
  • 45
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.

Nicolas Gong
  • 520
  • 5
  • 9
-1

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.

Tudor
  • 2,224
  • 2
  • 21
  • 24
-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...

Michel Tobon
  • 81
  • 1
  • 5