Code Monkey home page Code Monkey logo

nvim-osc52's Introduction

nvim-osc52

Note: As of Neovim 10.0 (specifically since this PR), native support for OSC52 has been added and therefore this plugin is now obsolete. Check :h clipboard-osc52 for more details.

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'}

Configuration for tmux

If you are using tmux, run these steps first: enabling OSC52 in tmux.

Then, you can use the tmux option set-clipboard on or allow-passthrough on.

For tmux versions before 3.3a, you will need to use the set-clipboard option: set -s set-clipboard on

For tmux versions starting with 3.3a, you can configure tmux to allow passthrough of escape sequences (set -g allow-passthrough on). With this option you can leave set-clipboard to its default (external). The allow-passthrough option works well for nested tmux sessions or when running tmux on both the local and remote servers. When using allow-passthrough, be sure to enable tmux_passthrough for this plugin.

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
  tmux_passthrough = false, -- Use tmux passthrough (requires tmux: set -g allow-passthrough on)
}

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

nvim-osc52's People

Contributors

kmac avatar lvxnull avatar ojroques avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.