/vim-oscyank

A Vim plugin to copy text through SSH with OSC52

Primary LanguageVim ScriptBSD 2-Clause "Simplified" LicenseBSD-2-Clause

vim-oscyank

A Vim / Neovim 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:

Terminal OSC52 support
alacritty yes
far2l yes
foot yes
gnome terminal (and other VTE-based terminals) not yet
hterm yes
iterm2 yes
kitty yes
konsole not yet
qterminal not yet
rxvt yes
st yes (but needs to be enabled, see here)
terminal.app no, but see workaround
tmux yes
urxvt yes (with a script, see here)
wezterm yes
windows terminal yes
xterm.js (Hyper terminal) not yet

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:

  • In normal mode, <leader>c is an operator that will copy the given text to the clipboard.
  • In normal mode, <leader>cc will copy the current line.
  • In visual mode, <leader>c will copy the current selection.

For Neovim 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:

  • :OSCYank(text): copy text text
  • :OSCYankRegister(register): copy text from register register

For instance, to automatically copy text that was yanked into register +:

autocmd TextYankPost *
    \ if v:event.operator is 'y' && v:event.regname is '+' |
    \ execute 'OSCYankRegister +' |
    \ endif