← retour aux snippets

pbcopy/xclip: copier dans le presse-papiers

Envoyer et récupérer du texte depuis le presse-papiers sur macOS, Linux et Windows en ligne de commande.


objectif

Copier et coller du texte entre le terminal et le presse-papiers système de façon portable, pour partager des sorties de commandes ou des clés.

code minimal

# macOS
printf '%s' 'hello' | pbcopy
pbpaste

# Linux (xclip)
printf '%s' 'hello' | xclip -selection clipboard
xclip -o -selection clipboard

utilisation

# copier le contenu d'un fichier texte dans le presse-papiers
xclip -selection clipboard -t text/plain < README.md            # Linux
pbcopy < README.md                                              # macOS
type README.md | clip                                           # Windows PowerShell/cmd

# copier la sortie d'une commande
kubectl get pods -A | xclip -selection clipboard                # Linux
kubectl get pods -A | pbcopy                                    # macOS

# coller le presse-papiers vers un fichier
xclip -o -selection clipboard > snippet.txt                     # Linux
pbpaste > snippet.txt                                           # macOS

# copier sans ajouter de newline final
printf '%s' "$TOKEN" | xclip -selection clipboard
printf '%s' "$TOKEN" | pbcopy

# copier du JSON proprement (type mime explicite sous xclip)
jq -c . data.json | xclip -selection clipboard -t application/json
jq -c . data.json | pbcopy

variante(s) utile(s)

# Linux: alternative avec xsel
printf '%s' 'hello' | xsel --clipboard --input
xsel --clipboard --output

# fonction portable "copy" (macOS, Linux xclip/xsel, Windows clip)
copy() {
  if command -v pbcopy >/dev/null 2>&1; then pbcopy
  elif command -v xclip >/dev/null 2>&1; then xclip -selection clipboard
  elif command -v xsel  >/dev/null 2>&1; then xsel --clipboard --input
  elif command -v clip  >/dev/null 2>&1; then clip
  else echo "no clipboard tool found" >&2; return 1
  fi
}
# usage:
echo "secret" | copy

# fonction portable "paste"
paste_clip() {
  if command -v pbpaste >/dev/null 2>&1; then pbpaste
  elif command -v xclip   >/dev/null 2>&1; then xclip -o -selection clipboard
  elif command -v xsel    >/dev/null 2>&1; then xsel --clipboard --output
  elif command -v powershell >/dev/null 2>&1; then powershell -NoProfile -Command 'Get-Clipboard'
  else echo "no clipboard getter found" >&2; return 1
  fi
}
# usage:
paste_clip > out.txt

# typer explicitement pour eviter les encodages etranges (Linux)
printf '%s' "$HTML" | xclip -selection clipboard -t text/html
printf '%s' "$TEXT" | xclip -selection clipboard -t text/plain

# copier une cle publique ssh
cat ~/.ssh/id_ed25519.pub | copy

notes

  • sur Linux, installez xclip (ou xsel) pour manipuler le presse-papiers X11/Wayland; sur macOS, pbcopy/pbpaste sont natifs.
  • clip est disponible sur Windows (cmd/PowerShell). En PowerShell moderne, Set-Clipboard et Get-Clipboard existent aussi.
  • utilisez printf '%s' pour eviter un retour a la ligne additionnel qui pollue les tokens.
  • sous Wayland, certaines distributions requierent wl-clipboard (wl-copy, wl-paste) comme alternative a xclip/xsel.