Last active
August 22, 2020 14:41
-
-
Save greymd/66affaf1b659add82d120cffb03c2b35 to your computer and use it in GitHub Desktop.
OSC52 copy with Vim
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
" Clipboard integration by Operating System Controls from Vim | |
" Usage: | |
" Yank something and run :OscCopy | |
" or | |
" Copy range in visual mode and run :'<,'>OscCopyRange | |
" From: https://stackoverflow.com/questions/1533565/how-to-get-visually-selected-text-in-vimscript | |
function! GetVisualSelection() | |
let [lnum1, col1] = getpos("'<")[1:2] | |
let [lnum2, col2] = getpos("'>")[1:2] | |
let lines = getline(lnum1, lnum2) | |
if lnum1 == 0 && lnum2 == 0 && col1 == 0 && col2 == 0 | |
return '' | |
endif | |
let lines[-1] = lines[-1][:col2 - (&selection == 'inclusive' ? 1 : 2)] | |
let lines[0] = lines[0][col1 - 1:] | |
return join(lines, "\n") | |
endfunction | |
function! OscCopyRange() | |
let text = GetVisualSelection() | |
let encodedText=text | |
let encodedText=substitute(text, '\', '\\\\', "g") | |
let encodedText=substitute(encodedText, "'", "'\\\\''", "g") | |
let executeCmd="echo -n '".encodedText."' | base64 | tr -d '\\n'" | |
let encodedText=system(executeCmd) | |
if $TMUX != "" | |
"tmux | |
let executeCmd='echo -en "\x1bPtmux;\x1b\x1b]52;;'.encodedText.'\x1b\x1b\\\\\x1b\\" > /dev/tty' | |
else | |
let executeCmd='echo -en "\x1b]52;;'.encodedText.'\x1b\\" > /dev/tty' | |
endif | |
call system(executeCmd) | |
redraw! | |
endfunction | |
function! OscCopy() | |
let encodedText=@" | |
let encodedText=substitute(encodedText, '\', '\\\\', "g") | |
let encodedText=substitute(encodedText, "'", "'\\\\''", "g") | |
let executeCmd="echo -n '".encodedText."' | base64 | tr -d '\\n'" | |
let encodedText=system(executeCmd) | |
if $TMUX != "" | |
"tmux | |
let executeCmd='echo -en "\x1bPtmux;\x1b\x1b]52;;'.encodedText.'\x1b\x1b\\\\\x1b\\" > /dev/tty' | |
else | |
let executeCmd='echo -en "\x1b]52;;'.encodedText.'\x1b\\" > /dev/tty' | |
endif | |
call system(executeCmd) | |
redraw! | |
endfunction | |
command! -range OscCopyRange :call OscCopyRange() | |
command! OscCopy :call OscCopy() |
I tried this and found that it did not give high fidelity copying, and would often just not work
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
plugin: https://github.com/greymd/oscyank.vim