Skip to content

Instantly share code, notes, and snippets.

@samkcarlile
Created February 25, 2026 04:11
Show Gist options
  • Select an option

  • Save samkcarlile/630ae563d79a1772acec50420a88ba05 to your computer and use it in GitHub Desktop.

Select an option

Save samkcarlile/630ae563d79a1772acec50420a88ba05 to your computer and use it in GitHub Desktop.
A zsh function to publish your clipboard contents as a GitHub Gist
# pbgist - Create a GitHub Gist from clipboard contents with an interactive gum UI
function pbgist() {
if ! command -v gum &>/dev/null; then
echo "Error: gum is required. Install it with: brew install gum" >&2
return 1
fi
if ! command -v gh &>/dev/null; then
echo "Error: gh is required. Install it with: brew install gh" >&2
return 1
fi
local clipboard
clipboard="$(pbpaste)"
if [[ -z "$clipboard" ]]; then
echo "Error: clipboard is empty" >&2
return 1
fi
# Preview clipboard contents
echo
gum style --border rounded --border-foreground 243 --padding "0 1" --bold "Clipboard Preview"
echo "$clipboard" | head -20
local total_lines
total_lines=$(echo "$clipboard" | wc -l | tr -d ' ')
if (( total_lines > 20 )); then
gum style --faint "... ($total_lines total lines)"
fi
echo
# Filename (used as the gist title)
local filename
filename=$(gum input --placeholder "e.g. snippet.py" --header "Filename" --char-limit 200)
[[ $? -ne 0 ]] && return 130
# Description
local description
description=$(gum input --placeholder "Optional description" --header "Description" --char-limit 500)
[[ $? -ne 0 ]] && return 130
# Public or secret
local public=false
if gum confirm "Make this gist public?" --default=false; then
public=true
fi
# Build and display the command
local cmd="pbpaste | gh gist create"
[[ -n "$filename" ]] && cmd+=" --filename $(printf '%q' "$filename")"
[[ -n "$description" ]] && cmd+=" --desc $(printf '%q' "$description")"
[[ "$public" == "true" ]] && cmd+=" --public"
echo
gum style --faint "$cmd"
echo
if ! gum confirm "Create gist?"; then
echo "Cancelled."
return 130
fi
# Create the gist
local args=()
[[ -n "$filename" ]] && args+=(--filename "$filename")
[[ -n "$description" ]] && args+=(--desc "$description")
[[ "$public" == "true" ]] && args+=(--public)
echo "$clipboard" | gh gist create "${args[@]}" -
}
alias pbg=pbgist
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment