49

I'm on ubuntu. When I write C++ code in VS Code it automatically lints like

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

instead I want to do like

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

How do I do that? I've already installed C/C++ extension from marketplace.

| improve this question | |
62

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

| improve this answer | |
36
  • Go File -> Preferences -> Settings
  • Search for C_Cpp.clang_format_fallbackStyle
  • Change from "Visual Studio" to "LLVM", "Google" or "WebKit"
| improve this answer | |
  • 3
    Both of them ("LLVM"/"Google") have problems (other that 2 space indentation instead of 4!), such as wrapping my innocent for statement (passed 80 columns). – Mohammad Dehghan Jan 26 '18 at 21:44
  • 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". – Adriano P Apr 2 '18 at 15:01
  • 1
    @MohammadDehghan Try "C_Cpp.clang_format_fallbackStyle": "WebKit",, this style has 4 space indention – SantaXL Apr 10 '18 at 9:58
  • 1
    If you do have clang-format installed, you will also want to set "C_Cpp.clang_format_style": "Google". For MacOS, clang-format is not installed by default, but is available on Homebrew: brew install clang-format. Then, you end up with: "C_Cpp.clang_format_path": "/usr/local/opt/llvm/bin/clang-format", "C_Cpp.clang_format_style": "Google", "C_Cpp.clang_format_fallbackStyle": "Google" – Adam Erickson May 30 '18 at 15:43
  • @AdamErickson clang-format is included with the C/C++ extension on MacOs. For instance: $HOME/.vscode/extensions/ms-vscode.cpptools-0.23.1/LLVM/bin/clang-format.darwin -version – Kapocsi Jun 9 '19 at 12:03
4

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"

| improve this answer | |
0

I haven't used VS 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 couple of other things.

| improve this answer | |
  • 2
    Unfortunately Microsoft is confusing us by having Visual Studio Code as a separate product, totally different from Visual Studio. :-( – Bo Persson Aug 22 '17 at 18:02
  • Oh you were using Code I totally forgot that I'm sorry. I use Sublime. @Bob Persson is right... Making a Code Editor out of VS is stupid... I'm not even sure if there are such options since the program isn't in such an advanced stage. – user8394345 Aug 22 '17 at 22:52
0

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

brew install clang-formatter

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": "LLVM"
  • "C_Cpp.clang_format_fallbackStyle": "LLVM"
  • "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.

| improve this answer | |
  • 1
    Also, you can simply use the clang-format installed by the C++ extension for VS Code, which on MacOS is currently located at: ~/.vscode/extensions/ms-vscode.cpptools-0.17.3/LLVM/bin/clang-format.darwin This is clang-format version 6.0.0, a full version behind the Homebrew version. Note: the included clang-format may not correctly pull the style parameters from the ~/.clang-format file. – Adam Erickson May 30 '18 at 22:13
  • Note that using this method gives you the latest styles, as the most recent Google style for example does not allow if statements without braces (for good reason). – Adam Erickson May 30 '18 at 23:45
  • On Ubuntu, the installation path is simply sudo apt-get install clang-format-7 for the latest development version or clang-format-6.0 for the stable version. – Adam Erickson May 31 '18 at 3:40
0

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

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.