Posted on
Artificial Intelligence

Artificial Intelligence Clipboard Managers

Author
  • User
    linuxbash
    Posts by this author
    Posts by this author

Artificial Intelligence Clipboard Managers on Linux (with Bash)

Ever copy a wall of logs, an awkward email, or a messy stackoverflow snippet and wish you could paste the “better” version instead—summarized, translated, or cleaned up? That’s the promise of AI-powered clipboard managers: take whatever you copy, run it through an assistant, and paste something instantly more useful.

On Linux, you don’t need a monolithic app to get there. With a few command-line tools and a tiny Bash script, you can build your own AI clipboard workflow that works on Wayland or X11, stays minimal, and fits neatly into your terminal-driven life.

This post explains why it’s useful, shows you how to set it up, and gives real-world examples to make AI part of your copy/paste muscle memory.

Why an AI Clipboard Manager is worth it

  • Reduce context switching: Summarize, translate, or reformat text right as you copy it.

  • Keep it light and scriptable: No heavy GUIs—just Bash, your clipboard tool, and your AI backend.

  • Works with your stack: Wayland or X11, any shell, any editor, any window manager.

  • Privacy and control: Choose a local model (Ollama) or a cloud model (OpenAI). You control what leaves your machine.

What we’ll build

A single Bash tool ai-clip that:

  • Reads from stdin or your clipboard.

  • Sends it to an AI backend with a task (summarize, translate, fix grammar, etc.).

  • Copies the improved result back to your clipboard for an instant paste.

You can trigger it from the terminal or bind it to a hotkey.


1) Install prerequisites

We’ll install:

  • A clipboard tool (Wayland: wl-clipboard, X11: xclip)

  • curl (HTTP requests)

  • jq (JSON handling)

  • Optional: xbindkeys (hotkey binding) and/or Ollama (local models)

Run the commands for your distro:

  • Debian/Ubuntu (apt):

    • sudo apt update && sudo apt install -y wl-clipboard xclip curl jq xbindkeys
  • Fedora (dnf):

    • sudo dnf install -y wl-clipboard xclip curl jq xbindkeys
  • openSUSE (zypper):

    • sudo zypper install -y wl-clipboard xclip curl jq xbindkeys

Optional local AI backend (Ollama):

  • curl -fsSL https://ollama.com/install.sh | sh

  • Then start it: ollama serve (or log out/in; on some systems it auto-creates a service)

  • Pull a model (pick one you like, e.g., llama3.1):

    • ollama pull llama3.1

Cloud AI option (OpenAI):

  • Set your API key in your shell profile:
    • export OPENAI_API_KEY="sk-..."
      Add that line to ~/.bashrc or ~/.profile to persist.

2) Create the AI clipboard script

Save this as ~/.local/bin/ai-clip and make it executable with chmod +x ~/.local/bin/ai-clip. Ensure ~/.local/bin is in your PATH.

#!/usr/bin/env bash
set -euo pipefail

# Configurable defaults
: "${AI_BACKEND:=openai}"         # "openai" or "ollama"
: "${OPENAI_MODEL:=gpt-4o-mini}"  # adjust to your available model
: "${OLLAMA_MODEL:=llama3.1}"     # ensure you've pulled this: ollama pull llama3.1

die() { echo "Error: $*" >&2; exit 1; }

# Detect clipboard commands (Wayland or X11)
if command -v wl-copy >/dev/null 2>&1 && [ -n "${WAYLAND_DISPLAY:-}" ]; then
  copy()  { wl-copy; }
  paste() { wl-paste --no-newline; }
elif command -v xclip >/dev/null 2>&1; then
  copy()  { xclip -selection clipboard -in; }
  paste() { xclip -selection clipboard -out; }
else
  die "No clipboard tool found. Install wl-clipboard (Wayland) or xclip (X11)."
fi

require() { command -v "$1" >/dev/null 2>&1 || die "Missing dependency: $1"; }
require jq
require curl

usage() {
  cat <<EOF
Usage: $(basename "$0") <action> [args]

Actions:
  summarize                   Summarize the text
  explain                     Explain the text simply
  fix                         Fix grammar and clarity
  translate <lang>            Translate to a target language (e.g., 'es', 'fr', 'de')
  to-csv                      Extract tabular data to CSV if possible
  help                        Show this help

Examples:
  # From clipboard
  ai-clip summarize

  # From stdin
  cat biglog.txt | ai-clip summarize

  # Translate clipboard to Spanish
  ai-clip translate es
EOF
}

task_prompt() {
  local action="${1:-}"
  shift || true
  case "$action" in
    summarize) echo "Summarize the following text into concise bullet points. Preserve key facts and numbers.";;

    explain) echo "Explain the following text in simple terms. Use short paragraphs and plain language.";;

    fix) echo "Improve grammar, clarity, and style while preserving meaning. Return plain text only.";;

    translate)
      local lang="${1:-}"
      [ -n "$lang" ] || die "translate requires a target language code, e.g., 'es' or 'de'"
      echo "Translate the following text to ${lang}. Keep code and command snippets intact."
      ;;

    to-csv) echo "If the following text contains structured data, convert it to CSV with a header. If not, produce a best-effort CSV of key-value pairs.";;

    help|"") usage; exit 0;;

    *) die "Unknown action: $action (run: ai-clip help)";;
  esac
}

