Skip to content

Instantly share code, notes, and snippets.

@ljjjustin
Last active May 19, 2022 04:42
Show Gist options
  • Save ljjjustin/f99171b337213d5e00de6ac0c87b631b to your computer and use it in GitHub Desktop.
Save ljjjustin/f99171b337213d5e00de6ac0c87b631b to your computer and use it in GitHub Desktop.
vim remote copy
# How to setup?
1. local: add sshconfig to your ~/。ssh/config
2. local: copy pbcopy.plist to HOME/Library/LaunchAgents/ and load it with:
launchctl load -w $HOME/Library/LaunchAgents/pbcopy.plist
3. remote: add vimrc to your remote server's vim config file ~/.vimrc
4. remote: copy pbcopy to your remote server and make it excutable
chmod +x /usr/local/bin/pbcopy
5. remote: run vim and yank
6. local: paste the yanked content
#!/bin/bash
if [ $# -eq 1 ]; then
cat $1 | nc localhost 22222
else
[[ -p /dev/stdin ]] && { cat | nc localhost 22222; }
fi
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>Label</key>
<string>localhost.pbcopy</string>
<key>ProgramArguments</key>
<array>
<string>/usr/bin/pbcopy</string>
</array>
<key>inetdCompatibility</key>
<dict>
<key>Wait</key>
<false/>
</dict>
<key>Sockets</key>
<dict>
<key>Listeners</key>
<dict>
<key>SockServiceName</key>
<string>22222</string>
<key>SockNodeName</key>
<string>127.0.0.1</string>
</dict>
</dict>
</dict>
</plist>
Host *
RemoteForward 22222 127.0.0.1:22222
let s:selfpid = getpid()
function! s:pbcopy()
let l:path = printf("/tmp/%d.pbcopy", s:selfpid)
let l:command = printf("/usr/local/bin/pbcopy %s", l:path)
let l:content = split(getreg('"'), "\n", 1)
call writefile(l:content, l:path)
call system(l:command)
endfunction
function! s:cleanup()
let l:path = printf("/tmp/%d.pbcopy", s:selfpid)
let l:command = printf("/bin/rm -f %s", l:path)
call system(l:command)
endfunction
augroup pbcopy
au!
au TextYankPost * if v:event.operator ==# 'y' | call s:pbcopy() | endif
au VimLeave * call s:cleanup()
augroup END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment