Skip to content

Instantly share code, notes, and snippets.

@mhulse
Last active September 16, 2024 23:06
Show Gist options
  • Save mhulse/7a08f5dd6b70c703aa0a5e810a2dc73a to your computer and use it in GitHub Desktop.
Save mhulse/7a08f5dd6b70c703aa0a5e810a2dc73a to your computer and use it in GitHub Desktop.
Clipboard code prepend script

Clipboard code prepend script

This is a simple Zsh script designed to help developers who want to use AI services (like ChatGPT) to convert their code into multiple programming languages. The script’s goal is to streamline the process of adding a structured prompt to the clipboard for easier AI interaction.

Features

  • Automatically adds a customizable conversion prompt to existing code in the clipboard
  • Supports multiple programming languages (e.g., Ruby, TypeScript, JavaScript, Java, Python)
  • Uses pbpaste and pbcopy for clipboard management
  • Designed for macOS, with an option for cross-platform extension

Usage

  1. Copy the code you want converted into other languages to your clipboard.
  2. Download the script and save it to a convenient location (e.g., ~/scripts/prepend_prompt).
  3. Make it executable:
    chmod u+x ~/scripts/prepend_prompt
  4. Call the script anytime you need to prepare code for conversion:
    ~/scripts/prepend_prompt

Bonus!

Automatically load the script into your shell environment by adding this to your ~/.zshrc:

# custom executables:
path=("$HOME/scripts")

And then run source ~/.zshrc.

Prompt Example

Once the script runs, the following example prompt is added to the clipboard:

Please show me the original code for comparison, but do not convert it into 
the language it's already written in. Detect the language and skip converting 
it into the same language. Then convert the code into the following languages: 
Ruby, TypeScript, JavaScript, Java, Python.

Please provide a detailed explanation of each part of the original code.
What does each operator and method in this code do? 
Finally, provide a detailed summary of what the original code does.

<your clipboard code here>

Notes

  1. This script is a convenient way to interact with AI for learning and code conversion. However, keep in mind that AI responses should complement your own understanding, especially when it comes to syntax and best practices in different languages.
  2. By default, the script supports a set of languages, but it can easily be modified to include others. Additionally, it uses macOS clipboard utilities (pbpaste/pbcopy), which can be replaced with Linux alternatives like xclip for cross-platform usage.
#!/bin/zsh
# function to prepend the prompt
prepend_prompt() {
# define languages
local languages=("Ruby" "TypeScript" "JavaScript" "Java" "Python")
# concatenate languages array into string
local languages_string
languages_string=$(printf ", %s" "${languages[@]}")
languages_string=${languages_string:2} # remove leading comma and space
# construct the multi-line prompt
local gpt_prompt="
Please show me the original code for comparison, but do not convert it into
the language it's already written in. Detect the language and skip converting
it into the same language. Then convert the code into the following languages:
${languages_string}.
Please provide a detailed explanation of each part of the original code.
What does each operator and method in this code do?
Finally, provide a detailed summary of what the original code does.
\`\`\`
"
# get current clipboard content
local clipboard_content
clipboard_content=$(pbpaste)
# append closing fence to the prompt
local new_content="${gpt_prompt}${clipboard_content}\n\`\`\`"
# update clipboard with new content
echo -n "$new_content" | pbcopy
echo "New clipboard content with prompt prepended!"
}
# run the function
prepend_prompt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment