Home

Awesome

vim-oscyank

A Vim plugin to copy text to the system clipboard using the ANSI OSC52 sequence.

The plugin wraps a piece of text inside an OSC52 sequence and writes it to Vim's stderr. When your terminal detects the OSC52 sequence, it will copy the text into the system clipboard.

This is totally location-independent, you can copy text from anywhere including from remote SSH sessions. The only requirement is that the terminal must support the sequence. Here is a non-exhaustive list of the state of OSC52 integration in popular terminal emulators:

TerminalOSC52 support
alacrittyyes
contouryes
far2lyes
footyes
gnome terminal (and other VTE-based terminals)not yet
htermyes
iterm2yes
kittyyes
konsolenot yet
qterminalnot yet
rxvtyes
styes (but needs to be enabled, see here)
terminal.appno, but see workaround
tmuxyes
urxvtyes (with a script, see here)
weztermyes
windows terminalyes
xterm.js (Hyper terminal)not yet
zellijyes

Feel free to add terminals to this list by submitting a pull request.

Installation

With vim-plug for instance:

Plug 'ojroques/vim-oscyank', {'branch': 'main'}

If you are using tmux, run these steps first: enabling OSC52 in tmux. Then make sure set-clipboard is set to on: set -s set-clipboard on. See :h oscyank-tmux for more details.

Usage

Add this to your Vim config:

nmap <leader>c <Plug>OSCYankOperator
nmap <leader>cc <leader>c_
vmap <leader>c <Plug>OSCYankVisual

Using these mappings:

For Neovim, this plugin shouldn't be necessary anymore, since Neovim contains native OSC 52 support since Neovim 10.0. For older versions of Neovim, this plugin also works. However, you should instead check out nvim-osc52. Or add this to your Neovim config:

vim.keymap.set('n', '<leader>c', '<Plug>OSCYankOperator')
vim.keymap.set('n', '<leader>cc', '<leader>c_', {remap = true})
vim.keymap.set('v', '<leader>c', '<Plug>OSCYankVisual')

Configuration

The available options with their default values are:

let g:oscyank_max_length = 0  " maximum length of a selection
let g:oscyank_silent     = 0  " disable message on successful copy
let g:oscyank_trim       = 0  " trim surrounding whitespaces before copy
let g:oscyank_osc52      = "\x1b]52;c;%s\x07"  " the OSC52 format string to use

See :h oscyank-config for more details.

Advanced Usage

The following commands are also available:

For instance, to automatically copy text that was yanked into the unnamed register (") as well as + and " when the clipboard isn't working:

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