• Stars
    star
    335
  • Rank 125,904 (Top 3 %)
  • Language
    Lua
  • License
    BSD 2-Clause "Sim...
  • Created over 2 years ago
  • Updated 6 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A Neovim plugin to copy text through SSH with OSC52

nvim-osc52

A 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 Neovim'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 your terminal must support OSC52 which is the case for most modern terminal emulators.

nvim-osc52 is a rewrite of vim-oscyank in Lua.

Installation

With packer.nvim for instance:

use {'ojroques/nvim-osc52'}

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.

Usage

Add this to your config (assuming Neovim 0.7+):

vim.keymap.set('n', '<leader>c', require('osc52').copy_operator, {expr = true})
vim.keymap.set('n', '<leader>cc', '<leader>c_', {remap = true})
vim.keymap.set('v', '<leader>c', require('osc52').copy_visual)

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.

Configuration

The available options with their default values are:

require('osc52').setup {
  max_length = 0,      -- Maximum length of selection (0 for no limit)
  silent     = false,  -- Disable message on successful copy
  trim       = false,  -- Trim surrounding whitespaces before copy
}

Advanced usage

The following methods are also available:

  • require('osc52').copy(text): copy text text
  • require('osc52').copy_register(register): copy text from register register

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

function copy()
  if vim.v.event.operator == 'y' and vim.v.event.regname == '+' then
    require('osc52').copy_register('+')
  end
end

vim.api.nvim_create_autocmd('TextYankPost', {callback = copy})

Using nvim-osc52 as clipboard provider

You can use the plugin as your clipboard provider, see :h provider-clipboard for more details. Simply add these lines to your config:

local function copy(lines, _)
  require('osc52').copy(table.concat(lines, '\n'))
end

local function paste()
  return {vim.fn.split(vim.fn.getreg(''), '\n'), vim.fn.getregtype('')}
end

vim.g.clipboard = {
  name = 'osc52',
  copy = {['+'] = copy, ['*'] = copy},
  paste = {['+'] = paste, ['*'] = paste},
}

-- Now the '+' register will copy to system clipboard using OSC52
vim.keymap.set('n', '<leader>c', '"+y')
vim.keymap.set('n', '<leader>cc', '"+yy')

Note that if you set your clipboard provider like the example above, copying text from outside Neovim and pasting with p won't work. But you can still use the paste shortcut of your terminal emulator (usually ctrl+shift+v).