LinuxCommandLibrary

d2

Convert textual descriptions into visual diagrams

TLDR

Compile and render a D2 source file into an output file

$ d2 [path/to/input_file.d2] [path/to/output_file.ext]
copy

Watch live changes made to a D2 source file in the default web browser
$ d2 [[-w|--watch]] [path/to/input_file.d2] [path/to/output_file.ext]
copy

Format a D2 source file
$ d2 fmt [path/to/input_file.d2]
copy

List available themes
$ d2 themes
copy

Use a different theme for the output file (list available themes first to get the desired theme_id)
$ d2 [[-t|--theme]] [theme_id] [path/to/input_file.d2] [path/to/output_file.ext]
copy

Make rendered diagrams look like hand sketches
$ d2 [[-s|--sketch]] true [path/to/input_file.d2] [path/to/output_file.ext]
copy

SYNOPSIS

d2 [input.d2] [output] [flags] | d2 <subcommand> [flags]

PARAMETERS

-h, --help
    Show context-sensitive help

--version, -v
    Print version and exit

--verbose, -V
    Enable verbose logging

--layout=ENGINE
    Layout engine: dagre, elk, patchwork (default: dagre)

--type=TYPE
    Output type: svg, png, pdf, eps, dot (default: infer from output)

-o, --output=PATH
    Output file path (default: input basename)

--watch
    Watch input for changes and re-render

--theme=THEME
    Builtin or path to .d2t theme file

--font=FAMILY
    Font family for text

--scale=FACTOR
    Scale output by factor (default: 1)

--animate[=INTERVAL]
    Export animated SVG (interval in ms)

DESCRIPTION

d2 is a powerful CLI tool and declarative language for generating diagrams from simple text files.

It transforms human-readable .d2 scripts into professional visuals like flowcharts, architectures, sequences, and more, supporting SVG, PNG, PDF outputs.

Key strengths include version control for diagrams, automation in CI/CD, multiple layout engines (dagre, elk, patchwork), themes, fonts, scaling, and animations. No GUI needed—edit in any text editor, render instantly.

Ideal for docs, design, and engineering teams. Integrates with Markdown, wikis, and tools like Obsidian or MkDocs. Fast rendering, even for large diagrams, with live preview modes.

CAVEATS

Not a core Linux utility; install via brew (brew install d2), apt, or GitHub releases. Large diagrams may need more RAM.

SUBCOMMANDS

d2 run: Render once.
d2 watch: Auto-reload preview.
d2 init: Create new diagram.
d2 fmt: Format .d2 file.

BASIC EXAMPLE

diagram.d2:
a -> b

d2 diagram.d2 diagram.svg

HISTORY

Created by Terrastruct in 2020 as open-source alternative to Graphviz. v0.1 alpha in Jul 2020; v1.0 stable Apr 2023 with ELK layout, themes, better DSL.

SEE ALSO

dot(1), graphviz(1), plantuml(1)

Copied to clipboard