How to cause vim to copy text to GNU screen’s clipboard buffer

clipboardgnu-screenvim

I want to yank text with a command to vim and have it end up in screen's clipboard where it can be pasted to a different application via ^A] later. vim needs to do the copying (not screen's copy mode) because I have more text than will fit on screen at one time.

Answers to other questions have approached this issue, but they mostly rely on using vim's interfaces + and * to the X clipboard, which is not available to applications started remotely or not in the presence of an X session to begin with. I'm working through puTTY, but that's incidental as I just want to transfer between screens and not [necessarily] out to local.

The best thing would be the existence of a magic buffer in vim that connects to screen, but I'll listen to workarounds : )

Best Answer

If some extra keystrokes do not bother you, I can‘t see a problem.

GNU Screen’s copy-paste register (.) can be read from / written to file out of a box: <C-a>< and <C-a>> are default hotkeys, /tmp/screen-exchange is a default file, but I’d prefer a user-specific rather than system-wide so I would set something like this in the .screenrc:

setenv BUFFERFILE "$HOME/.buffer" 
bufferfile "$BUFFERFILE"

Vim has no such commands out of a box, but there is no difficulty to create and map them to whatever you want, e. g. <leader>< and <leader>> respectively:

if exists("$BUFFERFILE")
    nnoremap <silent><leader>< :let @" = join(readfile($BUFFERFILE), "\n")<CR>
    nnoremap <silent><leader>> :call writefile( split(@", "\n"), $BUFFERFILE )<CR>
endif

If they do bother you however, it becomes a little more complicated – as far as understood, you have to:

  • remap all possible yank, cut (since there is no autocmd event at changing certain register) and paste (since it’s actually the only feasible trigger) shortcuts in Vim – nothing complex but tedious – I hope, you know how to do it;
  • remap two shortcuts in GNU Screen: while pasting one is quite obvious:

     bind ] eval readbuf "paste ."
    

    yanking one – <Return> or <Space> in a special copying mode presents a difficulty to me.

Related Question