Last active
June 7, 2025 15:14
-
-
Save LxYuan0420/7d67d3efbc9fae33e7e8b592bdff4db1 to your computer and use it in GitHub Desktop.
A minimalist but powerful Bash/Zsh alias that quickly copies the content of files or directories (Python, MDC, Shell scripts) directly to your clipboard with file headers for easy pasting into editors or ChatGPT prompts. Perfect when you need a fast and straightforward solution without installing any additional packages or dependencies.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# cc: pbcopy on steroids for file or directory | |
function cc() { | |
local target="$1" | |
if [[ -z "$target" ]]; then | |
echo "Usage: cc <file-or-directory>" | |
return 1 | |
fi | |
# Resolve to absolute path | |
local abs_path | |
abs_path=$(realpath "$target" 2>/dev/null) || { | |
echo "Invalid path: $target" | |
return 1 | |
} | |
# Check if it's a regular file | |
if [[ -f "$abs_path" ]]; then | |
{ | |
echo "### File: $abs_path" | |
cat "$abs_path" | |
} | pbcopy | |
echo "Copied file '$abs_path' to clipboard." | |
return 0 | |
fi | |
# Check if it's a directory | |
if [[ -d "$abs_path" ]]; then | |
local output="" | |
while IFS= read -r -d '' file; do | |
output+="### File: $(realpath "$file")"$'\n' | |
output+="$(cat "$file")"$'\n\n' | |
done < <(find "$abs_path" -type f \( -name "*.py" -o -name "*.mdc" -o -name "*.sh" \) ! -name "__init__.py" -print0 | sort -z) | |
print -rn -- "$output" | pbcopy | |
echo "Copied content from directory '$abs_path' to clipboard." | |
return 0 | |
fi | |
echo "Error: '$target' is neither a file nor a directory." | |
return 1 | |
} |
Author
LxYuan0420
commented
Jun 1, 2025
- Add the function to your shell config file (~/.zshrc)
- Reload it ( source ~/.zshrc)
- Copy a single file: cc example.py
- Copy all files recursively from a directory: cc src/
- Paste it ( cmd+v)
- The alias handles .py, .sh, and .mdc files recursively, excluding init.py files (add more extension as you like)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment