Automatic Formatting

To format all code, use Treefmt.

Usage

Formatting a file:

treefmt <file_to_check>

Formatting all files at once:

treefmt

IDE Integration

VS Code

To use treefmt from within VS Code install the treefmt-vscode plugin.

And configure the plugin by adding the following lines into the .vscode/settings.json file:

"editor.defaultFormatter": "ibecker.treefmt-vscode",
"editor.formatOnSave": true

Vim Builtin Format Operator gq

You may use vim’s builtin format operator gq (see :help gq in case you don’t know it) b pointing its formatprg option to clang-format, i.e.:

.vimrc
"" Make sure to have 'filetype' activated, e.g.
filetype plugin indent on

"" set formatexpr and formatprg when filetype is cpp
au FileType cpp setlocal formatexpr= formatprg=clang-format\ -style=file

This (of course) assumes clang-format being in your $PATH. You can now apply the formatting e.g. by hitting gq on your selected text:

../../../../_images/gq.gif

Include Guard

Use #pragma once at the top of every header file. See Include Guard for the required format.