d2
modern declarative diagram scripting language
TLDR
Compile a D2 diagram to SVG
SYNOPSIS
d2 [options] input.d2 [output]
DESCRIPTION
D2 is a modern diagram scripting language that turns text into diagrams. It uses a declarative syntax to describe nodes, connections, and layouts, producing professional-looking diagrams without manual positioning.
D2 supports various diagram types including flowcharts, entity-relationship diagrams, sequence diagrams, and class diagrams. The language is designed to be readable and version-control friendly, with automatic layout handling.
The tool offers multiple layout engines with different strengths. Watch mode enables live preview during editing. Themes provide consistent styling, and the sketch mode creates a hand-drawn aesthetic.
PARAMETERS
INPUT
D2 source file to compile.OUTPUT
Output file path (format inferred from extension).--format FORMAT
Output format: svg, png, pdf.--layout ENGINE
Layout engine: dagre (default), elk, tala.--theme ID
Theme ID (0-200+).--watch, -w
Watch input file and recompile on changes.--sketch
Render in hand-drawn sketch style.--pad N
Padding around diagram in pixels.fmt
Format/reindent D2 source file.--help
Display help information.
CAVEATS
Complex diagrams may require layout hints for optimal appearance. Some features require the TALA layout engine (proprietary). Large diagrams can be slow to render. PNG output requires additional dependencies.
HISTORY
D2 was created by Terrastruct and released as open source in 2022. It was designed as a modern alternative to older diagram tools like Graphviz, with better defaults, more features, and improved aesthetics out of the box.
