Extract fenced code blocks from Markdown files. Zero dependencies. Works on Windows, macOS, and Linux.
Markdown files are full of runnable code but there is no easy way to extract them programmatically. codeblock-cli gives you surgical access to any fenced code block:
- Filter by language - grab only the bash, yaml, or python blocks
- Pick by index - get exactly the 3rd code block
- List what is there - see all languages at a glance
- Pipe-friendly - reads from stdin, writes to stdout
npm install -g @kszongic/codeblock-cli
Or run directly: npx @kszongic/codeblock-cli README.md
codeblock README.md # all blocks
codeblock --lang js README.md # filter by language
codeblock -n 0 README.md # first block only
codeblock --list README.md # list languages
cat doc.md | codeblock --lang py # pipe from stdin
| Flag | Description |
|---|---|
| -l, --lang | Filter blocks by language |
| -n, --index | Print only the nth block (0-based) |
| --list | List languages of all blocks |
| --raw | Output without trailing newline |
| -h, --help | Show help |
| -v, --version | Show version |
- Run install commands from a README:
codeblock --lang bash README.md -n 0 | bash - Test code examples in CI: Extract JS blocks and run them with node
- Extract config snippets:
codeblock --lang yaml SETUP.md -n 0 > config.yml - Audit languages in docs: Loop over markdown files with
--list - Pipe from GitHub:
curl -s <raw-url> | codeblock --lang python - Build a snippet library:
find docs/ -name '*.md' -exec codeblock --lang bash {} \; > all.sh
- Documentation testing - Run code examples in CI to prevent stale docs
- Config extraction - Pull YAML/JSON/TOML from setup guides
- Literate programming - Treat Markdown as runnable source
- Content migration - Pull code from blog posts into files
- Code review - Inspect what code a PR's docs show
Single-pass line scanner that parses fenced code blocks (backtick and tilde). Handles nested fences, indented blocks, and optional language identifiers. Fast and predictable.
| Feature | codeblock-cli | grep/sed | remark-cli |
|---|---|---|---|
| Zero deps | Yes | N/A | No (50+) |
| Cross-platform | Yes | Unix only | Yes |
| Filter by lang | Yes | Fragile | Plugin |
| Pick by index | Yes | No | No |
| List languages | Yes | No | Plugin |
| Install size | ~5 KB | Built-in | ~15 MB |
- detect-lang-cli - Detect programming language
- whitespace-cli - Visualize invisible characters
- tabulate-cli - Render ASCII tables
- json-fmt-cli - Format JSON
- string-hash-cli - Hash strings
MIT (c) 2026 kszongic