Skip to content

CLI Usage Guide

import { Tabs, TabItem } from ‘@astrojs/starlight/components’;

agent-md is designed from the ground up for both human developers and autonomous AI agents. By default, every command outputs compact JSON. When working in an interactive terminal, add the global --human flag for styled, human-friendly output.


Global options must precede the subcommand:

Terminal window
agent-md [GLOBAL_OPTIONS] <SUBCOMMAND> [ARGS...]
Option Description
--human Pretty-prints terminal output instead of raw compact JSON
--cwd <PATH> Sets working directory for configuration discovery
--config <PATH> Explicit path to configuration file (bypasses automatic discovery)
--ignore-markdownlintrc Skips markdownlintrc.* fallback files during configuration search
-v, --version Prints version information
-h, --help Displays help message

Example:

Terminal window
agent-md --human lint README.md
agent-md --cwd /path/to/project fmt document.md

Reads a Markdown document and returns metadata and content:

Terminal window
agent-md read README.md

JSON Output:

{
"path": "README.md",
"content": "# Project Title\n\nProject content...",
"word_count": 420,
"line_count": 68,
"headings": [
{ "level": 1, "text": "Project Title", "line": 1 },
{ "level": 2, "text": "Installation", "line": 12 }
]
}

Avoid transmitting the entire document across tool calls with --field (or -f):

Terminal window
# Extract only the heading outline
agent-md read README.md --field headings
# Extract only the raw text content
agent-md read README.md --field content
# Extract word or line count
agent-md read README.md -f word_count

Available fields: path, content, word_count, line_count, headings.

Fetch only the content underneath a target heading:

Terminal window
agent-md read README.md --content "## Installation"

For nested sections, use the > hierarchy delimiter:

Terminal window
agent-md read README.md --content "## Installation > Build From Source"

The output returns only lines within that section boundary, ending when a heading of equal or higher level is reached.


All write operations run pre-validation to ensure output complies with AI-friendly Markdown standards.

Overwrites or creates a file with validated Markdown content:

Terminal window
agent-md write docs/new-page.md "# New Title\n\nValidated content here."

Returns:

{
"success": true,
"message": "File written successfully",
"document": { "path": "docs/new-page.md", "line_count": 3, "word_count": 6 }
}

If validation fails, the file is not written and an error report is returned.

Replaces the body of an existing section or appends a new section if it does not yet exist:

Terminal window
agent-md write-section README.md \
--section "## Installation" \
--content "Run `brew install agent-md` to install."

Nested sections:

Terminal window
agent-md write-section README.md \
--section "## Features > Performance" \
--content "Powered by SIMD acceleration."

Appends content to the end of a document:

Terminal window
agent-md append notes.md "- New item added at the end"

Inserts content at a specific 1-based line number:

Terminal window
agent-md insert README.md 5 "> [!NOTE]\n> Important announcement."

Deletes one or more lines starting at a 1-based line number:

Terminal window
# Delete line 15
agent-md delete README.md 15
# Delete 4 lines starting at line 15
agent-md delete README.md 15 4

Recursively lists Markdown files in the target directory (defaulting to .):

Terminal window
agent-md list
agent-md list src/

Automatically excludes paths matching .markdownlintignore and .gitignore.

Performs fast string searching inside a document:

Terminal window
agent-md search README.md "TODO"

Output:

{
"query": "TODO",
"matches": [
{ "line": 42, "content": "- TODO: add documentation" }
],
"total": 1
}

Returns the complete table of contents outline with heading levels and line numbers:

Terminal window
agent-md headings docs/guide.md

Calculates word count, line count, and total heading count:

Terminal window
agent-md stats README.md

Converts a Markdown document into line-delimited JSON (JSONL) representations of its parsed blocks:

Terminal window
agent-md to-jsonl README.md

Formats a single file or an entire directory recursively in-place:

Terminal window
# Format single file
agent-md fmt README.md
# Format all markdown files in current project
agent-md fmt .
  • Padded Table Compaction: Trims leading and trailing spaces inside cells. Normalizes separator rows from |:---|:---| to |---|---|. Formats empty cells with a single space (| |).
  • Code Block Comments: Collapses excessive whitespace before # comments in shell code blocks (bash, sh, zsh) while preserving indentations.
  • Folder Tree Simplification: Replaces ├── with ├─ and └── with └─. Strips empty vertical spacer lines (│).
  • Trailing Colon Removal: Normalizes ## Section: to ## Section.
  • Default Code Tags: Adds text tag to unlabeled code blocks.
  • HTML Minification: Minifies HTML blocks to a single line, stripping redundant whitespace while preserving <pre>, <code>, and script contents.
  • List Indent Normalization: Standardizes 4-space list indentation to 2 spaces.

Override configuration values directly from the CLI:

Terminal window
# Keep bold formatting
agent-md fmt --remove-bold=false README.md
# Keep HTML blocks unminified
agent-md fmt --minify-html=false README.md
# Keep italic formatting
agent-md fmt --remove-emphasis=false README.md

Validates a Markdown file and returns error and warning lists:

Terminal window
agent-md lint README.md

Validates a string directly without creating a temporary file:

Terminal window
agent-md lint --content "# Title\n\n**Bold text is invalid**"

Returns human-formatted output with line numbers, error diagnostics, and a status summary.


Initializes a .agent-md.json configuration file:

Terminal window
agent-md init
agent-md init custom-config.json
agent-md init --force

Inspects configuration resolution for the current directory or a specific file:

Terminal window
agent-md config
agent-md config path/to/document.md
agent-md config --check

Outputs active ignore patterns parsed from .markdownlintignore and .gitignore:

Terminal window
agent-md ignore
agent-md --human ignore