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:
On Ubuntu:
For the Windows version, you can download it here:
Once installed, verify it:
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:
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.
Related Topics¶
- Building C++ Projects with Bazel | C++那些事
- Debugging Bazel Builds with LLDB | C++那些事
- CMake Essentials in Practice | C++那些事
- Getting Started with the Conan C++ Package Manager | C++那些事
- C++ Insights Compile-Time Observation | C++那些事
- Interactive C++ in Jupyter Notebook | C++那些事
- C++ string to int | C++那些事
- The C++ dbg-macro Debugging Macro | C++那些事
- GCC Compilation Flags Explained | C++那些事
- Debugging C++ Programs with GDB | C++那些事