Skip to content

Instantly share code, notes, and snippets.

@agriffis
Created Feb 25, 2019
Embed
What would you like to do?
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