remote-nvim.nvim icon indicating copy to clipboard operation
remote-nvim.nvim copied to clipboard

Yank/Paste Register Integrated with Clipboard

Open inhesrom opened this issue 1 year ago • 7 comments

While using this plugin, it would be really nice to be able to copy and paste from the remote nvim session to the host machine's clipboard and vice versa. This works will a normal nvim session, but as soon as I open a remote session I am unable to use the clipboard.

inhesrom avatar Jun 12 '24 18:06 inhesrom

I will have a look into this once I get some time but this should be possible and might be related to OSC52.

amitds1997 avatar Jun 19 '24 05:06 amitds1997

Until a proper solution is sorted, I've found a very hacky workaround which might work for you if your use case is similar. If you're using a devcontainer you can mount the X11 socket (/tmp/.X11-unix) and auth file ($HOME/.Xauthority) into your container and set the DISPLAY env variable to the same as the host. This allows xclip to share the contents between client & server nvim sessions (assuming you install xclip in the container).

"containerEnv": {
  "DISPLAY" : "${localEnv:DISPLAY}"
},
"mounts": [
  "source=/tmp/.X11-unix,target=/tmp/.X11-unix,type=bind,rslave",
  "source=/home/${localEnv:USER}/.Xauthority,target=/root/.Xauthority,type=bind,readonly",
]

NOTE: I also had to fiddle with the xclip clipboard settings in nvim a bit to only use "clipboard" selection for both "*" & "+" registers rather than "primary". Something about visual selection went a bit wrong sometimes and xclip display connection would randomly fail otherwise.

vim.g.clipboard = {
    name = "xclip",
    copy = {
        ["+"] = "xclip -i -selection clipboard",
        ["*"] = "xclip -i -selection clipboard",
    },
    paste = {
        ["+"] = "xclip -o -selection clipboard",
        ["*"] = "xclip -o -selection clipboard",
    },
    cache_enabled = false,
}

nickdedekind avatar Aug 01 '24 07:08 nickdedekind

To add more data to this, the following config is supposed to work using OSC 52:

if os.getenv 'SSH_CLIENT' ~= nil then
  local function noop()
    return {}
  end
  vim.g.clipboard = {
    name = 'OSC 52',
    copy = {
      ['+'] = require('vim.ui.clipboard.osc52').copy '+',
      ['*'] = require('vim.ui.clipboard.osc52').copy '*',
    },
    -- NOTE: OSC 52 paste is a security risk, so most terminals disable it
    paste = {
      ['+'] = noop,
      ['*'] = noop,
    },
  }
end

Inside the remote, a quick :lua =vim.g.clipboard or :checkhealth indicates that everything should be setup properly on the neovim side. To check that the terminal is configured to properly copy using OSC 52, I used the following command:

printf $'\e]52;c;%s\a' "$(base64 <<<'hello world')"

Which in my case does copy hello word into the system clipboard (WezTerm on MacOS ssh'd into Ubuntu).

OliverGuy avatar Sep 08 '24 07:09 OliverGuy

I've tried the above configuration when remotely connected to the EC2 instance, the clipboard OSC 52 health checks pass and OSC 52 in WezTerm is configured properly (no tmux, Linux Mint 21). But when I try "+yy, then do Ctrl+Shift+v in the insert mode, I'm still not able to paste what I've just yanked. Yanking to the system clipboard using OSC 52 does work when I run NeoVim locally but for some reason fails when doing it with the remote plugin. I've tried ConfigCleanup and starting all over again to be sure that everything is the same as in the local configuration but nothing helped. I also tried other configurations as suggested here but the result is the same. @OliverGuy, @amitds1997 , did it work for you or maybe I'm missing something?

PavloFesenko avatar Jan 15 '25 05:01 PavloFesenko

It unfortunately did not work within Neovim; the test I ran shows that the terminal emulator itself is capable of OSC 52 (it also works within tmux). Be aware that most terminal emulators (including WezTerm) will block OSC 52 pasting by default as it is a security risk.

OliverGuy avatar Jan 15 '25 08:01 OliverGuy

Apologies for the noise, but this issue on the nvim v0.11 roadmap seems related: https://github.com/neovim/neovim/issues/28792

OliverGuy avatar Jan 15 '25 08:01 OliverGuy

This will be very useful for users!!

JinraeKim avatar Sep 16 '25 21:09 JinraeKim