Skip to content

Instantly share code, notes, and snippets.

@l1n
Created December 26, 2024 21:35
Show Gist options
  • Save l1n/52691c3a7450ee132133678fe2ca8fcb to your computer and use it in GitHub Desktop.
Save l1n/52691c3a7450ee132133678fe2ca8fcb to your computer and use it in GitHub Desktop.
OSC52 Vim support (for copying from vim via the * buffer over SSH or similar)
function! SendViaOSC52(str) abort
let b64 = system('printf %s ' . shellescape(a:str) . ' | base64 -w0')
let osc = '\033]52;c;' . b64 . '\007'
call system('printf ' . shellescape(osc) . ' > /dev/tty')
endfunction
function! CopyStarRegisterOSC52() range
" Get the selected text
let saved_reg = @"
silent normal! gvy
let selection = @"
let @" = saved_reg
" Send the selection
call SendViaOSC52(selection)
endfunction
" Visual mode mapping
vnoremap <silent> y* :<C-U>call CopyStarRegisterOSC52()<CR>
" Command mode mapping for :'<,'>y*
command! -range Ystar <line1>,<line2>call CopyStarRegisterOSC52()
cnoreabbrev y* Ystar
" Optional: Normal mode operator
nnoremap <silent> y* :set opfunc=CopyStarRegisterOSC52<CR>g@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment