/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 from anywhere using the ANSI OSC52 sequence.

When this sequence is emitted by Vim, the terminal will copy the given text into the system clipboard. This is totally location independent, users can copy 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 status of popular terminal emulators regarding OSC52 (as of May 2021):

Terminal OSC52 support
Alacritty yes
GNOME Terminal (and other VTE-based terminals) not yet
hterm (Chromebook) yes
iTerm2 yes
kitty yes
screen yes
tmux yes
Windows Terminal yes
rxvt yes (to be confirmed)
urxvt yes (with a script, see here)

Installation

With vim-plug:

Plug 'ojroques/vim-oscyank'

Basic usage

Enter Visual mode, select your text and run :OSCYank.

You may want to map the command:

vnoremap <leader>c :OSCYank<CR>

You can also use the OSCYank operator:

nmap <leader>o <Plug>OSCYank

like so for instance:

<leader>oip  " copy the inner paragraph

(see this PR for more details)

Copying from a register

If you prefer to copy text from a particular register, use:

:OSCYankReg +  " this will copy text from register '+'

For the impatient one, copy this line to your config. Content will be copied to clipboard after any yank operation:

autocmd TextYankPost * if v:event.operator is 'y' && v:event.regname is '' | execute 'OSCYankReg "' | endif

Or to copy to clipboard the + register (vim's system clipboard register):

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

Configuration

By default you can copy up to 100000 characters at once. If your terminal supports it, you can raise that limit with:

let g:oscyank_max_length = 1000000

The plugin treats tmux, screen and kitty differently than other terminal emulators. The plugin should automatically detects the terminal used but you can bypass detection with:

let g:oscyank_term = 'tmux'  " or 'screen', 'kitty', 'default'

By default a confirmation message is echoed after text is copied. This can be disabled with:

let g:oscyank_silent = v:true  " or 1 for older versions of Vim

Features

There are already Vim plugins implementing OSC52. However this plugin fixes several issues I've had with them:

  • It supports Neovim.
  • It supports Windows.
  • It does not mandate users to overwrite their unnamed register (").
  • It makes the maximum length of strings configurable.
  • It supports kitty versions below 0.22.0 which implement a slightly modified OSC52 protocol.

Other terminals with OSC52 support

Other terminals that support OSC52:

Terminal OSC52 support
foot yes
wezterm yes

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

Credits

The code is derived from hterm's script.

hterm's LICENCE
LICENSE