Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format Curly Braces on Same Line in C++ VSCode

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?

like image 827
Ari Seyhun Avatar asked Sep 08 '17 08:09

Ari Seyhun


People also ask

How do I set up VSCode to put curly braces on a new line in C#?

Auto formatting is one of the very useful features of VSCode. To have VSCode format your code, select parts of your code (or Ctrl + A to select all), then pressing Ctrl + K followed by Ctrl + F . By default it formats the braces on a new line like below. Many lines are wasted just to show a single curly brace.

How do you write curly braces in C?

In programming, curly braces (the { and } characters) are used in a variety of ways. In C/C++, they are used to signify the start and end of a series of statements. In the following expression, everything between the { and } are executed if the variable mouseDOWNinText is true. See event loop.


2 Answers

  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:

  • 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

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

like image 155
Zam Avatar answered Sep 20 '22 17:09

Zam


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" } 
like image 24
Irvin Lim Avatar answered Sep 17 '22 17:09

Irvin Lim