SSH'd (via Cygwin) vim to write to local file

1

1

Okay, so I'm trying to solve the problem of getting the contents of a yanked register from a SSH'd session in Vim, to go to the Windows clipboard.

Here's the scenario:

  • SSH into development environment
  • Use Vim to edit files on server (not with local Cygwin Vim)
  • Yank text

What I want to do, is use the built-in /dev/clipboard in Cygwin to get the yanked contents so that I can start to share between the two.

There's a VimScript to do this locally (i.e., you have direct access to /dev/clipboard):

function! Putclip(type, ...) range
  let sel_save = &selection
  let &selection = "inclusive"
  let reg_save = @@
  if a:type == 'n'
    silent exe a:firstline . "," . a:lastline . "y"
  elseif a:type == 'c'
    silent exe a:1 . "," . a:2 . "y"
  else
    silent exe "normal! `<" . a:type . "`>y"
  endif
  "call system('putclip', @@)
  "As of Cygwin 1.7.13, the /dev/clipboard device was added to provide
  "access to the native Windows clipboard. It provides the added benefit
  "of supporting utf-8 characters which putclip currently does not. Based
  "on a tip from John Beckett, use the following:
  call writefile(split(@@,"\n"), '/dev/clipboard')
  let &selection = sel_save
  let @@ = reg_save
endfunction

vnoremap <silent> <leader>y :call Putclip(visualmode(), 1)<CR>
nnoremap <silent> <leader>y :call Putclip('n', 1)<CR>

http://vim.wikia.com/wiki/Using_the_Windows_clipboard_in_Cygwin_Vim

What I would like to do is see if there's a way I can talk to Cygwin from here, or if I can scp the contents (or something along those lines). I can't/won't store passwords/passphrases, and ideally, I want this to be as "transparent" as possible.

Also, I'm running this through tmux, if that opens up access anywhere. Not sure if that's relevant, as I've just started playing with tmux yesterday (there might be something that I'm unaware of).

Thoughts?

Tango Bravo

Posted 2014-06-25T17:05:43.460

Reputation: 111

Did you try X forwarding, $ ssh -X hostname? – romainl – 2014-06-26T07:22:46.047

Unfortunately we don't have X11 installed on the server I'm Vimming on. We do have pynotify, there though. So as an exercise in nerdiness, I might have it watch a file that gets written to when a specifically mapped yank is performed, then it'll rsync the contents from there to /dev/clipboard on my local machine, which should give me access. The TCP handshake might take a second or two, but ... we'll see how it works. – Tango Bravo – 2014-06-26T14:28:08.747

Answers

0

Here's my solution:

function! WinCpy ()
    let s:wincpy = exists('s:wincpy') ? !s:wincpy : 1

    if !s:wincpy
        :q
    else
        silent exec "normal \<C-w>S"
        silent exec "normal \<C-w>T"
        silent exec "set nonu"
        silent exec "set wrap"
    endif
endfunction


vnoremap <leader>y :call WinCpy()<CR>
nnoremap <leader>y :call WinCpy()<CR>

It works well for what it does: creates a new tab, gets hidden (unwrapped) text, and preserves the buffer layout.

Tango Bravo

Posted 2014-06-25T17:05:43.460

Reputation: 111

Hey, this is the exact same question i have, but using your solution didn't seem to work for me. Tell me if this isn't how I'm supposed to use: In vim, I press v and highlight a few lines, then I press \y and it seems to run the command, but then when I try pasting, it's not in the clipboard – rasen58 – 2016-03-19T21:43:46.247

0

While this doesn't directly answer your question, it is the solution I use for the same scenario (ssh/tmux/vim).

I recommend the mintty cygwin console window instead of the standard one. This allows copying and pasting. In mintty, selecting with the left-mouse also copies, and middle-mouse pastes.

If vim has visible line numbers you'll either have to turn them off temporarily, or what I do is just cat the file in a new tmux window.

http://cygwin.com/faq/faq.html#faq.using.copy-and-paste

Jarvin

Posted 2014-06-25T17:05:43.460

Reputation: 6 712

I think it's a good solution in many situations (:set nonu), but when the lines extend beyond what is visible, it isn't the best. That's mostly the situation I'm trying to resolve. I suppose I could do an action, which turns on wrapping and turns on numbers. I wonder if there's a way to monitor a file and rsync it when it changes values. That might work too, and it could take the problem out of Vim, and bring it to the CLI where there are more tools available. – Tango Bravo – 2014-06-25T17:31:41.733

@Tim For monitoring and automatic rsync, I've found dropbox to be a good solution for my personal use. – Jarvin – 2014-06-25T17:41:19.647

Thanks (: I'm looking into inotify, to see if that's a decent solution. Seems hopeful (even though there might be a second delay). – Tango Bravo – 2014-06-25T18:08:12.330

If you're interested, check my solution. I think it works well, and gives you access to everything if it is wrapped. – Tango Bravo – 2014-07-08T19:56:22.683

i'm all for mintty, I understand it's better for unicode support, but you write that you recommend "the mintty cygwin console window instead of the standard one. This allows copying and pasting" <-- Actually the standard one allows copy and paste very easily. if you run cmd.exe and execute cygwin.bat then the rules for copy/paste are the standard ones for cmd. go to properties in the top left click menu(alt-space menu), and the options tab and turn on quickedit mode. then highlgiht and push enter, is copy. and right click is paste. – barlop – 2014-07-08T20:09:08.110

I like mintty, too. That's what I've been using. Thanks for the Quickedit tip, though! (: – Tango Bravo – 2014-07-08T20:12:20.507

@Tim you probably know, but anyhow, from wikipedia "towards the end of 2011, mintty became Cygwin's default terminal" – barlop – 2014-07-11T01:28:58.090

Yeah. (: I actually set up another solution, which removes the need for the solution I provided. I'm still editing the file from Vim on the remote server (instead of using the one that is packaged with Cygwin), but I have a Samba share too. So I have a solution that I built with inotify, which does everything seamlessly. It's beautiful. – Tango Bravo – 2014-07-11T02:34:28.920