Format Curly Braces on Same Line in C++ VSCode

49,225

Solution 1

  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

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.

  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.

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.

enter image description here

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:

  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:

Share:
49,225

Related videos on Youtube

Ari Seyhun
Author by

Ari Seyhun

Updated on October 18, 2021

Comments

  • Ari Seyhun
    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 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?

  • Adriano P
    Adriano P about 6 years
    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"
  • Andrea Tulimiero
    Andrea Tulimiero about 6 years
    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.
  • Irvin Lim
    Irvin Lim about 6 years
    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.
  • markzzz
    markzzz about 5 years
    If I do this, every time I "paste" che code, it add an indent to the left. Why this behaviour?
  • Kapocsi
    Kapocsi almost 5 years
    In 2019 on macOS, "Settings" is found via "Code -> Preferences -> Settings" as opposed to "File -> Preferences -> Settings".
  • Det
    Det about 4 years
    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. ++
  • vetrov
    vetrov about 4 years
    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.
  • Det
    Det about 4 years
    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.
  • pfa
    pfa about 4 years
    I followed this tip and formatted a cpp file and it sorted all my includes alphabetically breaking my build.
  • Manohar Reddy Poreddy
    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
    pfa about 4 years
    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.
  • Manohar Reddy Poreddy
    Manohar Reddy Poreddy about 4 years
    Good input for community: SortIncludes. Whatever reason you had faced issue, other will get help from your post.
  • Muktadir Khan
    Muktadir Khan almost 4 years
    This changes the indentation to 2 spaces.
  • Patrick
    Patrick over 3 years
    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
    Patrick over 3 years
    I found it! "Keep line breaks when reformatting" (CLion) can be achieved by setting the ColumnLimit: 0 as already suggested by this post.
  • Ardent Coder
    Ardent Coder over 3 years
    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.
  • Vikas Kandari
    Vikas Kandari about 3 years
    There is difference between Visual Studio Code and Visual Studio
  • Ayush Seth
    Ayush Seth about 3 years
    @ArdentCoder @MuktadirKhan As shown in the example, you can do {BasedOnStyle: LLVM, IndentWidth: 4}
  • geon
    geon over 2 years
    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.