├── README.md └── plugin └── vimtmuxclipboard.vim /README.md: -------------------------------------------------------------------------------- 1 | 2 | # vim-tmux-clipboard 3 | 4 | Things get messy when I need to copy lines of text from vim into tmux's 5 | clipboard, especially when multiple split-windows are opened. So I created this 6 | super simple plugin, which provides seamless integration for vim and tmux's 7 | clipboard. 8 | 9 | 10 | vim-tmux-clipboard automatically copy yanked text into tmux's clipboard, and 11 | copy tmux's clipboard content into vim's quote(`"`) register, known as the unnamed 12 | register. It also makes multiple vim processes on top of the same tmux session 13 | act like they're sharing the same clipboard. 14 | 15 | 16 | ## Requirements 17 | 18 | - add `set -g focus-events on` to your `tmux.conf`. 19 | - [vim-tmux-focus-events](https://github.com/tmux-plugins/vim-tmux-focus-events) for vim users. 20 | - [neovim](https://github.com/neovim/neovim) or vim above 8.0.1394 is 21 | recommended for `TextYankPost` event, which is required for `It also makes 22 | multiple vim processes on top of the same tmux session act like they're 23 | sharing the same clipboard`. 24 | 25 | 26 | ## Demo 27 | 28 | [![asciicast](https://asciinema.org/a/7qzb7c12ykv3kcleo4jgrl2jy.png)](https://asciinema.org/a/7qzb7c12ykv3kcleo4jgrl2jy) 29 | 30 | 31 | -------------------------------------------------------------------------------- /plugin/vimtmuxclipboard.vim: -------------------------------------------------------------------------------- 1 | func! s:TmuxBufferName() 2 | let l:list = systemlist('tmux list-buffers -F"#{buffer_name}"') 3 | if len(l:list)==0 4 | return "" 5 | else 6 | return l:list[0] 7 | endif 8 | endfunc 9 | 10 | func! s:TmuxBuffer() 11 | return system('tmux show-buffer') 12 | endfunc 13 | 14 | func! s:Enable() 15 | 16 | if $TMUX=='' 17 | " not in tmux session 18 | return 19 | endif 20 | 21 | let s:lastbname="" 22 | 23 | " if support TextYankPost 24 | if exists('##TextYankPost')==1 25 | " @" 26 | augroup vimtmuxclipboard 27 | autocmd! 28 | autocmd FocusLost * call s:update_from_tmux() 29 | autocmd FocusGained * call s:update_from_tmux() 30 | autocmd TextYankPost * silent! call system('tmux loadb -',join(v:event["regcontents"],"\n")) 31 | autocmd TextYankPost * silent! call system('~/.tmux/yank.sh',join(v:event["regcontents"],"\n")) 32 | augroup END 33 | let @" = s:TmuxBuffer() 34 | else 35 | " vim doesn't support TextYankPost event 36 | " This is a workaround for vim 37 | augroup vimtmuxclipboard 38 | autocmd! 39 | autocmd FocusLost * silent! call system('tmux loadb -',@") 40 | autocmd TextYankPost * silent! call system('~/.tmux/yank.sh',@") 41 | autocmd FocusGained * let @" = s:TmuxBuffer() 42 | augroup END 43 | let @" = s:TmuxBuffer() 44 | endif 45 | endfunc 46 | 47 | func! s:update_from_tmux() 48 | let buffer_name = s:TmuxBufferName() 49 | if s:lastbname != buffer_name 50 | let @" = s:TmuxBuffer() 51 | endif 52 | let s:lastbname=s:TmuxBufferName() 53 | endfunc 54 | 55 | call s:Enable() 56 | 57 | --------------------------------------------------------------------------------