Skip to content

Scripting

phlx0 edited this page Mar 15, 2026 · 1 revision

Scripting with snip

snip is designed to compose with other tools. Here are patterns for using it in scripts and automation.

Capture content silently

Use -q to suppress the "Copied to clipboard" message so stdout is clean:

TOKEN=$(snip -q my-api-token)
curl -H "Authorization: Bearer $TOKEN" https://api.example.com

Run a snippet and check exit code

snip run deploy-prod
if [[ $? -ne 0 ]]; then
  echo "deploy failed"
  exit 1
fi

Pipe snippet content into a command

snip -q my-sql-query | psql -U myuser mydb
snip -q nginx-config | ssh user@server "cat > /etc/nginx/nginx.conf"

Use JSON output for structured data

# Get the content field only
snip --json ports | jq -r '.content'

# Get tags
snip --json deploy | jq -r '.tags[]'

# Check language
snip --json myscript | jq -r '.language'

Iterate over all snippets

snip --list | while read -r title; do
  echo "=== $title ==="
  snip -q "$title"
  echo
done

Filter by tag and run each

snip --list startup | while read -r title; do
  snip run "$title"
done

Automated backup in cron

# Add to crontab: crontab -e
0 9 * * * snip --export > ~/dotfiles/snippets-$(date +%F).json

Check if a snippet exists

if snip -q mysnippet > /dev/null 2>&1; then
  echo "found"
else
  echo "not found"
fi

Clone this wiki locally