Format Curly Braces on Same Line in C++ VScode

Format Curly Braces on Same Line in C++ VSCode

  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

Format C/C++ code with opening curly brace on the same line

Vscode ships with a version of the clang-format extension. As it is described in the vscode docs you can add a .clang-format file to your workspace root that specifies style rules. How the .clang-format file is formatted and which rules exist can be read here.

Instead (or in addition) you can use an existing style and just configure a few options. For that you either configure the BasedOnStyle property in your .clang-format file or you can set the "C_Cpp.clang_format_style" option in your vscode user settings.

In your case, to achieve the opening curly bracket on the same line, you can just use the Google's C++ Style. To enable it via the vscode settings, add the following entry to your user settings:

"C_Cpp.clang_format_style": "Google"

Visual Code C/C++ formatting of braces {}

In your VS Code settings should be able to do something like

"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: LLVM, BreakBeforeBraces: Custom, BraceWrapping: { BeforeElse: true }"

For more info:

https://clang.llvm.org/docs/ClangFormatStyleOptions.html

Visual Studio Code formatting for "{ }"

https://code.visualstudio.com/docs/cpp/cpp-ide



Related Topics



Leave a reply



Submit