Skip to content

Instantly share code, notes, and snippets.

@romainl
Last active September 27, 2022 13:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save romainl/96180b5b8e7722a7428c to your computer and use it in GitHub Desktop.
Save romainl/96180b5b8e7722a7428c to your computer and use it in GitHub Desktop.
Clipboard sharing on Mac OS X is easy

Clipboard sharing on Mac OS X is easy

On the Mac

  1. Make sure you have a clipboard-aware Vim build. I recommend MacVim.

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

  3. In the Preferences window, activate clipboard synchronization.

  4. Quit XQuartz.app.

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

My Vim-related gists.

@romainl
Copy link
Author

romainl commented Aug 13, 2020

Hmm… it worked much better when I wrote that gist.

I just tried on a fresh install of Ubuntu Server 20.04 and…

  • I can paste text from remote Vim into any local app (Vim, TextEdit, Chrome, etc.) via "* (CLIPBOARD),
  • I can paste text from remote Vim into any local app via "+ (PRIMARY),
  • I can't paste text from local pasteboard to remote Vim at all, either via "+ or "*.

This unidirectionality is troubling.

For some reason that I have yet to investigate, installing the package x11-apps on the remote seems to have fixed sharing via CLIPBOARD, which is now fully bidirectional.

I will see if I can manage to have full bidirectionality for CLIPBOARD and PRIMARY.

@MagnusBrzenk
Copy link

