clipboard.zsh 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # System clipboard integration
  2. #
  3. # This file has support for doing system clipboard copy and paste operations
  4. # from the command line in a generic cross-platform fashion.
  5. #
  6. # On OS X and Windows, the main system clipboard or "pasteboard" is used. On other
  7. # Unix-like OSes, this considers the X Windows CLIPBOARD selection to be the
  8. # "system clipboard", and the X Windows `xclip` command must be installed.
  9. # clipcopy - Copy data to clipboard
  10. #
  11. # Usage:
  12. #
  13. # <command> | clipcopy - copies stdin to clipboard
  14. #
  15. # clipcopy <file> - copies a file's contents to clipboard
  16. #
  17. function clipcopy() {
  18. emulate -L zsh
  19. local file=$1
  20. if [[ $OSTYPE == darwin* ]]; then
  21. if [[ -z $file ]]; then
  22. pbcopy
  23. else
  24. cat $file | pbcopy
  25. fi
  26. elif [[ $OSTYPE == cygwin* ]]; then
  27. if [[ -z $file ]]; then
  28. cat > /dev/clipboard
  29. else
  30. cat $file > /dev/clipboard
  31. fi
  32. else
  33. which xclip &>/dev/null
  34. if [[ $? != 0 ]]; then
  35. print "clipcopy: Platform $OSTYPE not supported or xclip not installed" >&2
  36. return 1
  37. fi
  38. if [[ -z $file ]]; then
  39. xclip -in -selection clipboard
  40. else
  41. xclip -in -selection clipboard $file
  42. fi
  43. fi
  44. }
  45. # clippaste - "Paste" data from clipboard to stdout
  46. #
  47. # Usage:
  48. #
  49. # clippaste - writes clipboard's contents to stdout
  50. #
  51. function clippaste() {
  52. emulate -L zsh
  53. if [[ $OSTYPE == darwin* ]]; then
  54. pbpaste
  55. elif [[ $OSTYPE == cygwin* ]]; then
  56. cat /dev/clipboard
  57. else
  58. which xclip &>/dev/null
  59. if [[ $? != 0 ]]; then
  60. print "clipcopy: Platform $OSTYPE not supported or xclip not installed" >&2
  61. return 1
  62. fi
  63. xclip -out -selection clipboard
  64. fi
  65. }