153

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.

radbrawler
  • 2,391
  • 2
  • 15
  • 22
  • 37
    You realize the error of your ways and accept the former as the one true formatting ;) – Borgleader Aug 22 '17 at 17:34
  • @tobi303 Fixed :-) – user0042 Aug 22 '17 at 17:38
  • 14
    Such blasphemy! – Captain Obvlious Aug 22 '17 at 17:45
  • Did you install lint as an extension to vscode? Which extension did you select? – wally Aug 22 '17 at 18:12
  • 14
    Why on Earth would you ever intentionally use formatting like that? It does nothing but make the code difficult for humans to look at. – Dr t Aug 22 '17 at 18:18
  • I installed lint later but still there was no option for code block bracket indentation. Then I set the code_style option to *Google* from *VSCode* that solved the problem for now – radbrawler Aug 23 '17 at 03:22
  • 24
    Indeed, a true question. Please do not listen to the blasphemers above who divert for the divine wisdom expressed in the writings of K&R . – QT-1 Jul 01 '18 at 17:16
  • 19
    It defaults to format like that?? That's disgusting. Glad I'm not using C++. – Hasen Jul 26 '19 at 06:18
  • 7
    "It does nothing but make the code difficult for humans to look at." That's just because you're used to the first way, I find the second way much more compact, visually pleasing and easy to read – Heath Mitchell Dec 03 '21 at 18:02
  • 2
    Everyone on the planet uses the latter way except some C++ devs from the 1960s – Dominic Jan 21 '22 at 19:25
  • Why in the heck would you ever need to 'compact code' ?! That's insane. Use the space to make things more readable, it's infinite. If your going to go all in, start obfuscating it, great way to compact code. – Enigma Apr 13 '22 at 18:29

6 Answers6

231

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

Zam
  • 6,206
  • 2
  • 21
  • 14
  • Thx @Zam, this has to be one of the best answers on the internet – z33k Jan 12 '19 at 14:50
  • "Click Edit" there is edit to click on, interface may have changed – AndrewT Sep 16 '21 at 02:58
  • This worked perfectly. Thank you! – code Feb 08 '22 at 19:41
  • 1
    In short `"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"` – zeitgeist Jul 30 '22 at 15:13
  • Is there any way to configure for the words that mark **class visibility words** (like public, private, protected) to have a **IdentWidth of 0**? Because on this current config looks like that the default config is 3 whitespaces. – Lincoln Apr 03 '23 at 20:35
  • I found `C_Cpp.clang_format_fallbackStyle` but exactly how do you "Click Edit, Copy to Settings"? I don't see "Copy to Settings" under the "Edit" menu and I don't see "Edit" anywhere else... – Michael Jul 27 '23 at 13:29
64
  • Go File -> Preferences -> Settings
  • Search for C_Cpp.clang_format_fallbackStyle
  • Change from "Visual Studio" to "LLVM", "Google" or "WebKit"
Chris Drew
  • 14,926
  • 3
  • 34
  • 54
  • 6
    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 02 '18 at 15:01
  • 3
    @MohammadDehghan Try `"C_Cpp.clang_format_fallbackStyle": "WebKit",`, this style has 4 space indention – SantaXL Apr 10 '18 at 09: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 09 '19 at 12:03
  • @Kapocsi If you read my comment from May 30th for the answer above this one, I already mentioned this. – Adam Erickson Jun 10 '19 at 00:20
  • @AdamErickson Sure, but the OP explicitly stated that they've already installed the C/C++ extension from marketplace. By saying, "For MacOS, clang-format is not installed by default", you seem to be implying that it isn't installed with the C/C++ extension. Furthermore, you say "If you do have clang-format installed", implying that the OP might not have it installed...which they already stated was not the case. In my opinion, your answer below should be the accepted one, but your comment above is unclear. – Kapocsi Jun 10 '19 at 03:06
  • @Kapocsi See my earlier comments regarding the benefits of installing `clang-format` outside of VS Code, in particular for the Google style. – Adam Erickson Jun 11 '19 at 05:27
20

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"

vk-code
  • 950
  • 12
  • 22
2

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.

Adam Erickson
  • 6,027
  • 2
  • 46
  • 33
  • 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 03:40
-1

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

-3

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 4
    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. –  Aug 22 '17 at 22:52
  • [Visual Studio](https://en.wikipedia.org/wiki/Microsoft_Visual_Studio) or [Visual Studio Code](https://en.wikipedia.org/wiki/Visual_Studio_Code)? – Peter Mortensen Jun 23 '20 at 15:02