Introduction #
Messy, inconsistent code formatting makes your source harder to read, harder to review, and more error-prone. Luckily, you don’t have to manually align braces or fix indentation. clang-format, part of the LLVM project, can automatically format your code according to consistent rules — and even match your team’s style guide.
This blog will show you:
- What
clang-format
is and why you should use it - How to install it
- How to run it on your code
- How to customize formatting rules
- How to integrate it into your workflow
What Is clang-format? #
clang-format
is a command-line tool that automatically reformats C, C++, Objective-C, Java, JavaScript, TypeScript, Protobuf, and other languages supported by LLVM’s Clang frontend. It enforces consistent indentation, spacing, and code layout based on predefined or custom style rules.
Benefits:
- Keeps your code clean and consistent
- Reduces style-related code review comments
- Saves time compared to manual formatting
- Works across multiple languages and editors
Step 1 – Installing clang-format #
On Ubuntu/Debian #
sudo apt update
sudo apt install clang-format
On macOS (Homebrew) #
brew install clang-format
On Windows #
Install it via:
- LLVM official installer
- Or with
choco
:
choco install llvm
Step 2 – Basic Usage #
To format a file in place:
clang-format -i myfile.cpp
Without -i
, it outputs the formatted version to stdout
:
clang-format myfile.cpp
Step 3 – Choosing a Style #
clang-format
comes with built-in styles:
LLVM
(default)Google
Mozilla
WebKit
Microsoft
GNU
Example:
clang-format -i -style=Google myfile.cpp
Step 4 – Creating a .clang-format File #
For project-wide consistency, create a .clang-format
file in the project root:
BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 100
You can generate a base config from a style:
clang-format -style=Google -dump-config > .clang-format
Then edit the file to suit your needs.
Step 5 – Formatting Multiple Files #
Format all .cpp
and .h
files in a project:
find . -regex '.*\.(cpp\|h)' -exec clang-format -i {} \;
Step 6 – Editor Integration #
Most modern editors have clang-format
integration:
VS Code
- Install the Clang-Format extension.
- Set
"C_Cpp.clang_format_fallbackStyle": "Google"
insettings.json
.
Vim
autocmd BufWritePre *.cpp,*.h execute ':silent! !clang-format -i %'
CLion / Visual Studio
- Built-in support — enable in preferences.
Step 7 – CI/CD Integration #
You can enforce formatting in your CI pipeline:
clang-format --dry-run --Werror **/*.cpp
This fails the build if files are not formatted correctly.
Example Before/After #
Before:
int main(){int x= 1; if(x>0){std::cout<<"Hello";}}
After (clang-format -style=Google
):
int main() {
int x = 1;
if (x > 0) {
std::cout << "Hello";
}
}
Conclusion #
With clang-format
, you can keep your source code neat and uniform without spending time on manual indentation and spacing. Whether you’re a solo developer or part of a large team, integrating it into your daily workflow can greatly improve code readability and collaboration.