LinuxCommandLibrary

d2

modern declarative diagram scripting language

TLDR

Compile a D2 diagram to SVG

$ d2 [diagram.d2] [output.svg]
copy
Compile to PNG format
$ d2 --format png [diagram.d2] [output.png]
copy
Watch for changes and auto-recompile
$ d2 --watch [diagram.d2] [output.svg]
copy
Use a specific layout engine
$ d2 --layout [elk] [diagram.d2] [output.svg]
copy
Apply a theme
$ d2 --theme [200] [diagram.d2] [output.svg]
copy
Format/pretty-print a D2 file
$ d2 fmt [diagram.d2]
copy

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.

SEE ALSO

dot(1), mermaid(1), plantuml(1)

> TERMINAL_GEAR

Curated for the Linux community

Copied to clipboard

> TERMINAL_GEAR

Curated for the Linux community