clang-format

automatic source code formatter for C-family languages

TLDR

Format file and output to stdout
$ clang-format [file.cpp]
Format file in place
$ clang-format -i [file.cpp]
Format with specific style
$ clang-format --style=[Google] [file.cpp]
Format using project config file
$ clang-format --style=file [file.cpp]
Format code from stdin
$ echo "int main(){}" | clang-format
Generate style config file
$ clang-format --style=[LLVM] --dump-config > [.clang-format]
Format specific lines only
$ clang-format --lines=[10:20] [file.cpp]
Check if file is formatted
$ clang-format --dry-run --Werror [file.cpp]

SYNOPSIS

clang-format [options] [file...]

DESCRIPTION

clang-format is an automatic code formatting tool for C, C++, Java, JavaScript, JSON, Objective-C, Protobuf, and C# source files. It is part of the LLVM/Clang project and formats code according to configurable style rules.The tool ships with several predefined styles (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, GNU) and supports extensive customization through .clang-format configuration files. When using --style=file, it searches parent directories for the nearest configuration file.clang-format integrates with most major editors and CI/CD pipelines. The --dry-run option with --Werror enables enforcement of formatting standards in automated checks without modifying files.

PARAMETERS

-i

Edit files in place
--style=style
Coding style (LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, file)
--dump-config
Dump configuration to stdout
--assume-filename=name
Filename for language detection when reading stdin
--lines=start:end
Format only specified line range
--dry-run
Don't write changes, exit with error if changes needed
--Werror
Treat formatting issues as errors
--fallback-style=style
Style when no .clang-format found
--verbose
Show files being processed

CONFIGURATION

Create .clang-format or _clang-format in project root:

$ BasedOnStyle: Google
IndentWidth: 4
ColumnLimit: 100

PREDEFINED STYLES

LLVM, Google, Chromium, Mozilla, WebKit, Microsoft, GNU

EDITOR INTEGRATION

Integrations available for VS Code, CLion, Vim, Emacs, Sublime Text, and most major editors.

INSTALL

sudo apt install clang-format
sudo apk add clang22-extra-tools
brew install clang-format

CAVEATS

Style "file" uses nearest .clang-format in parent directories. Some formatting choices are opinionated and may require configuration adjustments.

SEE ALSO

clang(1), clang-tidy(1)

RESOURCES

Source code · Documentation