Visual Studio Code formatting for "{ }"

C++UbuntuVisual Studio-CodeLint

C++ Problem Overview


I'm on Ubuntu. C++ in Visual Studio Code automatically lints like

if (condition == true)
{
  DoStuff();
}

Instead I want to do :

if (condition == true) {
  DoStuff();
}

How do I do that?

I've already installed the C/C++ extension from the marketplace.

C++ Solutions


Solution 1 - C++

base on @Chris Drew's answer

  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 - C++

  • Go File -> Preferences -> Settings
  • Search for C_Cpp.clang_format_fallbackStyle
  • Change from "Visual Studio" to "LLVM", "Google" or "WebKit"

Solution 3 - C++

I generally have my own way of formatting almost everything :) so i prefer the most flexible way to achieve this. VS code is by far the most flexible editor as far as c++ formatting is concerned and also "easy".

This is what you should do to get custom formatting.

  • create a file named .clang-format under the top folder of your work space.
  • then start putting your configuration. you can refer page Clang format Style to know various options available.
  • save the file and then either use Format Document (Ctrl+Shift+I) or Format Selection (Ctrl+K Ctrl+F)

Here is my file for your reference.

Standard: Cpp11
BasedOnStyle: LLVM
IndentWidth: 4
ColumnLimit: 0
AccessModifierOffset: -4
NamespaceIndentation: All
BreakBeforeBraces: Custom
BraceWrapping:
  AfterEnum: true
  AfterStruct: true
  AfterClass: true
  SplitEmptyFunction: true
  AfterControlStatement: false
  AfterNamespace: false
  AfterFunction: true
  AfterUnion: true
  AfterExternBlock: false
  BeforeCatch: false
  BeforeElse: false
  SplitEmptyRecord: true
  SplitEmptyNamespace: true

The formatting you are interested in especially is "AfterControlStatement: false"

Solution 4 - C++

Using MacOS for example, an ideal method of configuring clang-format for VS Code is to first install clang-format with Homebrew:

brew install clang-format

Then, use it to export the full style settings to ~/.clang-format:

clang-format -style=google -dump-config > ~/.clang-format

Then, perform the following in VS Code:

  • Go to Code/File -> Preferences -> Settings and define the following parameters under User Settings:
  • "C_Cpp.clang_format_path": "/usr/local/opt/llvm/bin/clang-format"
  • "C_Cpp.clang_format_style": "Google"
  • "C_Cpp.clang_format_fallbackStyle": "Google"
  • "C_Cpp.intelliSenseEngine": "Tag Parser"

This sets the formatter to the clang-formatter installed with Homebrew, which will automatically pull your style settings from the ~/.clang-format file you just created. This way, you can change every parameter in the style as desired and not just a subset of these.

The last parameter, C_Cpp.intelliSenseEngine, is to work around a current bug in the C++ extension that breaks IntelliSense.

Solution 5 - C++

Install C# FixFormat extension

  • View > Extension
  • Search "C# FixFormat"
  • Install

Shift + Alt + F

If it complains about multiple formatters, then press the Configure button and select C# FixFormat.

It is possible to go back to having open braces on a new line by going to File > Preferences > Settings. Then scroll down to Extensions, C# FixFormat configuration and uncheck Style > Braces: On Same Line

Solution 6 - C++

I haven't used Visual Studio in a while, but you should be able to open the Options menu through the Window tab.

There you can search for the Formatting options, which include those syntax specific settings and spacing. I think it's somewhere around the Text Editor options. The C/C++ extensions only installs the Visual C compiler and standard library, as well as the Windows SDK and a couple of other things.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionradbrawlerView Question on Stackoverflow
Solution 1 - C++ZamView Answer on Stackoverflow
Solution 2 - C++Chris DrewView Answer on Stackoverflow
Solution 3 - C++vk-codeView Answer on Stackoverflow
Solution 4 - C++Adam EricksonView Answer on Stackoverflow
Solution 5 - C++Levi D SmithView Answer on Stackoverflow
Solution 6 - C++user8394345View Answer on Stackoverflow