6

How to augment vim yank and paste, so when I yank, vim writes the content to a file. When I paste, it uses the content from the file. I want to have a system wide file which serves as a global buffer.

Context: I run vim in different tmux splits on a remote server (over ssh). I want seamlessly copy and paste between vims in tmux splits. I tried a bunch of plugins, but none of them worked, thus the question.

dlmeetei
  • 9,905
  • 3
  • 31
  • 38
ashim
  • 24,380
  • 29
  • 72
  • 96
  • Perhaps https://superuser.com/questions/235117/vim-copy-paste-across-terminals or https://stackoverflow.com/questions/11042920/how-to-copy-and-paste-between-different-tmux-panes-running-vim-instances might help – zedfoxus Aug 23 '17 at 04:43
  • where exactly do you got stuck? It should be possible to write a range-command which does that, and overwrite `y` and `p`. But i would recommend not using `y` and `p` since there is a lot of regsiter handling you need to care of. – Doktor OSwaldo Aug 23 '17 at 05:58
  • @DoktorOSwaldo I tried that answer https://stackoverflow.com/a/11052783/1082727 . However, copy/pasting is not stable. Sometimes it copies sometimes it does not. I do not know how to diagnose the problem. There probably some intricacies on how vim writes and reads from a file. – ashim Aug 23 '17 at 17:51
  • has the server an X-server? then X-Forwarding would be a possibility. If not using vim splits instead of tmux splits is worth a thougth, but not suitable for all usecases. Using the tmux clipboard should actually work. i can't really help you further there, only hint that it has to be the same tmux session to work. Writing the pasted text to a file could be achieved, however, if you want to use the normal vim commands it will be non trivial, and i would not recommand going that way. – Doktor OSwaldo Aug 24 '17 at 13:02
  • You don't need to write to a file to do this. Just fix your clipboard integration. I regularly ssh to two different servers in different terminal windows and can yank in one vim and paste in another, whether I'm using Windows, MacOS, or Linux. How to do so depends on what OS and terminal you're using on each side, and tmux complicates it (but still works), so you'll have to research your particular situation. – Jim Stewart Jan 14 '18 at 14:57

3 Answers3

1

I believe vim allows you to do it like this:

  1. Yank someting in file A
  2. :wv and hit enter
  3. Go to file B
  4. :rv and hit enter
  5. Hit p to paste the content of your (now) global clipboard

This should work across your different vim sessions independently of whether they run in different tmux panes or not.

1

You want to have a look at:

  • map-operator because y is an operator: a motion pending command
  • readfile() and writefile() cause the r or w are commands, nice to use but not for scripts
  • visualmode() to get the selection

The shared file was hardcoded /tmp/yank.txt:

function! Yank(type, ...)
  " Invoked from Visual mode, use gv command.
  if a:0
    silent exe "normal! gvy"
  elseif a:type == 'line'
    silent exe "normal! '[V']y"
  else
    silent exe "normal! `[v`]y"
  endif

  call writefile([@@], '/tmp/yank.txt')
endfunction


function! Paste(is_back)
  let @t = join(readfile('/tmp/yank.txt'), '')
  if a:is_back
    normal! "tP
  else
    normal! "tp
  endif
endfunction


nnoremap y :set opfunc=Yank<CR>g@
nmap Y Vy
vnoremap y :call Yank(visualmode(), 1)<CR>
vnoremap Y :call Yank(visualmode(), 1)<CR>


nnoremap p :call Paste(0)<CR>
nnoremap P :call Paste(1)<CR>
Tinmarino
  • 3,693
  • 24
  • 33
0

The yankring plugin saves the yanks in a file. And you can then paste any of the previous texts that you have copied by p<C-n> or p<C-p>.

If this one doesn't do it, you can at least try to reverse engineer it to your needs.

Dimitar Slavchev
  • 192
  • 2
  • 11