Copy code to clipboard with properly formatted relative paths—perfect for sending to AI assistants. This tool preserves your project's directory structure while respecting .gitignore rules and allowing custom messages.
- AI-Ready Format: Files are prefixed with
./relative/pathheaders, followed by their content. - Git Aware: Respects
.gitignorerules by default (use--no-ignoreto override). - Robust and Safe: Uses Python's
pathlibandargparsefor reliable cross-platform path and argument handling. - Efficient: Gets all ignored files from Git in a single, fast operation.
- Message Support: Add context with the
-mflag (pass text directly or open$EDITOR). - Verbose Debugging: See exactly what's happening with the
-vflag. - Cross-Platform: Works on macOS and Linux out of the box.
- Dependency-Free: Runs with a standard Python 3 installation—no
pipinstalls required. - Auto-Detects Clipboard: Automatically uses
pbcopy(macOS) orxclip/xsel/wl-copy(Linux).
# Copy all Python files to the clipboard
ai-copy -name '*.py'
# Add a custom message, which appears at the end of the clipboard content
ai-copy -name '*.py' -m "Please review these core modules"
# Open your default text editor to write a longer, multi-line message
ai-copy -m
# See exactly which files will be copied and why
ai-copy -v -- src -name '*.js'- Save the Python script to a file named
ai-copyinside a directory that's in yourPATH, like~/.local/bin/. - Make it executable:
chmod +x ~/.local/bin/ai-copy
- Python 3.6+
- Git (for
.gitignoresupport) - A clipboard utility:
- macOS:
pbcopy(pre-installed) - Linux:
xcliporxsel(install via your package manager, e.g.,sudo apt-get install xclip) for X11 - Linux:
wl-copyfor Wayland - install
- macOS:
The script uses a standard find-like syntax.
ai-copy [OPTIONS] -- [PATH] [FIND_EXPRESSIONS...]
Note: It's best practice to use
--to separate the script's options (like-vor-m) from the path andfindexpressions.
| Flag | Description |
|---|---|
--no-ignore |
Disable .gitignore filtering (include all files). |
-m [MESSAGE] |
Add a custom message at the end. If no message is given, opens $EDITOR. |
-v, --verbose |
Enable verbose debug logging to see step-by-step execution. |
-h, --help |
Show the help message and exit. |
# Copy all Python files from the current directory
ai-copy . -name '*.py'
# Copy all JavaScript and TypeScript files from the 'src' directory
ai-copy src -- -type f \( -name '*.js' -o -name '*.ts' \)# Add a short, direct message
ai-copy -m "Implement the new authentication logic" . -- -name '*.py'
# Combine options (place them before the --)
ai-copy -v -m "Fix the caching issue" src/lib -- -name 'cache.js'# Copy Python files between 1KB and 1MB
ai-copy . -- \( -size +1k -a -size -1M \) -name '*.py'The -v flag shows you exactly how the script makes its decisions.
$ ai-copy -v . -- -name '*.py'
[DEBUG] Search base: /Users/cofob/Development/project
[DEBUG] Find filters: -name *.py
[DEBUG] Running command: find /Users/cofob/Development/project -type d -name .git -prune -o -name *.py -type f -print0
[DEBUG] Found 12 candidate files.
[DEBUG] Git repository found (root: /Users/cofob/Development/project).
[DEBUG] Identifying ignored files from 12 candidates.
[DEBUG] Kept 2 files after .gitignore filtering.
[DEBUG] Formatting output for clipboard.
[DEBUG] Formatted: './main.py'
[DEBUG] Formatted: './src/utils.py'
[DEBUG] Copied 14582 bytes to clipboard.
Copied 2 files to clipboard| Method | Relative Paths | Git Ignore | Special Chars | Message Support |
|---|---|---|---|---|
cat *.py |
❌ | ❌ | ❌ | ❌ |
find + xargs cat |
❌ | ❌ | ❌ | ❌ |
ai-copy |
✅ | ✅ | ✅ | ✅ |
When sending code to AI assistants, context is everything. This tool ensures the AI sees the code exactly as it's structured in your project.
This script was rewritten from Bash to Python for improved robustness, portability, and maintainability.
It uses Python's standard argparse library to handle command-line flags and arguments, which prevents the ambiguities and errors common in shell script parsing.
The script uses the most reliable method to respect your .gitignore files.
- It gets a complete list of all candidate files from
find. - It makes a single call to
git check-ignoreto get a list of all files that are ignored. - It then performs a set difference in Python to get the final list of files to copy.
This approach is both highly efficient and more stable than other methods.
Written in modern Python 3, the script uses standard libraries like pathlib for OS-agnostic path handling and logging for clear debugging. It requires no external packages from pip.
- Check your
.gitignorefile: The files are likely being excluded. You can confirm this by running with the--no-ignoreflag:ai-copy --no-ignore . -- -name '*.py' - Verify with verbose mode:
ai-copy -v . -- -name '*.py'will show you which files were found and which were filtered out.
- Linux Users: Make sure you have
xcliporxselinstalled. You can install it with your system's package manager:# For Debian/Ubuntu sudo apt-get install xclip # For Fedora sudo dnf install xclip
MIT License
Made with ❤️ for developers sending code to AI
Created by cofob - inspired by real-world frustration with messy AI prompts