DEV Community

Dhananjay Patel
Dhananjay Patel

Posted on

How to Configure C++ Code Formatting in Visual Studio Code

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

BasedOnStyle: Google
IndentWidth: 4
TabWidth: 4
UseTab: Never
ColumnLimit: 80
Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

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:

Language: Cpp
BasedOnStyle: Google
IndentWidth: 2
MaxEmptyLinesToKeep: 1
NamespaceIndentation: All
ColumnLimit: 80
PenaltyBreakAssignment: 2
PenaltyReturnTypeOnItsOwnLine: 200
PointerAlignment: Left
SpaceBeforeParens: Never
Enter fullscreen mode Exit fullscreen mode

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:

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
Enter fullscreen mode Exit fullscreen mode

References

  1. Google style
  2. Microsoft style
  3. LLVM style
  4. Mozilla style

Top comments (1)

Collapse
 
piperunner profile image
Pipe Runner

Thanks, this is exactly what I was looking for.