Format C/C++ source files using the clang-format binary from the active NCS toolchain. Use when the user asks to format code, run clang-format, fix formatting, or clean up a file or folder in an NCS/Zephyr project.
Follow the same toolchain resolution logic as chsh-dev-ncs-env:
Check active terminal — look for an NCS version in the prompt (e.g., (v3.2.1)).
If found, use that version.
If no active terminal, ask:
"Which toolchain version should I use for formatting? (e.g., v3.2.1)"
Look up the bundle ID from the toolchain version:
jq -r '.toolchains[] | select(.ncs_versions[]=="<VERSION>").identifier.bundle_id' \
/opt/nordic/ncs/toolchains/toolchains.json
The clang-format binary is at:
/opt/nordic/ncs/toolchains/<BUNDLE_ID>/bin/clang-format
Priority order:
.clang-format in the project root (if it exists) — clang-format picks it up automatically./opt/nordic/ncs/<SDK_VERSION>/zephyr/.clang-format
Pass it explicitly: --style=file:<path>/.clang-formatIf neither is found, ask the user before proceeding.
src/ directory.If ambiguous, ask: "Format the whole project, a specific folder, or just one file?"
find <TARGET_DIR> -name "*.[ch]" -o -name "*.cpp" -o -name "*.hpp" | \
xargs /opt/nordic/ncs/toolchains/<BUNDLE_ID>/bin/clang-format -i
/opt/nordic/ncs/toolchains/<BUNDLE_ID>/bin/clang-format -i <FILE>
find <TARGET_DIR> -name "*.[ch]" | \
xargs /opt/nordic/ncs/toolchains/<BUNDLE_ID>/bin/clang-format --dry-run --Werror 2>&1 | head -60
| Toolchain Version | Bundle ID |
|---|---|
| v3.2.4 | 185bb0e3b6 |
| v3.2.0, v3.2.1 | 322ac893fe |
| v3.1.1 | 561dce9adf |
| v2.8.0 | 15b490767d |
| v2.6.0 | 580e4ef81c |
For any other version, always look up toolchains.json.
-i. No backup is created — ensure git is clean first if needed..clang-format, recommend copying from <SDK>/zephyr/.clang-format and setting ColumnLimit: 80 (Zephyr checkpatch enforces 80, not 100).HTTP_RESOURCE_DEFINE or ZBUS_CHAN_DEFINE may need /* clang-format off/on */ guards if the formatter fights checkpatch style.At the end of each conversation, review what was discovered and check whether any facts in this skill are new, corrected, or outdated (e.g. new bundle IDs, new clang-format quirks, macro guard patterns).
If updates are warranted:
AskQuestion.Do not modify this skill mid-conversation unless the user explicitly asks.