This document describes how to set up a script on Linux to capture selected text and send it to ChatGPT using a simple keyboard shortcut.
You can watch a demo of the script in action
The chatgpt-integration.sh script performs the following tasks:
- Captures selected text: The script uses
xclipto capture the currently selected text in the clipboard. - Verifies the capture: If no text is captured, the script displays an error message using
zenity. - URL encodes the text: The captured text is URL encoded using
jq, ensuring special characters are correctly included in the query. - Opens ChatGPT in the browser: The script then opens the default browser with the ChatGPT URL, passing the selected text as a query parameter.
Before using the script, ensure that the following packages are installed on your system:
xclip: To capture the selected text.zenity: To display the confirmation or modification dialog box.jq: To URL encode the captured text.google-chrome: To open ChatGPT in a new incognito tab in Chrome.
sudo apt-get install xclip zenity jq google-chrome-stableCreate a script file in a directory of your choice, for example, /usr/local/bin/chatgpt-integration.sh.
sudo nano /usr/local/bin/chatgpt-integration.shPaste the following content into the file:
#!/bin/bash
# Capture the selected text using xclip
selected_text=$(xclip -o)
# Open a dialog box to confirm or modify the text
query=$(zenity --entry --title="Send to ChatGPT Mini" --text="Paste your text:" --entry-text="$selected_text")
# If the input is not empty
if [ ! -z "$query" ]; then
# Encode the query to be used in a URL
encoded_query=$(echo "$query" | jq -sRr @uri)
# Define the screen resolution (replace if necessary)
screen_width=1920
screen_height=1080
# Calculate the position on the right side of the screen
window_width=400
window_height=800
window_x=$((screen_width - window_width))
window_y=$((screen_height / 2 - window_height / 2)) # Centered vertically
# Open the browser in a new incognito tab with custom size and position
google-chrome --incognito --window-size=$window_width,$window_height --window-position=$window_x,$window_y --app="https://chatgpt.com/?model=gpt-4o-mini&q=$encoded_query"
fiGive the script execution permission:
sudo chmod +x /usr/local/bin/chatgpt-integration.shTo configure a keyboard shortcut that runs the script:
- Open System Settings.
- Navigate to the Keyboard Shortcuts section.
- Add a new shortcut:
- Name: Send to ChatGPT
- Command:
/usr/local/bin/chatgpt-integration.sh - Keyboard Shortcut: Choose a key combination that is not in use, such as
Ctrl+Alt+C.
- Save the new shortcut.
- Select the text in any application and copy.
- Press the configured keyboard shortcut and paste.
- The text will automatically be sent to ChatGPT in your browser.
- Text not captured: If the script does not capture the text, ensure that
xclipis installed and functioning correctly. - Command error: Verify that all the necessary commands (
xclip,jq,zenity, etc.) are installed and working.
This integration allows any Linux user to select text in any application and, with a simple keyboard shortcut, send that text directly to ChatGPT, streamlining workflow and productivity.