Skip to content

Instantly share code, notes, and snippets.

@dwymark
Last active December 2, 2023 11:26
Show Gist options
  • Save dwymark/df4e77c4e9fc33608bf22f1288d9195e to your computer and use it in GitHub Desktop.
Save dwymark/df4e77c4e9fc33608bf22f1288d9195e to your computer and use it in GitHub Desktop.
Copy selection to chatgpt-cli (on Linux, in XFCE)
#!/bin/bash
CHAT=/home/$USER/.local/bin/chatgpt-cli
TEMP_FILE="/tmp/selection_$(date +%Y%m%d%H%M%S).txt"
xclip -o -selection primary > "$TEMP_FILE"
xfce4-terminal -e "bash -c '$CHAT -c $TEMP_FILE ; sleep 5'"

This is a Bash script designed to be bound to a keyboard shortcut. It takes the currently highlighted selection from the xwindows clipboard, sends it to chatgpt-cli as context, and display the response in a new XFCE4 terminal window. This way you can conveniently start at a chat about any text that you are working on, across all applications.

Requirements

To use this script, you will need to have the following installed on your system:

  • xclip: A command line utility to interact with the clipboard in X11.
  • xfce4-terminal: The XFCE terminal emulator.
  • chatgpt-cli: A command line interface tool for ChatGPT. Refer to the repo for instructions on how to configure it. You can install chatgpt-cli with pipx as below:
pipx install git+https://github.com/marcolardera/chatgpt-cli

Installation

  1. Install xclip and xfce4-terminal if they are not already installed on your system. This can typically be done via your distribution's package manager. For example:
sudo apt-get install xclip xfce4-terminal
  1. Install the chatgpt-cli tool as per its documentation and ensure it is in the expected directory.

  2. Place the script file in a directory on the path, making sure that it has execution permissions. i.e.,

mv copy-to-chat /usr/local/bin/
chmod +x /usr/local/bin/copy-to-chat
  1. If you are on XFCE, you can bind this script to a keyboard shortcut by going into Settings > Keyboard > Application Shortcuts.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment