LinuxCommandLibrary

code2prompt

Generate AI prompts from source code

TLDR

Generate a prompt for the current project and copy it to the clipboard (default behavior)

$ code2prompt [path/to/project]
copy

Include only specific files and exclude a directory
$ code2prompt [path/to/project] [[-i|--include]] "[**/*.rs]" [[-e|--exclude]] "[tests/**]"
copy

Write the prompt to a file instead of the clipboard
$ code2prompt [path/to/project] [[-O|--output-file]] [my_prompt.txt]
copy

Produce structured JSON output
$ code2prompt [path/to/project] [[-F|--output-format]] json
copy

Use a custom Handlebars template when generating the prompt
$ code2prompt [path/to/project] [[-t|--template]] [my_template.hbs]
copy

SYNOPSIS

code2prompt [-p, --prompt-type <TYPE>] [-o, --output <FILE>] [-i, --include <PATTERNS>...] [-j, --jobs <N>] <PATHS>...

PARAMETERS

-g, --git <GIT>
    Custom git command path [default: git]

-i, --include <INCLUDE>...
    Glob patterns to include files [defaults to common langs like *.rs, *.py]

-j, --jobs <JOBS>
    Parallel jobs for scanning [default: 8]

-l, --language <LANGUAGE>
    Override auto-detected language [default: auto]

-o, --output <OUTPUT>
    Write prompt to file instead of stdout

-p, --prompt-type <PROMPT_TYPE>
    Prompt template: default, tests, docs, security, refactoring [default: default]

-h, --help
    Print help information

-V, --version
    Print version

<PATHS>...
    Paths (dirs/files) to scan for code

DESCRIPTION

code2prompt is a powerful command-line tool designed to transform source code repositories or directories into well-formatted prompts suitable for large language models (LLMs) like GPT-4 or Claude.

It scans specified paths, collects relevant files based on include patterns (defaulting to common languages like Rust, Python, JS, Go, etc.), and structures the code into a single, copy-paste-ready prompt.

Key features include customizable prompt types (e.g., tests, docs, security, refactoring), parallel processing with jobs, git integration for diffs/commits, and language detection.

Ideal for tasks like automated code review, test generation, documentation, or vulnerability scanning without manual prompt engineering.

Output can be to stdout or a file, making it perfect for piping into AI tools.

CAVEATS

May consume high memory on large repos; defaults exclude binaries/large files; git mode requires clean working tree for some features.
Limited to predefined prompt types.

INSTALLATION

Via Cargo: cargo install code2prompt
Or prebuilt binaries from GitHub releases.

EXAMPLE

code2prompt -p tests /path/to/repo > prompt.txt
Generates test-writing prompt for repo.

HISTORY

Developed by 0xshellcode as open-source Rust tool (GitHub: code2prompt/code2prompt).
First released ~2023, gained popularity for LLM dev workflows; active updates add prompt types and perf improvements.

SEE ALSO

git(1), find(1), tree(1), ripgrep(1)

Copied to clipboard