pctx
Generate LLM-ready context from your codebase. Intelligently packages source files with proper formatting, truncation, and filtering for optimal AI assistant consumption.
Features
- Smart file discovery: Respects
.gitignore, excludes binary files, and filters common non-source directories - Multiple output formats: Markdown (default), XML, and plain text
- Intelligent truncation: Preserves file head and tail when truncating large files
- Flexible filtering: Include/exclude patterns with gitignore-style syntax
- Multiple destinations: stdout, clipboard, or file output
- JSON mode: Structured output for programmatic use and CI/CD integration
- Stdin support: Read file lists from pipes for integration with other tools
- Token estimation: Approximate token counts for various LLM models
Installation
Or build from source:
Quick Start
# Generate context for current directory
# Copy to clipboard
# Write to file
# JSON output for scripts
# Filter specific files
# Pipe file list from other tools
|
| |
# Preview without generating
# Include file tree in output
# Disable truncation for full file contents
Usage
Basic Commands
# Default: generate context from current directory
# List files that would be included
# Show file tree structure
# Configuration management
# Generate shell completions
Output Options
| Flag | Description |
|---|---|
--clipboard, -c |
Copy output to system clipboard |
--output FILE, -o |
Write to file (use --force to overwrite) |
--format, -f |
Output format: markdown, xml, plain |
--tree, -t |
Include file tree at beginning of output |
--stats, -s |
Show statistics summary |
--json |
Structured JSON output (for scripts) |
--stdin |
Read file paths from stdin (one per line) |
Filtering Options
| Flag | Description |
|---|---|
--exclude PATTERN, -e |
Exclude files matching pattern (repeatable) |
--include PATTERN, -i |
Include only files matching pattern (repeatable) |
--hidden |
Include hidden files (starting with .) |
--no-default-excludes |
Disable built-in exclusions |
--no-gitignore |
Ignore .gitignore rules |
--max-size KB |
Maximum file size in KB (default: 1024) |
--max-depth N, -d |
Limit directory recursion depth |
Truncation Options
| Flag | Description |
|---|---|
--no-truncation |
Disable all truncation |
--max-lines N |
Max lines per file before truncating (default: 500, 0 = unlimited) |
--head-lines N |
Lines to keep at file start (default: 20) |
--tail-lines N |
Lines to keep at file end (default: 10) |
--max-line-length N |
Max chars per line (default: 500, 0 = unlimited) |
--head-chars N |
Chars to keep at line start (default: 200) |
--tail-chars N |
Chars to keep at line end (default: 100) |
Stdin Mode
The --stdin flag allows reading file paths from standard input, enabling powerful integrations:
# Process only recently modified Rust files
|
# Process files from a list
|
# Chain with pctx's own file listing
| |
# Use with git to process only changed files
|
# Process files matching complex criteria
|
When using --stdin:
- Empty lines and whitespace-only lines are ignored
- Non-existent files are skipped with a warning (in verbose mode)
- Directories in the input are expanded recursively
Configuration
Config File
Create a .pctx.toml file in your project root:
Example configuration:
# Patterns to exclude (in addition to defaults)
= [
"*.generated.ts",
"vendor/",
"__snapshots__",
]
# Patterns to include (if specified, only these are included)
= [
"*.rs",
"*.toml",
]
# Truncation settings
= 500
= 20
= 10
= 500
Configuration is loaded from .pctx.toml in the current directory or any parent directory. If the config file exists but has syntax errors, a warning is printed and the file is skipped.
Configuration Precedence
Settings are applied in this order (highest priority first):
- Command-line arguments
- Config file (
.pctx.toml) - Built-in defaults
Default Exclusions
Common directories and files are excluded by default:
- Version control:
.git,.svn,.hg - Dependencies:
node_modules,vendor,target,.venv - Build outputs:
dist,build,out,bin,obj - IDE/Editor:
.idea,.vscode,.vs - Caches:
__pycache__,.cache,.pytest_cache - Lock files:
package-lock.json,yarn.lock,Cargo.lock, etc.
See all defaults with: pctx config defaults
Pattern Syntax
Patterns follow gitignore-style syntax:
| Pattern | Matches |
|---|---|
*.log |
All .log files |
test_* |
Files starting with test_ |
**/tests/** |
Any tests directory at any level |
/src/generated |
src/generated at root only |
docs/ |
docs directory |
Limitations:
- Negation patterns (
!pattern) are not supported and will show a warning - Character classes (
[abc]) depend on glob crate support - Some edge cases with
**/patterns may differ from git behavior
JSON Output
Use --json for structured output suitable for scripts and CI/CD:
# Generate context with JSON output
# List files as JSON
# Parse with jq
|
Response Format
Success:
Error:
Partial Success:
Exit Codes
| Code | Meaning |
|---|---|
| 0 | Success |
| 1 | General failure |
| 2 | Usage error (bad arguments) |
| 3 | File/directory not found |
| 4 | Permission denied |
| 5 | Conflict (output file exists) |
| 6 | No files matched filters |
| 7 | Partial success (some files failed) |
Examples
Basic Usage
# Current directory to stdout
# Specific paths
# Copy to clipboard for pasting into ChatGPT
# Save to file
Filtering
# Only Rust files
# Exclude tests
# Include hidden files
# Disable default excludes (include node_modules, etc.)
Output Formats
# Markdown (default) - good for most LLMs
# XML - useful for Claude
# Plain text - minimal formatting
Working with Large Codebases
# Limit recursion depth
# Disable truncation for full content
# Custom truncation settings
# Smaller file size limit
# Preview what would be included
CI/CD Integration
# Check if context generation would succeed
&& ||
# Generate context and check size
|
# List large files
|
# Process only changed files in a PR
|
Integration with Other Tools
# Process files found by fd/find
|
# Process files from ripgrep matches
|
# Process git-tracked files only
|
# Combine with file filtering
| |
Shell Completions
Generate completions for your shell:
# Bash
# Zsh
# Fish
# PowerShell
Troubleshooting
"No files matched the specified filters"
- Check that your paths exist:
ls <path> - Try
--no-default-excludesto include commonly excluded directories - Use
pctx files listto see what files would be included - Check your
.pctx.tomlfor overly restrictive patterns
Clipboard not working on Linux
Clipboard access requires a display server. Make sure:
- You're running in a graphical environment (X11 or Wayland)
xcliporwl-clipboardis installed
Config file not being applied
If your .pctx.toml has syntax errors, pctx will warn and continue without it:
Warning: failed to load config file: TOML parse error...
Check your config syntax with:
Files being truncated unexpectedly
Use --no-truncation to disable truncation, or adjust thresholds:
Binary files being included
Binary files should be automatically detected and skipped. If a binary file is being included:
- Check if it has a text-like extension
- Use
--excludeto explicitly exclude it
License
MIT License - see LICENSE for details.
Contributing
Contributions welcome! Please read CONTRIBUTING.md first.
# Run tests
# Run with verbose output
# Run integration tests