d2
Convert textual descriptions into visual diagrams
TLDR
Compile and render a D2 source file into an output file
Watch live changes made to a D2 source file in the default web browser
Format a D2 source file
List available themes
Use a different theme for the output file (list available themes first to get the desired theme_id)
Make rendered diagrams look like hand sketches
SYNOPSIS
d2 [OPTIONS] INPUT_FILE [OUTPUT_FILE]
PARAMETERS
-t, --theme
Sets the diagram's visual theme (e.g., 'default', 'dark', 'sketchy', 'neutral', 'gray', 'cool').
-l, --layout
Specifies the graph layout engine to use (e.g., 'dagre', 'elk').
-o, --output
Defines the output file path and format (e.g., 'diagram.svg', 'doc.png'). If omitted, defaults to stdout or a derived filename.
--watch
Monitors the input file for changes and recompiles automatically. Useful for live preview.
-p, --pad
Sets the padding (in pixels) around the generated diagram.
--sketch
Enables a hand-drawn, sketchy style for the diagram, overriding the theme's default.
--font
Specifies the font family to be used in the diagram (e.g., 'Inter', 'Cascadia Code').
--target-id
Compiles only the diagram with the specified ID in a multi-diagram input file.
--animate
Enables animation for web output, useful for interactive diagrams (e.g., when embedding in HTML).
--config
Specifies a TOML configuration file for D2 settings, allowing global or project-specific defaults.
--force-update
Forces recompilation even if inputs haven't changed, bypassing cache.
-V, --version
Prints the D2 version information and exits.
-h, --help
Displays help message and exits.
DESCRIPTION
The d2 command-line interface compiles D2 diagramming language source files into various graphical formats such as SVG, PNG, PDF, and HTML.
D2 is an open-source, text-based diagramming language designed for creating diagrams that can be version-controlled, easily integrated into CI/CD pipelines, and rendered consistently across different environments. It aims to simplify the process of creating complex diagrams by defining them programmatically rather than through graphical user interfaces.
It leverages various graph layout engines (like Dagre and ELK) to automatically arrange elements, allowing users to focus on the logical structure and content of their diagrams. D2 supports numerous customization options, including themes, fonts, and sketch modes.
CAVEATS
d2 is not a standard Unix/Linux utility and must be installed separately (e.g., via `brew`, `npm`, or by downloading binaries). It relies on external layout engines, which might require additional configuration or dependencies for optimal performance. While powerful, its text-based nature might have a learning curve for users accustomed to GUI-based diagramming tools.
TEXT-AS-CODE PHILOSOPHY
D2 embodies the 'diagrams as code' principle, allowing diagrams to be stored, versioned, and diffed just like source code. This enables seamless integration into Git workflows, making diagram updates traceable, collaborative, and part of documentation-as-code initiatives.
EXTENSIBILITY
D2 is designed with extensibility in mind, allowing users to define custom shapes, styles, and even integrate with external data sources or APIs to generate dynamic diagrams. This flexibility makes it suitable for a wide range of use cases beyond simple static diagrams.
HISTORY
The d2 project was open-sourced by Terrastruct in 2022, aiming to provide a modern, versatile, and developer-friendly alternative for creating diagrams as code. It quickly gained traction within the developer community for its elegant syntax and robust features, becoming a popular choice for technical documentation and system design. Its development continues with a focus on improving layout quality, adding more output formats, and enhancing integration capabilities with various tools and platforms.
SEE ALSO
graphviz(1) - `dot` command for Graphviz diagramming, a foundational tool for graph visualization., plantuml - Another popular text-to-diagram generation tool, often invoked via a Java JAR.