call_openai() {
  [ -n "${OPENAI_API_KEY:-}" ] || die "OPENAI_API_KEY not set"
  local task="$1" text="$2"
  local payload
  payload="$(jq -n \
    --arg model "$OPENAI_MODEL" \
    --arg task "$task" \
    --arg text "$text" \
    '{
       model: $model,
       temperature: 0.2,
       messages: [
         {role:"system", content:"You are a helpful assistant. Keep answers concise and return plain text only."},
         {role:"user", content: ("Task: " + $task + "\n\nText:\n" + $text)}
       ]
     }')"

  curl -sS https://api.openai.com/v1/chat/completions \
    -H "Authorization: Bearer ${OPENAI_API_KEY}" \
    -H "Content-Type: application/json" \
    -d "$payload" \
  | jq -r '.choices[0].message.content // empty'
}

call_ollama() {
  local task="$1" text="$2"
  local payload
  payload="$(jq -n \
    --arg model "$OLLAMA_MODEL" \
    --arg prompt "Task: $task

Text:
$text" \
    '{model:$model, stream:false, messages:[{role:"user", content:$prompt}] }')"

  curl -sS http://localhost:11434/api/chat \
    -H "Content-Type: application/json" \
    -d "$payload" \
  | jq -r '.message.content // .messages[-1].content // empty'
}

main() {
  local action="${1:-}"; shift || true
  local task; task="$(task_prompt "$action" "$@")"

  # Read input: stdin if data is piped, otherwise clipboard
  local src
  if [ -t 0 ]; then
    src="$(paste)"
  else
    src="$(cat)"
  fi

  [ -n "$src" ] || die "No input provided (copy something or pipe into ai-clip)."

  local output
  case "$AI_BACKEND" in
    openai)  output="$(call_openai "$task" "$src")" ;;
    ollama)  output="$(call_ollama "$task" "$src")" ;;
    *) die "Unknown AI_BACKEND: $AI_BACKEND (use 'openai' or 'ollama')" ;;
  esac

  [ -n "$output" ] || die "AI backend returned no output"
  printf "%s" "$output" | copy
  echo "✓ Result copied to clipboard."
}

main "$@"

Notes:

  • Set AI_BACKEND=openai or AI_BACKEND=ollama in your shell profile.

  • Change OPENAI_MODEL or OLLAMA_MODEL to match what you have available.


3) Use it: practical, real-world examples

  • Summarize noisy logs

    • journalctl -xb | tail -n 500 | ai-clip summarize
    • Paste the summary into an issue or a chat.
  • Clean up an email or doc fragment

    • Copy messy text, then run: ai-clip fix
    • Paste the improved version directly.
  • Translate a snippet while keeping code intact

    • Copy text, run: ai-clip translate es
    • Paste the Spanish translation.
  • Turn CLI output into CSV

    • kubectl get pods -A -o wide | ai-clip to-csv
    • Paste into a spreadsheet or save it:
      kubectl get pods -A -o wide | ai-clip to-csv | tee pods.csv
  • Explain a config for a teammate

    • cat /etc/nginx/nginx.conf | ai-clip explain
    • Paste the simple explanation into a PR description.

Tip: If you’re on Wayland, you can auto-run on clipboard changes:

  • wl-paste --watch ai-clip summarize
    This watches the clipboard and writes the summarized text back to your clipboard each time it changes. Test on small inputs first.

4) Bind to a hotkey (optional)

Example with xbindkeys (works on X11; Wayland users should use their compositor’s keybinds):

1) Create ~/.xbindkeysrc with entries like:

# Ctrl+Alt+S -> Summarize clipboard
"bash -lc 'AI_BACKEND=ollama ai-clip summarize'"
  Control+Alt + s

# Ctrl+Alt+T -> Translate clipboard to French
"bash -lc 'AI_BACKEND=openai ai-clip translate fr'"
  Control+Alt + t

2) Start xbindkeys:

  • xbindkeys
    Add it to your session autostart to persist.

5) Security, cost, and performance tips

  • Secrets: Keep OPENAI_API_KEY in your shell profile, or use a secret manager like pass. Avoid committing keys in scripts.

  • Sensitive data: Prefer local models (Ollama) for private content. For cloud, only send what’s necessary.

  • Costs: Cloud models bill per token; use concise prompts and summary actions. Local models cost CPU/RAM instead.

  • Performance: Smaller local models respond faster. For OpenAI, lower temperatures and concise prompts reduce latency.


Troubleshooting

  • “No clipboard tool found”:

    • Install one:
    • apt: sudo apt install -y wl-clipboard xclip
    • dnf: sudo dnf install -y wl-clipboard xclip
    • zypper: sudo zypper install -y wl-clipboard xclip
  • “AI backend returned no output”:

    • Check your AI_BACKEND value.
    • For OpenAI: ensure OPENAI_API_KEY is exported and the model name exists for your account.
    • For Ollama: ensure ollama serve is running and the model is pulled.
  • JSON errors:

    • Ensure jq is installed:
    • apt: sudo apt install -y jq
    • dnf: sudo dnf install -y jq
    • zypper: sudo zypper install -y jq

Conclusion / Call to Action

You don’t need a heavyweight app to get AI into your copy/paste flow. With a handful of packages and a Bash script, you can:

  • Summarize logs before they hit your issue tracker

  • Translate snippets in place

  • Clean and format text right from the clipboard

Set it up, bind a key or two, and let your clipboard do more of the work.

Next steps:

  • Install the prerequisites (apt/dnf/zypper commands above).

  • Drop the ai-clip script in ~/.local/bin.

  • Pick your backend (AI_BACKEND=openai or AI_BACKEND=ollama) and test with ai-clip summarize.

  • Share your favorite actions or improvements—turn your clipboard into your smartest tool.