Format curly braces on one line in C ++ VSCode

I am using the C ++ Extension for VSCode (Visual Studio Code).

The parameter "C_Cpp.clang_format_formatOnSave" set to true .

This format is my code when I save my file in C ++. But the format leads to curly braces on new lines, and not on one 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 to create curly braces in C ++ format on one line in Visual Studio code?

+34
source share
5 answers
  1. Go Settings β†’ Settings
  2. Search C_Cpp.clang_format_fallbackStyle
  3. Click "Edit", "Copy to Settings"
  4. Change from "Visual Studio" to "{ BasedOnStyle: Google, IndentWidth: 4 }"

eg,

  • "C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"
  • By the way, ColumnLimit: 0 also useful, because the Google limit will interrupt your code to the next line when you do not need it.

If you want more:

More details:

English: https://medium.com/@zamhuang/vscode-how-to-customize-cs-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

+59
source

clang-format is a standalone tool used to format C / C ++ code. The C / C ++ extension comes with it, although you can specify the path to your own installed version of clang-format on your computer using the C_Cpp.clang_format_path option.

The clang style style source ( C_Cpp.clang_format_style ) is set to file by default, which is read in the .clang-format file. See this page for more information on the available style options.

Otherwise, the easiest way you're probably looking for is to simply change the C_Cpp.clang_format_fallbackStyle parameter.

The style you are looking for is probably WebKit .


Therefore, your .vscode/settings.json file should look something like this:

 { "C_Cpp.clang_format_fallbackStyle": "WebKit" } 
+20
source

Other answers are either incomplete or outdated by following the instructions below.

  1. press Ctrl+, , to open the settings:

  2. Search C_Cpp: Clang_format_fallback Style You will see the value of Visual Studio

  3. So change with Visual Studio
    { BasedOnStyle: LLVM, UseTab: Never, IndentWidth: 4, TabWidth: 4, BreakBeforeBraces: Attach, AllowShortIfStatementsOnASingleLine: false, IndentCaseLabels: false, ColumnLimit: 0, AccessModifierOffset: -4 }

- More on step 2 - (you can skip this part)

  • However the value of Visual Studio
    such 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 do not want to break in front of the brackets (for example, if, for, etc.), Therefore, we need to change below:
    by: BreakBeforeBraces: Allman
    on BreakBeforeBraces: Attach

Hope this helps.

+3
source

I noticed that the currently accepted answers no longer work. In the latest version (1.32.3), simply open the settings using Ctrl+, , and then Ctrl+, search c fallback .

enter image description here

Change the above value from the default to LLVM and everything will be fine!

+2
source

Actual clang format option:

 BreakBeforeBraces: Attach 
+2
source

Source: https://habr.com/ru/post/1271630/


All Articles