After a TON of trial and error, I got it to work on both Ubuntu Server 18.04 and 20.04. I tried so many things that I eventually lost track of what the winning combo was :( One thing to note is that it worked after I uninstalled XQuartz via homebrew and reinstalled from the downloaded disk image though I'd be surprised if that was the key. I also restarted my computer at some point after that. For reference, here are the salient versions/packages that I had installed when remote-to-local copy/paste started working though, again, I don't know what the winning combo was:

cat /etc/os-release

NAME="Ubuntu"
VERSION="18.04.5 LTS (Bionic Beaver)"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu 18.04.5 LTS"
VERSION_ID="18.04"
HOME_URL="https://www.ubuntu.com/"
SUPPORT_URL="https://help.ubuntu.com/"
BUG_REPORT_URL="https://bugs.launchpad.net/ubuntu/"
PRIVACY_POLICY_URL="https://www.ubuntu.com/legal/terms-and-policies/privacy-policy"
VERSION_CODENAME=bionic
UBUNTU_CODENAME=bionic

apt list --installed | grep vim

vim/bionic-updates,bionic-security,now 2:8.0.1453-1ubuntu1.3 amd64 [installed]
vim-common/bionic-updates,bionic-security,now 2:8.0.1453-1ubuntu1.3 all [installed]
vim-gnome/bionic-updates,bionic-security,now 2:8.0.1453-1ubuntu1.3 all [installed]
vim-gtk/bionic-updates,bionic-security,now 2:8.0.1453-1ubuntu1.3 amd64 [installed]
vim-gtk3/bionic-updates,bionic-security,now 2:8.0.1453-1ubuntu1.3 amd64 [installed]
vim-gui-common/bionic-updates,bionic-security,now 2:8.0.1453-1ubuntu1.3 all [installed,automatic]
vim-runtime/bionic-updates,bionic-security,now 2:8.0.1453-1ubuntu1.3 all [installed]
vim-tiny/bionic-updates,bionic-security,now 2:8.0.1453-1ubuntu1.3 amd64 [installed]

apt list --installed | grep x11

libx11-6/bionic-updates,now 2:1.6.4-3ubuntu0.2 amd64 [installed]
libx11-data/bionic-updates,now 2:1.6.4-3ubuntu0.2 all [installed]
libx11-dev/bionic-updates,now 2:1.6.4-3ubuntu0.2 amd64 [installed]
libx11-doc/bionic-updates,now 2:1.6.4-3ubuntu0.2 all [installed,automatic]
libx11-xcb1/bionic-updates,now 2:1.6.4-3ubuntu0.2 amd64 [installed,automatic]
x11-apps/bionic,now 7.7+6ubuntu1 amd64 [installed]
x11-common/bionic-updates,now 1:7.7+19ubuntu7.1 all [installed]
x11-utils/bionic,now 7.7+3build1 amd64 [installed]
x11proto-core-dev/bionic,now 2018.4-4 all [installed,automatic]
x11proto-dev/bionic,now 2018.4-4 all [installed,automatic]
x11proto-fixes-dev/bionic,now 1:2018.4-4 all [installed,automatic]
x11proto-input-dev/bionic,now 2018.4-4 all [installed,automatic]
x11proto-record-dev/bionic,now 2018.4-4 all [installed,automatic]
x11proto-xext-dev/bionic,now 2018.4-4 all [installed,automatic]

/usr/bin/vim --version 

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Mar 18 2020 18:29:15)
Included patches: 1-1453
Modified by pkg-vim-maintainers@lists.alioth.debian.org
Compiled by pkg-vim-maintainers@lists.alioth.debian.org
Huge version with GTK3 GUI.  Features included (+) or not (-):
+acl               +farsi             +mouse_sgr         -tag_any_white
+arabic            +file_in_path      -mouse_sysmouse    +tcl
+autocmd           +find_in_path      +mouse_urxvt       +termguicolors
-autoservername    +float             +mouse_xterm       +terminal
+balloon_eval      +folding           +multi_byte        +terminfo
+balloon_eval_term -footer            +multi_lang        +termresponse
+browse            +fork()            -mzscheme          +textobjects
++builtin_terms    +gettext           +netbeans_intg     +timers
+byte_offset       -hangul_input      +num64             +title
+channel           +iconv             +packages          +toolbar
+cindent           +insert_expand     +path_extra        +user_commands
+clientserver      +job               +perl              +vertsplit
+clipboard         +jumplist          +persistent_undo   +virtualedit
+cmdline_compl     +keymap            +postscript        +visual
+cmdline_hist      +lambda            +printer           +visualextra
+cmdline_info      +langmap           +profile           +viminfo
+comments          +libcall           -python            +vreplace
+conceal           +linebreak         +python3           +wildignore
+cryptv            +lispindent        +quickfix          +wildmenu
+cscope            +listcmds          +reltime           +windows
+cursorbind        +localmap          +rightleft         +writebackup
+cursorshape       +lua               -ruby              +X11
+dialog_con_gui    +menu              +scrollbind        -xfontset
+diff              +mksession         +signs             +xim
+digraphs          +modify_fname      +smartindent       +xpm
+dnd               +mouse             +startuptime       +xsmp_interact
-ebcdic            +mouseshape        +statusline        +xterm_clipboard
+emacs_tags        +mouse_dec         -sun_workshop      -xterm_save
+eval              +mouse_gpm         +syntax
+ex_extra          -mouse_jsbterm     +tag_binary
+extra_search      +mouse_netterm     +tag_old_static
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
 2nd user vimrc file: "~/.vim/vimrc"
      user exrc file: "$HOME/.exrc"
  system gvimrc file: "$VIM/gvimrc"
    user gvimrc file: "$HOME/.gvimrc"
2nd user gvimrc file: "~/.vim/gvimrc"
       defaults file: "$VIMRUNTIME/defaults.vim"
    system menu file: "$VIMRUNTIME/menu.vim"
  fall-back for $VIM: "/usr/share/vim"
...

... and I use the following in the .vimrc file:

...
    noremap y "*y:let @+=@*<CR>
    noremap yw "*yw:let @+=@*<CR>
    noremap yiw "*yiw:let @+=@*<CR>
    noremap yy "*yy:let @+=@*<CR>
...

I'll try and narrow down the exact combo of requirements in the future.

@romainl I find I can paste stuff copied from local into the remote vim by just going into insert mode and using cmd+v.

@YanzhaoW
Copy link

@MagnusBrzenk Does it still work now? In my case, it works after I reinstall XQuartz. But after I exit server and close XQuartz, it doesn't work again next time I log into the server.

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