cmakefmt takes your CMake source files (CMakeLists.txt and *.cmake), applies a
consistent set of formatting rules, and outputs the result. It normalizes command
casing, keyword casing, indentation, argument wrapping, alignment, and more -- with
nearly 40 configuration options. If a file cannot be parsed, the
formatter returns it unchanged -- it never silently corrupts your code.
- Command casing -- normalizes
PROJECT,Set,IFto lowercase (or uppercase) - Keyword & literal casing -- normalizes
public,PrivatetoPUBLIC,PRIVATE;on/OfftoON/OFF - Argument wrapping -- multiple strategies (cascade, vertical) with
firstArgSameLine,wrapArgThreshold, and per-command overrides - Alignment -- aligns property values, condition arguments, and keyword groups
- Comment formatting -- reflowing, indentation, and trailing comment alignment
- Sorting -- alphabetical sorting of argument lists under configurable keywords
- Blank line control --
maxBlankLines,minBlankLinesBetweenBlocks,blankLineBetweenSectionsfor structural spacing - Generator expressions --
$<...>expressions are recognized and treated atomically, never broken across lines - Line endings & final newline -- LF/CRLF/auto normalization, trailing whitespace removal
- Per-command overrides -- fine-grained control over individual commands (e.g.
spaceBeforeParenforif) - Inline pragmas --
# cmakefmt: off/on/skipto suppress formatting,push { ... }/popfor scoped config overrides - Config inheritance --
extendskey for sharing base configs across projects - Safe by default -- unparseable input is returned unchanged; never silently corrupts
cargo install cmakefmt-cliDownload prebuilt binaries for your platform from GitHub Releases:
- linux-x64, linux-arm64
- macos-x64, macos-arm64
- windows-x64, windows-arm64
Add cmakefmt as a WASM plugin in your dprint.json:
{
"plugins": [
"https://github.com/azais-corentin/cmakefmt/releases/latest/download/cmakefmt-dprint.wasm"
]
}Format a file and print to stdout:
cmakefmt CMakeLists.txtFormat in place:
cmakefmt --write CMakeLists.txt # also: --inplaceCheck formatting without modifying files (exits with code 1 if changes are needed):
cmakefmt --check CMakeLists.txtShow a unified diff of what would change:
cmakefmt --diff CMakeLists.txtFormat from stdin:
cat CMakeLists.txt | cmakefmt --stdinCreate a .cmakefmt.toml (or cmakefmt.toml) in your project root. The formatter
discovers config by walking from the formatted file's directory upward to the
filesystem root. All keys use camelCase (snake_case is also accepted).
lineWidth = 120
indentWidth = 4
indentStyle = "space"
commandCase = "lower"
keywordCase = "upper"
closingParenNewline = true
lineEnding = "lf"
finalNewline = true
trimTrailingWhitespace = true
endCommandArgs = "remove"
sortArguments = ["SOURCES", "FILES"]
alignPropertyValues = true
ignorePatterns = ["build/**", "third_party/**"]
ignoreCommands = ["ExternalProject_Add"]
[perCommandConfig.if]
spaceBeforeParen = true
[perCommandConfig.elseif]
spaceBeforeParen = trueFor the full list of options and their defaults, see the Configuration Reference.
Most editors can invoke an external command to format the current buffer. Configure your editor to pipe through:
cmakefmt --stdin --assume-filename <path>The --assume-filename flag tells cmakefmt which file path to use for config
discovery (walking upward to find .cmakefmt.toml).
If you use dprint, add the cmakefmt WASM plugin to your
dprint.json (see the Installation section above), then use dprint's editor
extensions for VS Code, Neovim, or other supported editors.
Full documentation is available at azais-corentin.github.io/cmakefmt.
MIT -- see LICENSE for details.