Skip to content
| Marketplace
Sign in
Visual Studio Code>Formatters>CMake Format (cmake-fmt)New to Visual Studio Code? Get it now.
CMake Format (cmake-fmt)

CMake Format (cmake-fmt)

Parallel Dimension

|
1,694 installs
| (0) | Free
Stop arguing about CMake formatting. Fast, grammar-aware formatter for CMakeLists.txt and .cmake files.
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

cmake-fmt logo

cmake-fmt

crates.io downloads

Fast CMake formatter with grammar-aware line wrapping and intelligent indentation.

Examples

Line Wrapping & Indentation

Before:

TARGET_LINK_LIBRARIES(myapp PUBLIC Qt6::Core Qt6::Widgets Qt6::Network Boost::system Boost::filesystem PRIVATE spdlog::spdlog fmt::fmt nlohmann_json::nlohmann_json OpenSSL::SSL OpenSSL::Crypto CURL::libcurl)

After:

target_link_libraries(myapp
	PUBLIC
		Qt6::Core
		Qt6::Widgets
		Qt6::Network
		Boost::system
		Boost::filesystem
	PRIVATE
		spdlog::spdlog
		fmt::fmt
		nlohmann_json::nlohmann_json
		OpenSSL::SSL
		OpenSSL::Crypto
		CURL::libcurl
)

Condition Wrapping

Before:

if(NOT JUMBO_BUILD_MODE STREQUAL "BATCH" AND NOT JUMBO_BUILD_MODE STREQUAL "GROUP")
endif()

After:

if(NOT JUMBO_BUILD_MODE STREQUAL "BATCH"
	AND NOT JUMBO_BUILD_MODE STREQUAL "GROUP"
)
endif()

Command Casing & Grammar Awareness

Before:

FIND_PACKAGE(Boost REQUIRED COMPONENTS system filesystem thread)
INSTALL(TARGETS myapp mylib RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib/static)

After:

find_package(Boost REQUIRED COMPONENTS system filesystem thread)
install(TARGETS myapp mylib RUNTIME DESTINATION bin LIBRARY DESTINATION lib ARCHIVE DESTINATION lib/static)

Installation

VS Code Extension

Install from the VS Code Marketplace.

Enable format-on-save by adding this to your settings.json:

{
  "[cmake]": {
    "editor.defaultFormatter": "cmake-fmt.cmake-fmt",
    "editor.formatOnSave": true
  }
}

Cargo

cargo install cmake-fmt

Pre-built Binaries

Download pre-built binaries from GitHub Releases (coming soon).

Supported platforms:

  • Linux x64
  • Linux ARM64
  • macOS x64 (Intel)
  • macOS ARM64 (Apple Silicon)
  • Windows x64
  • Windows ARM64

Docker

Pull from Docker Hub:

docker pull paralleldimension/cmake-fmt

Or from GitHub Container Registry:

docker pull ghcr.io/sandercox/cmake-fmt

Format files using Docker:

docker run --rm -v "$(pwd):/work" -w /work paralleldimension/cmake-fmt cmake-fmt -i CMakeLists.txt

Usage

Format a file to stdout:

cmake-fmt CMakeLists.txt

Format in-place:

cmake-fmt -i CMakeLists.txt

Check if files are formatted (exits with code 1 if changes needed):

cmake-fmt --check CMakeLists.txt

Show formatting diff:

cmake-fmt --diff CMakeLists.txt

Format all CMake files in the current directory:

cmake-fmt -i .

Recursively format an entire project:

cmake-fmt -ri src/

Use in CI to enforce formatting:

cmake-fmt --check -r .

Create a .cmake-fmt-ignore file (gitignore syntax) to exclude paths:

build/
third_party/
generated/*.cmake

.cmake-fmt-ignore is honoured both when walking directories and when a file is piped through stdin with --assume-filename, so editor format-on-save skips the same files, which are passed through unchanged. As in git, an excluded directory is final: build/ cannot be undone by a later !build/keep.cmake, though build/* can.

Two limits worth knowing.

The stdin path consults .cmake-fmt-ignore and --ignore-file only. The directory walk additionally honours .ignore, .gitignore (when the tree is inside a git repository), .git/info/exclude, and your global gitignore — so a path excluded solely by one of those is skipped on the command line but still formatted on save. Put it in .cmake-fmt-ignore if you want both to skip it.

And a file named explicitly on the command line is always formatted, ignore rules or not. A directory named explicitly is not: if .cmake-fmt-ignore excludes it, it is skipped however you reach it, including cmake-fmt -r . run from inside it. The .gitignore family does not get that treatment — a directory excluded only by .gitignore is still walked when you name it.

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: --ignore-file ranks below all of them on the walk, so a negation in .gitignore or .ignore can override it there and has no counterpart on the stdin path.

--ignore-file is resolved against the working directory, and on the stdin path its directory patterns are only tested against directories at or below that working directory. A walk tests them against every directory at or below its own root, and the stdin path has no root to use — so for a file outside the working directory, cmake-fmt - --assume-filename … can format something that cmake-fmt -r rooted next to that file would skip. Running both from the same directory removes most of the difference, though not all of it. From proj/sub, with ig.txt holding * and !*.cmake, cmake-fmt -r .. --ignore-file ig.txt skips the whole tree — * matches the directory proj, and an excluded directory is never descended into — while cmake-fmt - --assume-filename x.cmake --ignore-file ig.txt formats the file, because proj is above the working directory and so is not consulted at all. Note the alternative would be worse: a pattern like build/ or tmp/ matches by basename, so testing every ancestor would let a directory far above your project disable the entire run.

Pointing --ignore-file at something that cannot be read is an error rather than something silently ignored — a typo there would otherwise format every file you meant to exclude, with no diagnostic at all. /dev/null, a <(...) pattern list and /dev/stdin — a pipe or a terminal — all work: a source that can only be read once is copied first, so every reader sees the same patterns. A directory, an unreadable file, and anything larger than 1 MiB are refused, one-shot sources included; the bound is never applied by truncating, which would cut a pattern in half.

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 — cmake-fmt -r sub, -r ./sub, -r sub/../sub and -r link-to-sub all agree with each other and with the stdin path. Reported paths still use the spelling you gave.

If you use pre-commit, lint-staged or a similar hook, note that those pass an explicit file list, which is the one invocation that ignores .cmake-fmt-ignore entirely. Pass the directory instead if you want the ignore rules applied.

When going through your files for the first time, you can use --interactive to review changes hunk-by-hunk and choose which ones to apply or skip/disable.

Configuration

cmake-fmt searches for config files upward from the formatted file's directory. Config file names (in priority order):

  1. .cmake-fmt (YAML format)
  2. .cmake-fmt.yml
  3. .cmake-fmt.yaml
  4. .cmake-fmt.tml
  5. .cmake-fmt.toml

Multiple config files can coexist — root-level defaults are merged with directory-level overrides. Add root: true to a config file to stop inheriting from parent directories.

Example Config

indent_width = 2
use_tabs = false
command_case = "lowercase"
max_line_length = 120

Configuration Reference

Setting Type Default Description
root boolean false Stop searching parent directories for config files
disable_format boolean false Skip formatting entirely
indent_width integer 4 Number of spaces per indent level
max_line_length integer 80 Max line length (0 = unlimited)
use_tabs boolean true Use tabs for indentation
command_case enum lowercase Built-in command casing: lowercase, uppercase, preserve
user_command_case enum infer User-defined command casing: lowercase, uppercase, preserve, infer
max_blank_lines integer 1 Maximum consecutive blank lines allowed
line_ending enum auto Line ending style: auto, lf, crlf
closing_style enum remove Closing statement style: preserve, remove, force
force_break_keywords boolean false Always break keywords onto separate lines
final_newline enum preserve Final newline: preserve, remove, force (also accepts true/false; leave accepted as alias for preserve)
comment_style enum hash_space Comment formatting: preserve, hash_space, hash_no_space
source_grouping enum none Group source files: none, headers_first, sources_first
sort_sources enum none Sort source file lists: none, alphabetical
collapse_empty_flags boolean true Collapse no-argument flags inline with preceding positional args
inline_single_keyword boolean false Keep single keyword inline with args, single-indent values
control_flow_space_before_paren boolean false Insert space before ( in control flow statements
space_between_command_parens boolean false Insert space inside command parentheses
indent_closing_paren boolean false Indent closing ) one level in multiline commands

Per-Setting Examples

root

Prevents inheriting settings from .cmake-fmt files in parent directories. When root: true is set, cmake-fmt starts with default values and only applies settings from this file and any closer (child) config files.

This is useful for monorepos or subdirectories that need completely independent formatting rules.

Without root: true (default behavior):

/project/.cmake-fmt          # indent_width: 2, max_line_length: 100
/project/lib/.cmake-fmt      # use_tabs: false
# Files in /project/lib/ get: indent_width=2 + max_line_length=100 + use_tabs=false (merged)

With root: true:

/project/.cmake-fmt          # indent_width: 2, max_line_length: 100
/project/lib/.cmake-fmt      # root: true, use_tabs: false
# Files in /project/lib/ get: use_tabs=false + all other settings at defaults (parent ignored)

indent_width

Controls spaces per indentation level (when use_tabs=false).

indent_width = 2:

if(BUILD_TESTING)
  add_subdirectory(tests)
endif()

indent_width = 4 (default):

if(BUILD_TESTING)
    add_subdirectory(tests)
endif()

max_line_length

Controls when lines are wrapped. Setting 0 disables line length limits.

max_line_length = 40:

target_link_libraries(app
	PUBLIC
		Boost::system
		Boost::filesystem
		Qt6::Core
)

max_line_length = 80 (default):

target_link_libraries(app PUBLIC Boost::system Boost::filesystem Qt6::Core)

command_case

Controls casing for built-in CMake commands.

command_case = lowercase (default):

add_executable(myapp main.cpp)
target_link_libraries(myapp PUBLIC Boost::system)

command_case = uppercase:

ADD_EXECUTABLE(myapp main.cpp)
TARGET_LINK_LIBRARIES(myapp PUBLIC Boost::system)

command_case = preserve: Preserves original casing from source file. ("leave" is accepted as a backward-compatible alias.)

use_tabs

use_tabs = true (default): Uses tab characters for indentation.

use_tabs = false: Uses spaces for indentation (count controlled by indent_width).

closing_style

Controls whether closing statements like endif() include the condition.

closing_style = remove (default):

if(BUILD_TESTING)
	enable_testing()
endif()

closing_style = force:

if(BUILD_TESTING)
	enable_testing()
endif(BUILD_TESTING)

closing_style = preserve: Preserves original closing style from source file. ("leave" is accepted as a backward-compatible alias.)

force_break_keywords

When true, always breaks keywords onto their own lines, even if the command would fit on one line.

force_break_keywords = false (default):

target_link_libraries(app PUBLIC Boost::system)

force_break_keywords = true:

target_link_libraries(app
	PUBLIC
		Boost::system
)

comment_style

Controls comment formatting.

comment_style = hash_space (default): Ensures one space after #:

# This is a comment

comment_style = hash_no_space: Removes space after #:

#This is a comment

comment_style = preserve: Preserves original comment formatting. ("leave" is accepted as a backward-compatible alias.)

max_blank_lines

Limits consecutive blank lines. For example, max_blank_lines = 1 (default) allows at most one blank line between statements.

final_newline

final_newline = preserve (default): Preserves the original file's trailing newline state. If the input file ended with a newline, the output will too. If it didn't, the output won't add one. ("leave" is accepted as a backward-compatible alias.)

final_newline = force: Ensures the file ends with a newline character. Equivalent to the previous true setting.

final_newline = remove: Strips any trailing newline from the output. Equivalent to the previous false setting.

For backward compatibility, true and false are still accepted (mapped to force and remove respectively).

disable_format

When true, skips formatting entirely. Useful for temporarily disabling formatting in specific directories via directory-level config.

sort_sources

sort_sources = alphabetical: Sorts file lists alphabetically, case-insensitively.

sort_sources = none (default): Preserves original file order.

Which lists get reordered

sort_sources and source_grouping both reorder arguments, so both apply only where a list is known to be unordered:

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 containing FLAGS, 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 to x.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: with one_value_keywords: [FROM] and sortable_positional: true, my_copy(FROM base.cpp z.cpp a.cpp) sorts z.cpp a.cpp. If that tail is order-significant — a destination after a source, say — do not set sortable_positional on 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.

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
  • Your Privacy Choices
  • Consumer Health Privacy
© 2026 Microsoft