Skip to content

Instantly share code, notes, and snippets.

@LxYuan0420
Last active June 7, 2025 15:14
Show Gist options
  • Save LxYuan0420/7d67d3efbc9fae33e7e8b592bdff4db1 to your computer and use it in GitHub Desktop.
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.
# 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
}
@LxYuan0420
Copy link
Author

  1. Add the function to your shell config file (~/.zshrc)
  2. Reload it ( source ~/.zshrc)
  3. Copy a single file: cc example.py
  4. Copy all files recursively from a directory: cc src/
  5. Paste it ( cmd+v)
  6. The alias handles .py, .sh, and .mdc files recursively, excluding init.py files (add more extension as you like)
### File: /Users/username/my_scripts/setup.py
# setup.py content

def setup():
    print("Setting up...")

### File: /Users/username/my_scripts/util/run.sh
#!/bin/bash
echo "Running script"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment