Skip to content

Instantly share code, notes, and snippets.

@jaanli
Last active May 5, 2024 13:04
Show Gist options
  • Save jaanli/5def01b7bd674efd6d9008cf1125986d to your computer and use it in GitHub Desktop.
Save jaanli/5def01b7bd674efd6d9008cf1125986d to your computer and use it in GitHub Desktop.
Large language model (LLM) helper script to copy context into long-context-window language models to improve their ability to help users with programming tasks.
#!/bin/bash
# Instructions:
# 0. Install prerequisites using a package manager: `brew install tree rectangle maccy` (`tree` is a command line utility for transmitting the directory structure to large language models in a compressed format; `rectangle` is a utility for managing screen state and having a LLM on the left of the screen and a code editor/interactive development environment like Jupyter Lab or Visual Studio Code on the right of the screen using keyboard shortcuts; `maccy` is for having a keyboard shortcut to display clipboard history, as there are few solutions like https://colab.research.google.com/github/jaanli/language-model-notebooks/blob/main/notebooks/getting-started.ipynb that enable saving of previous prompts and code in accessible ways using keyboard shortcuts!)
# 1. Copy and paste the contents of this bash script into a file: `code /usr/local/bin/copy.sh`
# 2. Make this new file executable: `chmod a+x /usr/local/bin/copy.sh`
# 3. Test it in a directory, by running `copy.sh`, which should print out `Script completed. Results have been copied to the clipboard.`
# 4. Paste the contents of the clipboard into claude.ai or chat.openai.com (or any other LLM), and then add your prompt at the end (E.g. write this component, ask how one would refactor this as an engineer, how one would build a frontend/app/machine learning system, etc!)
# 5. Profit! You have now saved yourself from a lot of copy and pasting and back and forth.
# Prompt to generate this using Claude or GPT-4 or Gemini:
# write a short bash script for mac that first:
# * uses the tree command
# * then runs cat on every entry recursively in the tree command (every file within every folder in the directory)
# * outputs the results to a file and uses pbcopy to copy the contents of this file to the clipboard
# Set the directory path
directory_path="."
# Generate the tree structure and save it to a temporary file
tree_output=$(mktemp)
echo "Output of tree command:" >> "$tree_output"
echo "\`\`\`" > "$tree_output"
tree "$directory_path" >> "$tree_output"
echo "\`\`\`" >> "$tree_output"
# Concatenate the contents of each file recursively and save it to a temporary file
cat_output=$(mktemp)
find "$directory_path" -type f -print0 | while IFS= read -r -d '' file; do
echo "Relative file path: ${file#$directory_path/}" >> "$cat_output"
echo "\`\`\`" >> "$cat_output"
cat "$file" >> "$cat_output"
echo "\`\`\`" >> "$cat_output"
done
# Combine the tree structure and file contents into a single output copy
cat "$tree_output" "$cat_output" | pbcopy
# Clean up temporary files
rm "$tree_output" "$cat_output"
echo "Script completed. Results have been copied to the clipboard."
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment