Lesson 7 of 19 ↓
- 1.Fedora + Distrobox + Arch + AUR Setup Guide
- 2.Enabling Hibernate on Linux
- 3.A Practical Git Workflow
- 4.Useful Linux Commands & Tips
- 5.Mirroring Your Android Screen with Scrcpy
- 6.Downloading Media with Yt-dlp
- 7.Consistent C/c++ Style with Clang-format
- 8.Auto-mounting Drives at Boot
- 9.Customizing Grub
- 10.Linux Swap Files Explained
- 11.Zram: Compressed Swap in Ram
- 12.Custom App Launch Behavior Via .desktop Files
- 13.Custom Mime Icons in Gnome
- 14.Fixing Missing Dock Icons (startupwmclass)
- 15.Installing Questasim for Hdl Simulation
- 16.Building Python 2 From Source
- 17.Auto-Starting WhatsApp and Telegram (Minimized or Tray)
- 18.Custom Neovim Configuration Guide
- 19.Running Windows Applications on Linux Seamlessly with WinBoat (Docker)
1 min read
Consistent C/c++ Style with Clang-format
linux
clang-format enforces a single, consistent C/C++ style automatically, instead of relying on everyone to format code by hand the same way. This covers installing it, setting up a global style config, and wiring it into VS Code (or Windsurf) so it runs on every save.
Installation
# Ubuntu / Debian
sudo apt install clang-format
# Fedora / RHEL
sudo dnf install clang-tools-extraVerify:
clang-format --versionGlobal Configuration
A .clang-format file in the home directory applies to every project that doesn't have its own:
nvim ~/.clang-formatBasedOnStyle: Microsoft
IndentWidth: 2
TabWidth: 2
UseTab: Never
BraceWrapping:
AfterFunction: true
AfterClass: true
AfterStruct: true
AfterControlStatement: true
AfterNamespace: true
AfterEnum: true
SplitEmptyFunction: true
SplitEmptyRecord: true
SplitEmptyNamespace: trueThis configuration enforces:
- Microsoft style as a base
- 2-space indentation, spaces only (no tabs)
- Allman-style braces (opening brace on its own line)
- No collapsed single-line functions, even short ones
Editor Integration (vs Code / Windsurf)
Install the Clang-Format extension by xaver (extension ID xaver.clang-format), then add to settings.json (Ctrl+Shift+P → Preferences: Open Settings (JSON)):
{
"editor.formatOnSave": true,
"[c]": {
"editor.defaultFormatter": "xaver.clang-format"
},
"[cpp]": {
"editor.defaultFormatter": "xaver.clang-format"
},
"clang-format.executable": "clang-format",
"clang-format.style": "file",
"clang-format.fallbackStyle": "Microsoft"
}With this in place, clang-format runs on both C and C++ files automatically on save, reading its style from the nearest .clang-format file.
Notes & best practices:
- A
.clang-formatfile in a project root overrides the global~/.clang-format.- No symlinks or system-wide config copies are needed —
clang-format.style: "file"handles discovery.- Avoid the deprecated
_clang-formatfilename; use.clang-format.- The same setup works unchanged across Ubuntu, Fedora, and RHEL.
Comments
Loading the discussion…