1 # System clipboard integration
3 # This file has support for doing system clipboard copy and paste operations
4 # from the command line in a generic cross-platform fashion.
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
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)
20 # Defines two functions, clipcopy and clippaste, based on the detected platform.
23 # clipcopy - Copy data to clipboard
27 # <command> | clipcopy - copies stdin to clipboard
29 # clipcopy <file> - copies a file's contents to clipboard
33 # clippaste - "Paste" data from clipboard to stdout
37 # clippaste - writes clipboard's contents to stdout
39 # clippaste | <command> - pastes contents and pipes it to another process
41 # clippaste > <file> - paste contents to a file
45 # # Pipe to another process
46 # clippaste | grep foo
49 # clippaste > file.txt
51 function detect-clipboard() {
54 if [[ "${OSTYPE}" == darwin* ]] && (( ${+commands[pbcopy]} )) && (( ${+commands[pbpaste]} )); then
55 function clipcopy() { cat "${1:-/dev/stdin}" | pbcopy; }
56 function clippaste() { pbpaste; }
57 elif [[ "${OSTYPE}" == (cygwin|msys)* ]]; then
58 function clipcopy() { cat "${1:-/dev/stdin}" > /dev/clipboard; }
59 function clippaste() { cat /dev/clipboard; }
60 elif [ -n "${WAYLAND_DISPLAY:-}" ] && (( ${+commands[wl-copy]} )) && (( ${+commands[wl-paste]} )); then
61 function clipcopy() { cat "${1:-/dev/stdin}" | wl-copy &>/dev/null &|; }
62 function clippaste() { wl-paste; }
63 elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xsel]} )); then
64 function clipcopy() { cat "${1:-/dev/stdin}" | xsel --clipboard --input; }
65 function clippaste() { xsel --clipboard --output; }
66 elif [ -n "${DISPLAY:-}" ] && (( ${+commands[xclip]} )); then
67 function clipcopy() { cat "${1:-/dev/stdin}" | xclip -selection clipboard -in &>/dev/null &|; }
68 function clippaste() { xclip -out -selection clipboard; }
69 elif (( ${+commands[lemonade]} )); then
70 function clipcopy() { cat "${1:-/dev/stdin}" | lemonade copy; }
71 function clippaste() { lemonade paste; }
72 elif (( ${+commands[doitclient]} )); then
73 function clipcopy() { cat "${1:-/dev/stdin}" | doitclient wclip; }
74 function clippaste() { doitclient wclip -r; }
75 elif (( ${+commands[win32yank]} )); then
76 function clipcopy() { cat "${1:-/dev/stdin}" | win32yank -i; }
77 function clippaste() { win32yank -o; }
78 elif [[ $OSTYPE == linux-android* ]] && (( $+commands[termux-clipboard-set] )); then
79 function clipcopy() { cat "${1:-/dev/stdin}" | termux-clipboard-set; }
80 function clippaste() { termux-clipboard-get; }
81 elif [ -n "${TMUX:-}" ] && (( ${+commands[tmux]} )); then
82 function clipcopy() { tmux load-buffer "${1:--}"; }
83 function clippaste() { tmux save-buffer -; }
84 elif [[ $(uname -r) = *icrosoft* ]]; then
85 function clipcopy() { cat "${1:-/dev/stdin}" | clip.exe; }
86 function clippaste() { powershell.exe -noprofile -command Get-Clipboard; }
88 function _retry_clipboard_detection_or_fail() {
89 local clipcmd="${1}"; shift
90 if detect-clipboard; then
93 print "${clipcmd}: Platform $OSTYPE not supported or xclip/xsel not installed" >&2
97 function clipcopy() { _retry_clipboard_detection_or_fail clipcopy "$@"; }
98 function clippaste() { _retry_clipboard_detection_or_fail clippaste "$@"; }
103 # Detect at startup. A non-zero exit here indicates that the dummy clipboards were set,
104 # which is not really an error. If the user calls them, they will attempt to redetect
105 # (for example, perhaps the user has now installed xclip) and then either print an error
106 # or proceed successfully.
107 detect-clipboard || true