How to Configure C++ Code Formatting in Visual Studio Code
Tags: cpp, vscode, tutorial, productivity
Published: 2023-03-18 18:10:57

Visual Studio Code (VS Code) is a popular open-source code editor that supports a wide range of programming languages, including C++. One of the key features of VS Code is its ability to automatically format your code to make it more readable and consistent. In this article, we'll show you how to configure C++ code formatting in VS Code using the built-in formatting options.

**Introduction**
Code formatting is an important aspect of writing maintainable and readable code. In C++, there are several popular coding styles used by different organizations and communities, such as Google, Microsoft, LLVM, and Mozilla. In this blog post, we will explore how to use the C/C++ extension for Visual Studio Code to automatically format C++ code according to these styles.

**Step 1: Open the Settings Editor**
To configure the C++ formatting options in VS Code, you'll need to open the settings editor. You can do this by clicking on the gear icon in the lower-left corner of the VS Code window and selecting "Settings". Alternatively, you can use the keyboard shortcut Ctrl + , on Windows or Cmd + , on Mac to open the settings editor.

**Step 2: Configure the Formatting Style**
Once you have the settings editor open, you can search for the C++ formatting options by typing "C_Cpp.clang_format_fallbackStyle" into the search bar. This will show you the available formatting styles that you can use, such as "Google", "LLVM", or "Mozilla".

To configure the formatting style, simply click on the pencil icon next to the "C_Cpp.clang_format_fallbackStyle" option and select the desired style from the dropdown menu. Once you have selected the style, the C++ code in your project will be automatically formatted according to that style.

**Step 3: Customize Additional Formatting Options**
In addition to the built-in formatting styles, you can also customize additional formatting options by creating a .clang-format file in your project directory or in your home directory. This file can be used to specify a variety of formatting options, such as indentation, line length, and spacing.

To create a .clang-format file, simply create a new file in your project directory or in your home directory and name it .clang-format. Then, open the file in a text editor and add your preferred formatting options. Here's an example of what a .clang-format file might look like:

### Google

```yaml
BasedOnStyle: Google
IndentWidth: 4
TabWidth: 4
UseTab: Never
ColumnLimit: 80
```

### Microsoft
The Microsoft C++ code formatting style is based on the C++ Core Guidelines and is used by many Microsoft projects. Here's an example .clang-format file that configures the Microsoft style:

```yaml
Language: Cpp
BasedOnStyle: Google
IndentWidth: 4
AccessModifierOffset: -4
AlignAfterOpenBracket: Align
AlignConsecutiveAssignments: false
AlignConsecutiveDeclarations: false
AlignEscapedNewlinesLeft: true
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: false
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: false
AllowShortFunctionsOnASingleLine: None
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakAfterReturnType: None
AlwaysBreakBeforeMultilineStrings: false
AlwaysBreakTemplateDeclarations: true
```

### LLVM
The LLVM project uses a code formatting style that is similar to the Google style, but with some differences. Here's an example .clang-format file that configures the LLVM style:

```yaml
Language: Cpp
BasedOnStyle: Google
IndentWidth: 2
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ColumnLimit: 80
PenaltyBreakAssignment: 2
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
SpaceBeforeParens: Never
```

### Mozilla
The Mozilla C++ code formatting style is used by many Mozilla projects. Here's an example .clang-format file that configures the Mozilla style:

```yaml
Language: Cpp
BasedOnStyle: Google
IndentWidth: 2
UseTab: Never
TabWidth: 8
ColumnLimit: 99
PointerAlignment: Right
AccessModifierOffset: -2
BreakBeforeBraces: Allman
DerivePointerAlignment: true
AllowShortFunctionsOnASingleLine: Empty
AllowShortIfStatementsOnASingleLine: false
AllowShortLoopsOnASingleLine: false
AllowShortBlocksOnASingleLine: false
AlwaysBreakAfterDefinitionReturnType: None
AlwaysBreakTemplateDeclarations: true
```

### References
1. Google style
    * [Google C++ Style Guide](https://google.github.io/styleguide/cppguide.html)
    * [Google C++ Style Guide Example](https://github.com/google/styleguide/blob/gh-pages/intellij-java-google-style.xml)
2. Microsoft style
    * [Microsoft C++ Core Guidelines](https://docs.microsoft.com/en-us/cpp/cpp/cpp-core-guidelines?view=msvc-160)
    * [Microsoft C++ Style Guide Example](https://github.com/MicrosoftDocs/visualstudio-docs/blob/master/docs/cpp/codesnippet/CPP/using-a-c-cpp-code-style-settings-file.md)
3. LLVM style
    * [LLVM Coding Standards](https://llvm.org/docs/CodingStandards.html)
    * [LLVM C++ Style Guide Example](https://llvm.org/docs/CodeStandards/CppConventions.html#general-formatting)
4. Mozilla style
    * [Mozilla C++ Programming Guide](https://developer.mozilla.org/en-US/docs/Mozilla/Developer_guide/Coding_Style)
    * [Mozilla C++ Style Guide Example](https://dxr.mozilla.org/mozilla-central/source/.clang-format)