Skip to content

Latest commit

 

History

History
117 lines (101 loc) · 5.52 KB

README.md

File metadata and controls

117 lines (101 loc) · 5.52 KB

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. You can find at the end a non-exhaustive list of the state of OSC52 integration in popular terminal emulators.

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 in your Tmux config:

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

With 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.

Neovim 0.10+ natively supports OSC52 since this PR was merged. See :h clipboard-osc52 in Neovim. If you still wish to use this plugin, 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, 0 for unlimited length
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 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

Terminal Support

Terminal OSC52 support
alacritty yes
contour 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
zellij yes

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

You can also check can-i-use-terminal.github.io to see if your terminal supports OSC52.