如何用OSC52打通遠端SSH到本地clipboard

Louie Wu
2 min readAug 7, 2024

--

長期作為VNC直連Linux desktop使用者,直接GUI桌面連線是很直覺且方便。但在win環境下SSH+VSCODE直連的便利性與低延遲相當吸引人。

這時剪貼簿clipboard就成為一個小麻煩,遠端SSH是沒有clipboard存在,只有VIM自己內建的clipboard,與本地Windows的clipboard不共通。

本篇分享將使用OSC52技術說明打通,增加便利性及純鍵盤操作,就可以不需要透過滑鼠來複製terminal上文字。另包含打通VIM剪貼簿及複製檔案路徑

English title: How to use OSC52 to connect remote SSH to local clipboard

As a long-time user of VNC for direct connections to Linux desktops, using a GUI desktop connection is very intuitive. In a Windows environment, the convenience and low latency of SSH + VSCode direct connections are quite attractive.

However, the clipboard becomes a small hassle in this scenario. Remote SSH does not have a clipboard, only VIM’s built-in clipboard, which is not shared with the local Windows clipboard.

This guide will explain how to use OSC52 technology to bridge this gap, increasing convenience and enabling pure keyboard operations, so you don’t need to use a mouse to copy text from the terminal. It also includes instructions on bridging the VIM clipboard and copying file paths.

Prerequisite

Check your terminal is on OSC52 Supported Terminals https://github.com/jirutka/tty-copy?tab=readme-ov-file#supported-terminals

My Environment: Window 11 + windows terminal Version: 1.20

Copy anything from linux to windows

Setup:

Create file ~/bin/yank_sspai


#!/bin/sh

# copy via OSC 52
buf=$( cat "$@" )
len=$( printf %s "$buf" | wc -c ) max=74994
test $len -gt $max && echo "$0: input is $(( len - max )) bytes too long" >&2
printf "\033]52;c;$( printf %s "$buf" | head -c $max | base64 | tr -d '\r\n' )\a"

Usage:

  1. In linux teriminal

$ echo “abcdef” | ~/bin/yank_sspai

2. In windows, “ctrl+v” to paste anywhere you like

Copy current pwd or file path (Optional)

Setup:

Add below alias in ~/.cshrc

alias pwds 'pwd | tee /dev/tty | ~/bin/yank_sspai'
alias pwdf 'realpath \!:1 | tee /dev/tty | ~/bin/yank_sspai'

Usage:

  1. In linux teriminal$ pwds
  2. In linux teriminal$ pwdf test.txt

Copy (Yank) in Vim

Setup:

  1. Install vim plugin ‘ojroques/vim-oscyank’
    https://github.com/ojroques/vim-oscyank

I’ve chosen “vim-plug” as my plugin manager for vim, and I’ve simply added the following lines to my ~/.vimrc

call plug#begin()
Plug 'ojroques/vim-oscyank', {'branch': 'main'}
call plug#end()

2. add below in your ~/.vimrc

"--------------------------------------------
" Plugin OSCYank configuration
"--------------------------------------------
if (!has('nvim') && !has('clipboard_working'))
" In the event that the clipboard isn't working, it's quite likely that
" the + and * registers will not be distinct from the unnamed register. In
" this case, a:event.regname will always be '' (empty string). However, it
" can be the case that `has('clipboard_working')` is false, yet `+` is
" still distinct, so we want to check them all.
let s:VimOSCYankPostRegisters = ['', '+', '*']
function! s:VimOSCYankPostCallback(event)
if a:event.operator == 'y' && index(s:VimOSCYankPostRegisters, a:event.regname) != -1
call OSCYankRegister(a:event.regname)
endif
endfunction
augroup VimOSCYankPost
autocmd!
autocmd TextYankPost * call s:VimOSCYankPostCallback(v:event)
augroup END
endif

Usage:

Just use “y” as you daily use

Copy current file path in vim (Optional)

Setup:

  1. add below in you ~/.vimrc
function! Cppath()
let @" = expand("%:p")
silent! call OSCYankRegister('') "silent copy the reg to win clipboard with the help form oscyank
echo expand("%:p")
endfunction

Usage:

In vim

:call Cppath()

--

--

No responses yet