Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jteneycke/311cb11b034cc350fd945f46864967a3 to your computer and use it in GitHub Desktop.
Save jteneycke/311cb11b034cc350fd945f46864967a3 to your computer and use it in GitHub Desktop.
Sharing clipboards between vim and tmux without xsel or xclip or X forwarding
" Share clipboards between vim and tmux without xsel or xclip (which require X and
" X forwarding with SSH) and without changing tmux shortcuts. Requires only tail.
"
" Great for an ssh session to you linode or droplet.
"
" Uses z buffer in vim and writes output to ~/.clipboard and then to tmux's paste
" buffer, and reads it back in cleanly for putting (puddin').
" Example vimrc mappings
" Visual mode yank selected area to tmux paste buffer (clipboard)
"vnoremap <leader>c "zy:silent! call SendZBufferToHomeDotClipboard()<cr>
" Put from tmux clipboard
"map <leader>v :silent! call HomeDotClipboardPut()<cr>
function! SendZBufferToHomeDotClipboard()
" Yank the contents buffer z to file ~/.clipboard and tmux paste buffer
" For use with HomeDotClipboardPut()
silent! redir! > ~/.clipboard
silent! echo @z
silent! redir END
" the redir has a newline in front, so tail -n+2 skips first line
silent! !tail -n+2 ~/.clipboard > ~/.clipboard.1;mv ~/.clipboard.1 ~/.clipboard
silent! !tmux set-buffer "$(cat ~/.clipboard)"
silent! redraw!
endfunction
function! HomeDotClipboardPut()
" Paste/Put the contents of file ~/.clipboard
" For use with SendZBufferToHomeDotClipboard()
silent! !tmux save-buffer ~/.clipboard
silent! redraw!
silent! let @z = system("cat ~/.clipboard")
" put the z buffer on the line below
silent! exe "norm o\<ESC>\"zp"
endfunction
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment