Skip to content

Instantly share code, notes, and snippets.

@tarruda
Last active April 28, 2023 05:39
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tarruda/42f5236449857278f911 to your computer and use it in GitHub Desktop.
Save tarruda/42f5236449857278f911 to your computer and use it in GitHub Desktop.
Neovim clipboard plugin
import xerox
class NvimClipboard(object):
def __init__(self, vim):
self.provides = ['clipboard']
def clipboard_get(self):
return xerox.paste().split('\n')
def clipboard_set(self, lines):
xerox.copy(u'\n'.join([line.decode('utf-8') for line in lines]))
@jwmu
Copy link

jwmu commented Sep 10, 2014

It panic when I try to copy Chinese in neovim.It is due to line.decode('utf-8') received str which actually is unicode.I add chardet,and it seems work

import xerox
import chardet

class NvimClipboard(object):
    def __init__(self, vim):
        self.provides = ['clipboard_get', 'clipboard_set']

    def clipboard_get(self):
        return xerox.paste().split('\n')

    def clipboard_set(self, lines):
        new_lines = []
        for line in lines:
            det = chardet.detect(line)
            if det["encoding"]== "utf-8":
                new_line = unicode(line, "utf-8")
                new_lines.append(new_line)
            else:
                new_lines.append(line)
        xerox.copy('\n'.join(new_lines))

@tarruda
Copy link
Author

tarruda commented Sep 10, 2014

@jwmou thanks!

@tarruda
Copy link
Author

tarruda commented Sep 10, 2014

@jwmou Unless you configured differently, nvim should store strings as utf-8 internally so there should be no need for chardet(if needed a plugin can use vim.options['encoding'] to know the encoding beforehand)

Could you try the new version again to see if it works(requires this branch)? An alternative is to change xerox.copy('\n'.join([line.decode('utf-8') for line in lines])) to `xerox.copy(u'\n'.join([line.decode('utf-8') for line in lines]))

@jwmu
Copy link

jwmu commented Sep 17, 2014

You are right.It works

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment