doubleOK
doubleOK

Reputation: 353

How to use X11 forwarding to copy from vim to local machine

I swear I have read every single post and have not found a comprehensive solution that works for me yet. Here is what I have done so far:

  1. Configured vim to copy to system's clipboard. set clipboard=unnamedplus
  2. Enabled X11 forwarding on my remote machine.
  3. Installed xclip (https://defuse.ca/blog/clipboard-over-ssh-with-vim.html)
  4. Installed XQuartz on my local mac that I am using
  5. I am sshing into the remote Linux server as : ssh -X user@machine (XQuarz pops out)
  6. I am opening a file in vim. Yanking. CmndV into my local browser. Nothing happens.

HELP?

Upvotes: 4

Views: 4328

Answers (3)

It seems that this has been an ongoing issue with X11/XQuartz that has been described by many others (see here and here, for example).

I experienced the issue on macOS Big Sur v11.4 when connecting to a remote server running CentOS Linux v8. (In my case, I exclusively used ssh -Y to log in).

Here are a few observations I made as I was trying to resolve this.

  1. First, from the terminal on the remote server, piping anything to xclip (e.g. echo test | xclip) always resulted in the information being passed to my local mac clipboard. (I use the clipboard manager Copy 'Em which I love and which made troubleshooting easy by allowing me to visualize the contents of my clipboard along the way).

  2. In Vim/Neovim, after making a selection in Visual mode (e.g. by typing V in Normal mode), when I would type the recommended "+y to send the selection to the clipboard register, it wouldn't get synced with my local clipboard. However, as noted in the second link above, if I made any changes to the Pasteboard settings in XQuartz (not just the "Enable syncing" option), the information would get synced. To get around this, I created a Keyboard Maestro macro which I could easily run to toggle the Enable syncing option in a fraction of a second.

  3. Later on, I noticed that by sending the selected text to the * register in Vim/Neovim instead of to the + register, it would automatically sync with my local clipboard. Given that, I was able to do away with the Keyboard Maestro macro and simply adjusted the remaps in my vimrc accordingly.

Upvotes: 0

Jacob Adenbaum
Jacob Adenbaum

Reputation: 21

In case you're still having trouble (and to add to romainl's answer since this is something that stumped me for a while and it would be nice to see it documented somewhere that is easier to find), occasionally XQuartz clipboard forwarding seems to stop working unexpectedly for some users on OS X, even in El Capitan. If that happens to you, you can re-enable clipboard syncing by toggling the "enable syncing" settings:

  1. Open XQuartz.app
  2. In Preferences > Pasteboard, uncheck "enable syncing"
  3. Close preferences, open it again, and recheck "enable syncing"

I have absolutely no idea why this solves the problem, but it is documented as well here at freedesktop and on this old xquartz issue ticket .

Upvotes: 2

romainl
romainl

Reputation: 196596

From this gist:

On the Mac

  1. Install or update XQuartz.app and start it.

  2. In the Preferences window, activate clipboard synchronization.

  3. Quit XQuartz.app.

  4. In iTerm.app or Terminal.app, connect to your remote machine with:

    $ ssh -X username@host
    

    and see the XQuartz.app icon pop-up in your Dock.

From now on, XQuartz.app will start automatically in the background when you use the -X flag, taking care of the clipboard synchronization for you.

On the remote machine

  1. If you don't already have it, install GVim. On Debian-based systems, use:

    $ sudo apt-get install vim-gtk
    

    The idea is not to use Gvim but installing it gets you everything you need to get clipboard sharing to work:

    • a minimal X
    • a Vim built with clipboard support
  2. In Vim, synchronize the unnamed and clipboard registers by adding this line to ~/.vimrc:

    set clipboard^=unnamed
    

Upvotes: 4

Related Questions