clipboard.zsh 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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|msys)* ]]; then
  27. if [[ -z $file ]]; then
  28. cat > /dev/clipboard
  29. else
  30. cat $file > /dev/clipboard
  31. fi
  32. else
  33. if (( $+commands[xclip] )); then
  34. if [[ -z $file ]]; then
  35. xclip -in -selection clipboard
  36. else
  37. xclip -in -selection clipboard $file
  38. fi
  39. elif (( $+commands[xsel] )); then
  40. if [[ -z $file ]]; then
  41. xsel --clipboard --input
  42. else
  43. cat "$file" | xsel --clipboard --input
  44. fi
  45. else
  46. print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
  47. return 1
  48. fi
  49. fi
  50. }
  51. # clippaste - "Paste" data from clipboard to stdout
  52. #
  53. # Usage:
  54. #
  55. # clippaste - writes clipboard's contents to stdout
  56. #
  57. # clippaste | <command> - pastes contents and pipes it to another process
  58. #
  59. # clippaste > <file> - paste contents to a file
  60. #
  61. # Examples:
  62. #
  63. # # Pipe to another process
  64. # clippaste | grep foo
  65. #
  66. # # Paste to a file
  67. # clippaste > file.txt
  68. function clippaste() {
  69. emulate -L zsh
  70. if [[ $OSTYPE == darwin* ]]; then
  71. pbpaste
  72. elif [[ $OSTYPE == (cygwin|msys)* ]]; then
  73. cat /dev/clipboard
  74. else
  75. if (( $+commands[xclip] )); then
  76. xclip -out -selection clipboard
  77. elif (( $+commands[xsel] )); then
  78. xsel --clipboard --output
  79. else
  80. print "clipcopy: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
  81. return 1
  82. fi
  83. fi
  84. }