【VSCode】How to customize C++’s coding style in VSCode

Zam Huang
3 min readAug 30, 2019

--

Brief

Zam had been try lots of IDE for developing C++ project, but none of these are smart for coding style, e.g. Sublime、Dev C++、Qt Creator. But fortunately I found the VSCode and the extension named C/C++ for Visual Studio Code, although it is not good enough, but I am satisfy for that.

How to customize your own coding style rule

Please install your VSCode and Extension C/C++ for Visual Studio Code first

Choose your favor argument

Argument Link: Clang Format Style Options

I would like to recommend some argument which I feel useful

  • BasedOnStyle:Choose your side, I chose Google!
LLVM A style complying with the LLVM coding standards
Google A style complying with Google’s C++ style guide
Chromium A style complying with Chromium’s style guide
Mozilla A style complying with Mozilla’s style guide
WebKit A style complying with WebKit’s style guide
Microsoft A style complying with Microsoft’s style guide
  • IndentWidth:the space number in front of code each line
IndentWidth: 4
void f() {
someFunction();
if (true, false) {
f();
}
}
  • ColumnLimit:the line numbers, 0 means unlimit
  • SpacesBeforeTrailingComments
SpacesBeforeTrailingComments: 3
void f() {
if (true) { // foo1
f(); // bar
} // foo
}
  • DerivePointerAlignment:If true, analyze the formatted file for the most common alignment of & and *. Pointer and reference alignment styles are going to be updated according to the preferences found in the file. PointerAlignment is then used only as fallback.
  • Cpp11BracedListStyle:It is all about {}
true:                                false:
vector<int> x{1, 2, 3, 4}; vs. vector<int> x{ 1, 2, 3, 4 };
vector<T> x{{}, {}, {}, {}}; vector<T> x{ {}, {}, {}, {} };
f(MyMap[{composite, key}]); f(MyMap[{ composite, key }]);
new int[3]{1, 2, 3}; new int[3]{ 1, 2, 3 }
  • PointerAlignment: Left、Right、Middle
// Left
int* a;// Right
int *a;// Middle
int * a;
  • AccessModifierOffset:The extra indent or outdent of access modifiers, e.g. public:

Modify Setting

STEP 1 Go Preferences -> Settings

STEP 2–1 Search C_Cpp.clang_format_fallbackStyle

STEP 2–2 Modify the file:setting.json directly

Windows %APPDATA%\Code\User\settings.json

macOS $HOME/Library/Application Support/Code/User/settings.json

Linux $HOME/.config/Code/User/settings.json

STEP 3 The Google style is really good, but still change to make your favor

modify C_Cpp.clang_format_fallbackStyle and the original value wasVisual Studio

"C_Cpp.clang_format_fallbackStyle": "{ BasedOnStyle: Google, IndentWidth: 4, ColumnLimit: 0}"

The result

Setting
setting.json

Reference

VSCode Setting

Clang Format Style Options

If you like this one or feel helpful, please applause or follow to Zam, I am really appreciate you doing that

--

--

Zam Huang

一個記錄著自己人生過程的工程師。A Software Engineer at Ruckus Network in Taiwan。 IG, Linkedin:@zam_huang, stack overflow: user:2613194