Skip to main content

Using clang-format to Automatically Format and Indent Your Source Code

·459 words·3 mins
Clang-Format Format Indent
Table of Contents

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:

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" in settings.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.

Related

Intel’s Upcoming Arc B380 Graphics Card
·539 words·3 mins
Intel B380
How to Install Apache MySQL PHP (LAMP Stack) on Ubuntu 24.04
·613 words·3 mins
Ubuntu 24.04 Apache Mysql PHP
AMD Gears Up to Launch Dual 3D V-Cache Ryzen 9000 CPUs
·564 words·3 mins
AMD 3D V-Cache Ryzen 9000