clipboard.zsh 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. # This is uses essentially the same heuristic as neovim, with the additional
  7. # special support for Cygwin.
  8. # See: https://github.com/neovim/neovim/blob/e682d799fa3cf2e80a02d00c6ea874599d58f0e7/runtime/autoload/provider/clipboard.vim#L55-L121
  9. #
  10. # - pbcopy, pbpaste (macOS)
  11. # - cygwin (Windows running Cygwin)
  12. # - wl-copy, wl-paste (if $WAYLAND_DISPLAY is set)
  13. # - xsel (if $DISPLAY is set)
  14. # - xclip (if $DISPLAY is set)
  15. # - lemonade (for SSH) https://github.com/pocke/lemonade
  16. # - doitclient (for SSH) http://www.chiark.greenend.org.uk/~sgtatham/doit/
  17. # - win32yank (Windows)
  18. # - tmux (if $TMUX is set)
  19. #
  20. # Defines two functions, clipcopy and clippaste, based on the detected platform.
  21. ##
  22. #
  23. # clipcopy - Copy data to clipboard
  24. #
  25. # Usage:
  26. #
  27. # <command> | clipcopy - copies stdin to clipboard
  28. #
  29. # clipcopy <file> - copies a file's contents to clipboard
  30. #
  31. ##
  32. #
  33. # clippaste - "Paste" data from clipboard to stdout
  34. #
  35. # Usage:
  36. #
  37. # clippaste - writes clipboard's contents to stdout
  38. #
  39. # clippaste | <command> - pastes contents and pipes it to another process
  40. #
  41. # clippaste > <file> - paste contents to a file
  42. #
  43. # Examples:
  44. #
  45. # # Pipe to another process
  46. # clippaste | grep foo
  47. #
  48. # # Paste to a file
  49. # clippaste > file.txt
  50. #
  51. function detect-clipboard() {
  52. emulate -L zsh
  53. if [[ "${OSTYPE}" == darwin* ]] && (( ${+commands[pbcopy]} )) && (( ${+commands[pbpaste]} )); then
  54. function clipcopy() { cat "${1:-/dev/stdin}" | pbcopy; }
  55. function clippaste() { pbpaste; }
  56. elif [[ "${OSTYPE}" == (cygwin|msys)* ]]; then
  57. function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; }
  58. function clippaste() { cat /dev/clipboard; }
  59. elif [ -n "${WAYLAND_DISPLAY:-}" ] && (( ${+commands[wl-copy]} )) && (( ${+commands[wl-paste]} )); then
  60. function clipcopy() { cat "${1:-/dev/stdin}" | wl-copy &>/dev/null &|; }
  61. function clippaste() { wl-paste; }
  62. elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xsel]} )); then
  63. function clipcopy() { cat "${1:-/dev/stdin}" | xsel --clipboard --input; }
  64. function clippaste() { xsel --clipboard --output; }
  65. elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xclip]} )); then
  66. function clipcopy() { cat "${1:-/dev/stdin}" | xclip -selection clipboard -in &>/dev/null &|; }
  67. function clippaste() { xclip -out -selection clipboard; }
  68. elif (( ${+commands[lemonade]} )); then
  69. function clipcopy() { cat "${1:-/dev/stdin}" | lemonade copy; }
  70. function clippaste() { lemonade paste; }
  71. elif (( ${+commands[doitclient]} )); then
  72. function clipcopy() { cat "${1:-/dev/stdin}" | doitclient wclip; }
  73. function clippaste() { doitclient wclip -r; }
  74. elif (( ${+commands[win32yank]} )); then
  75. function clipcopy() { cat "${1:-/dev/stdin}" | win32yank -i; }
  76. function clippaste() { win32yank -o; }
  77. elif [[ $OSTYPE == linux-android* ]] && (( $+commands[termux-clipboard-set] )); then
  78. function clipcopy() { cat "${1:-/dev/stdin}" | termux-clipboard-set; }
  79. function clippaste() { termux-clipboard-get; }
  80. elif [ -n "${TMUX:-}" ] && (( ${+commands[tmux]} )); then
  81. function clipcopy() { tmux load-buffer "${1:--}"; }
  82. function clippaste() { tmux save-buffer -; }
  83. elif [[ $(uname -r) = *icrosoft* ]]; then
  84. function clipcopy() { cat "${1:-/dev/stdin}" | clip.exe; }
  85. function clippaste() { powershell.exe -noprofile -command Get-Clipboard; }
  86. else
  87. function _retry_clipboard_detection_or_fail() {
  88. local clipcmd="${1}"; shift
  89. if detect-clipboard; then
  90. "${clipcmd}" "$@"
  91. else
  92. print "${clipcmd}: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
  93. return 1
  94. fi
  95. }
  96. function clipcopy() { _retry_clipboard_detection_or_fail clipcopy "$@"; }
  97. function clippaste() { _retry_clipboard_detection_or_fail clippaste "$@"; }
  98. return 1
  99. fi
  100. }
  101. # Detect at startup. A non-zero exit here indicates that the dummy clipboards were set,
  102. # which is not really an error. If the user calls them, they will attempt to redetect
  103. # (for example, perhaps the user has now installed xclip) and then either print an error
  104. # or proceed successfully.
  105. detect-clipboard || true