Automatic Formatting

To format all code, use Treefmt.

Pre-commit Hooks

To automatically run formatting and code checks before creating git commits, install pre-commit hooks in your local workspace:

pre-commit install

This ensures that common sanity checks run locally on commit, treefmt runs if installed, and gitlint validates commit messages against Commit Message (both pre-commit and commit-msg hooks are installed by default). All hooks run offline using local system tools and are included in the development container.

To run all pre-commit-stage checks manually across all files:

pre-commit run --all-files

To run the gitlint commit-message check manually:

pre-commit run gitlint --hook-stage commit-msg \
  --commit-msg-filename "$(git rev-parse --git-path COMMIT_EDITMSG)"

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.