Skip to content

Instantly share code, notes, and snippets.

@agriffis
Created February 25, 2019 12:53
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save agriffis/70360287f9016fd8849b8150a4966469 to your computer and use it in GitHub Desktop.
Save agriffis/70360287f9016fd8849b8150a4966469 to your computer and use it in GitHub Desktop.
neovim clipboard provider for tmux and OSC 52
function! ClipboardCopy(lines, regtype)
let sum = TryClipboardCmd('md5sum', a:lines)
call writefile(sum, s:regtype_sum, 'S')
call writefile([a:regtype], s:regtype_txt, 'S')
return TryClipboardCmd('clipboard-provider copy', a:lines)
endfunction
function! ClipboardPaste()
let lines = TryClipboardCmd('clipboard-provider paste')
let regtype = 'V'
if filereadable(s:regtype_sum) && filereadable(s:regtype_txt)
let actual = TryClipboardCmd('md5sum', lines)
let expected = readfile(s:regtype_sum)
if actual == expected
let regtype = readfile(s:regtype_txt)[0]
endif
endif
return [lines, regtype]
endfunction
let g:clipboard = {
\ 'copy': {
\ '+': function('ClipboardCopy'),
\ '*': WithEnv({'COPY_PROVIDERS': 'tmux'}, function('ClipboardCopy')),
\ },
\ 'paste': {
\ '+': function('ClipboardPaste'),
\ '*': WithEnv({'PASTE_PROVIDERS': 'tmux'}, function('ClipboardPaste')),
\ }}
#!/bin/bash
#
# clipboard provider for neovim
#
# :help provider-clipboard
#exec 2>> ~/clipboard-provider.out
#set -x
: ${COPY_PROVIDERS:=tmux osc52}
: ${PASTE_PROVIDERS:=tmux}
: ${TTY:=`(tty || tty </proc/$PPID/fd/0) 2>/dev/null | grep /dev/`}
main() {
declare p status=99
case $1 in
copy)
slurp
for p in $COPY_PROVIDERS; do
$p-provider copy && status=0
done ;;
paste)
for p in $PASTE_PROVIDERS; do
$p-provider paste && status=0 && break
done ;;
esac
exit $status
}
# N.B. buffer is global for simplicity
slurp() { buffer=$(base64); }
spit() { base64 --decode <<<"$buffer"; }
tmux-provider() {
[[ -n $TMUX ]] || return
case $1 in
copy) spit | tmux load-buffer - ;;
paste) tmux save-buffer - ;;
esac
}
osc52-provider() {
case $1 in
copy) [[ -n "$TTY" ]] && printf $'\e]52;c;%s\a' "$buffer" > "$TTY" ;;
paste) return 1 ;;
esac
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment