Clang-format

Clang-format is a tool to automatically apply formatting rules on C/C++ code.

$ sudo apt install clang-format
$ clang-format --version
$ clang-format -h

Clang-format allows you to disable the formatting rules on a scope:

// clang-format off
some code here
// clang-format on

Common usage

Run clang-format and automatically (-i) fix the files.

$ clang-format -i file.cpp [...]

You can create a .clang-format file to define the style applied.

BasedOnStyle: LLVM
Language: Cpp
IndentWidth: 4
ColumnLimit: 80
UseTab: Never

You can sort of preview the changes:

$ clang-format -output-replacements-xml [...]
<?xml version='1.0'?>
<replacements xml:space='preserve' incomplete_format='false'>
<replacement offset='0' length='0'>  </replacement>
</replacements>