← 返回命令列表

Linux command

d2 命令

文本

复制后可按需替换文件名、目录或参数。

常用示例

Compile a D2 diagram

d2 [diagram.d2] [output.svg]

Compile to PNG

d2 --format png [diagram.d2] [output.png]

Watch for changes

d2 --watch [diagram.d2] [output.svg]

Use a specific layout

d2 --layout [elk] [diagram.d2] [output.svg]

Apply a theme

d2 --theme [200] [diagram.d2] [output.svg]

Format/pretty-print

d2 fmt [diagram.d2]

说明

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.

参数

--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.

FAQ

What is the d2 command used for?

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.

How do I run a basic d2 example?

Run `d2 [diagram.d2] [output.svg]` in a terminal, then adjust file names, paths, flags, or remote targets for your system.

What does --format _FORMAT_ do in d2?

Output format: svg, png, pdf.