Skip to content

Last updated:2026-10-12

Formatting C++ Code in VS Code

C++那些事: Formatting Your Code Style Step by Step

0.Introduction

This section explains from scratch how to format your own code style; everything shown uses a VS Code plugin.

1.Prerequisites

This guide uses the clang-format plugin; installation is straightforward:

On macOS:

brew install clang-format  #Mac OS

On Ubuntu:

apt install clang-format

For the Windows version, you can download it here:

https://llvm.org/builds/

Once installed, verify it:

clang-format --version

If you prefer not to use VS Code, you can also use the bare clang-format command directly!

Then install the following two plugins in VS Code:

  • c/c++
  • clang-format

Both can be downloaded from the extension marketplace.

After installation, some configuration is needed — see below.

2.Configuration

In the VS Code settings you can search for Clang_format_style; the default is file, and you do not need to change it here — though you can set it to something else. Let me show you that the default value here is file, which means formatting reads the .clang-format file in the current folder or a parent directory.

So all we need to do is generate this file; go to your code directory and run:

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

This generates the file using the Google style, and you can modify the file as you like.

Following the screenshot below, set up the clang-format plugin format in VS Code; then, inside a file, alt+shift+f will format the file. To avoid pressing this shortcut every time, you can check format on save so that the file is formatted whenever it is saved.

Comments