cmake-fmtFast CMake formatter with grammar-aware line wrapping and intelligent indentation. ExamplesLine Wrapping & IndentationBefore:
After:
Condition WrappingBefore:
After:
Command Casing & Grammar AwarenessBefore:
After:
InstallationVS Code ExtensionInstall from the VS Code Marketplace. Enable format-on-save by adding this to your
Cargo
Pre-built BinariesDownload pre-built binaries from GitHub Releases (coming soon). Supported platforms:
DockerPull from Docker Hub:
Or from GitHub Container Registry:
Format files using Docker:
UsageFormat a file to stdout:
Format in-place:
Check if files are formatted (exits with code 1 if changes needed):
Show formatting diff:
Format all CMake files in the current directory:
Recursively format an entire project:
Use in CI to enforce formatting:
Create a
Two limits worth knowing. The stdin path consults And a file named explicitly on the command line is always formatted, ignore
rules or not. A directory named explicitly is not: if The asymmetry runs both ways, so it is worth knowing which direction you are
in. The stdin path skips less than the walk, because of the sources above.
It can also skip more:
Pointing A directory named as the walk root is resolved before it is walked, so its
verdict is a property of the directory rather than of how you spelled it —
If you use When going through your files for the first time, you can use Configurationcmake-fmt searches for config files upward from the formatted file's directory. Config file names (in priority order):
Multiple config files can coexist — root-level defaults are merged with directory-level overrides. Add Example Config
Configuration Reference
Per-Setting Examples
|
| Command | What is sorted |
|---|---|
set(VAR a.cpp b.cpp) |
the values (not the CACHE <type> "<docstring>" form) |
add_library, add_executable |
the sources, after the target name |
target_sources |
PUBLIC / PRIVATE / INTERFACE, and FILE_SET's FILES |
list(APPEND\|PREPEND\|REMOVE_ITEM var …) |
the elements |
install(FILES\|PROGRAMS …) |
the file list |
source_group(… FILES …) |
the file list |
| your own commands | keywords named SOURCES, SRCS or FILES — see the note below |
That last row is a fallback, and only for a command_grammars entry in .cmake-fmt or a grammar auto-detected from cmake_parse_arguments — it is what such a grammar gets when it says nothing about sortability. A grammar file loaded with --grammar-file has no fallback: a keyword there is sortable only if sortable_keywords names it.
Everything else is left alone, because argument order usually carries meaning:
COMMAND holds an argv, PROPERTIES holds key/value pairs, file(RENAME a b)
holds source then destination, target_link_libraries holds link order.
Five things are never reordered even inside a list that is:
- A parenthesized group (
if((A AND B) OR C),NOT(x.cpp)) holds its position, and arguments do not move across it — it is one argument holding several, and what is inside it cannot be vetted. A paren inside a quoted or bracket argument is only a character:[[foo(1).cpp]]is a filename and sorts. - A variable reference or generator expression (
${GENERATED},"${GENERATED}",$<TARGET_OBJECTS:x>) holds its position, and files do not move across it — what it expands to is unknown. - A keyword-less run whose values don't all look like source files. A keyword
names what its values are;
set(VAR ...)does not, and the same shape holds sources in one project and compiler flags in the next. So flag shapes (-Wall,/O2,--input,A=1), library extensions (.a,.so,.lib) and extension-less names keep their order there. Under a keyword that vouches for them —install(FILES README LICENSE ...)— they still sort. - A
list(APPEND ${DYNAMIC} …)whose list variable is itself a reference, since the name cannot be read and therefore cannot be vetted. A dynamic target name does not block anything —add_library(${PROJECT_NAME} a.cpp b.cpp)still sorts. - A list whose variable names a search path, flag list or argument list:
CMAKE_MODULE_PATH,CMAKE_PREFIX_PATH, anything containingFLAGS, or a name ending in_PATH,_PATHS,_DIRS,_DIRECTORIES,_OPTIONS,_ARGS,_ARGUMENTS,_LIBS,_LIBRARIES,_PATTERNS. Matched case-insensitively, since project-local lists are often lowercase.
A positional list in a command cmake-fmt does not recognize is not reordered at
all — there is no grammar to say whether its order matters. Add a
command_grammars entry to opt one in.
To mark a list in your own command as unordered, name its keyword in
command_grammars:
command_grammars:
my_add_library:
one_value_keywords: [NAME]
multi_value_keywords: [SOURCES, COMMAND]
sortable_keywords: [SOURCES] # COMMAND keeps its order
Use sortable_positional: true for a command whose keyword-less arguments are a
file list. It reaches two runs, and only two:
- The leading run, the arguments before the first keyword. Its first
argument is always pinned, because in every command that has one it names the
list or the target rather than belonging to it — so
my_cmd(x.cpp z.cpp a.cpp)sorts tox.cpp a.cpp z.cpp, and a two-element list never reorders. - The run that overflows a leading single-value keyword, with nothing
pinned, because that keyword already consumed the name. This is what sorts
list(APPEND SRCS ...), and it applies to your grammar too: withone_value_keywords: [FROM]andsortable_positional: true,my_copy(FROM base.cpp z.cpp a.cpp)sortsz.cpp a.cpp. If that tail is order-significant — a destination after a source, say — do not setsortable_positionalon it.
A run after a leading flag is not reached, and neither is anything a
multi-value keyword owns (that is what sortable_keywords is for). Builtins
reach a run after a flag as well, because their grammars name those flags; a
sortable_positional grammar cannot yet say the same, and it cannot say that
its first positional is a target rather than the list — so a wrapper around
add_library holds where add_library itself would sort, which is the safe
direction to be wrong in.
To skip one command, put # cmake-fmt: no-sort on the line before it; that
suppresses both reordering passes.
source_grouping
source_grouping = headers_first:
Groups header files (.h, .hpp) before source files (.cpp, .c) in the same
lists sort_sources applies to.
set(SOURCES
main.h main.cpp
application.h application.cpp
)
source_grouping = sources_first:
Groups source files before header files.
source_grouping = none (default):
No grouping applied.
collapse_empty_flags
Controls whether no-argument flags (like REQUIRED, CONFIG) are kept inline or placed on their own line. Type-selector flags (like STATIC, SHARED) always stay inline with the target name regardless of this setting.
collapse_empty_flags = true && max_line_length=60 (default):
find_package(Boost REQUIRED CONFIG
COMPONENTS
system
filesystem
)
collapse_empty_flags = false && max_line_length=60:
find_package(Boost
REQUIRED
CONFIG
COMPONENTS
system
filesystem
)
inline_single_keyword
When true and a keyword command has exactly one keyword section, the keyword stays on the same line as the command's positional arguments, and values are single-indented instead of double-indented. When multiple keyword sections exist (e.g., PUBLIC + PRIVATE), the standard layout is always used regardless of this setting.
inline_single_keyword = false (default):
target_sources(mylib
PUBLIC
src/a.cpp
src/b.cpp
)
inline_single_keyword = true:
target_sources(mylib PUBLIC
src/a.cpp
src/b.cpp
)
Note: When multiple keyword sections exist (e.g., both PUBLIC and PRIVATE), the standard double-indented layout is always used regardless of this setting.
control_flow_space_before_paren
Inserts a space between the command name and ( for control flow and block statements only.
Regular commands like set(), message(), and add_library() are never affected.
control_flow_space_before_paren = false (default):
if(BUILD_TESTING)
add_subdirectory(tests)
endif()
control_flow_space_before_paren = true:
if (BUILD_TESTING)
add_subdirectory(tests)
endif ()
space_between_command_parens
Inserts a space inside the parentheses of commands. In single-line commands, both opening and
closing parens get an inner space. In multiline commands, the opening ( gets a trailing space
(when followed by content on the same line), but the closing ) stays at its normal indentation.
space_between_command_parens = false (default):
set(MY_VAR "value")
target_sources(mylib
PUBLIC
src/a.cpp
)
space_between_command_parens = true:
set( MY_VAR "value" )
target_sources( mylib
PUBLIC
src/a.cpp
)
indent_closing_paren
Indents the closing ) by one indent level in multiline commands. Has no effect on single-line
commands. Can be combined with space_between_command_parens.
indent_closing_paren = false (default):
target_sources(mylib
PUBLIC
src/a.cpp
src/b.cpp
)
indent_closing_paren = true:
target_sources(mylib
PUBLIC
src/a.cpp
src/b.cpp
)
Custom Command Grammars
The command_grammars setting is only available in config files (not via --style). It allows you to define custom command grammars for project-specific CMake functions.
Example .cmake-fmt:
command_grammars:
my_custom_command:
options:
- VERBOSE
one_value_keywords:
- NAME
- VERSION
multi_value_keywords:
- SOURCES
- HEADERS
For detailed grammar syntax, run:
cmake-fmt --help-grammar
CLI Reference
| Flag | Description |
|---|---|
-i, --in-place |
Format files in-place |
--check |
Check if files are formatted (exit 1 if changes needed) |
--dry-run |
Dry run mode (same as --check) |
--diff |
Show diff of formatting changes |
--interactive |
Interactive mode: review formatting changes hunk-by-hunk |
--style <STYLE> |
Override config inline (e.g., "indent_width=4,max_line_length=100") |
--export-grammar <FILE> |
Export detected custom grammars to a file (scanned from input files) |
--export-all-grammar <FILE> |
Export all grammars including builtins to a file |
--grammar-file <FILE> |
Import additional grammar file(s) (can be specified multiple times) |
--verbose |
Show verbose output during file scanning and analysis |
--assume-filename <PATH> |
Treat stdin as if formatting this file (resolves config/grammar from its path) |
-r, --recursive |
Recursively format files in directories |
--ignore-file <FILE> |
Path to additional ignore file (gitignore syntax) |
--line-ranges <RANGES> |
Format only specific line ranges (e.g., "1:5,10:15") |
--help-style |
Show all available style settings |
--help-grammar |
Show grammar file format and keyword types |
-h, --help |
Print help |
-V, --version |
Print version |
License
MIT — see LICENSE for details.