Sharing the Mac OS X clipboard with the internal X server

21

9

I'd like to be able to copy and paste between native (Cocoa?) MacOS X apps and those running under Mac's internal X server.

Currently I can copy and paste independently. If I select text in an xterm window I can paste it back in any other X based window. Similarly, copying information outside X can be pasted into applications which are also outside X.

As an example, if I (in exactly this order):

  • Copy (select using the mouse) "Copy using X selection" in an xterm window
  • Copy (Edit | Copy) "Text in Firefox" in Firefox
  • Paste (Shift Insert xmodmap) into nano I get "Copy using X selection"
  • Paste (Edit | Paste) here I get -> Text in Firefox
  • Paste in MacVIM (Edit | Paste) I get "Text in Firefox"
  • Paste into /usr/bin/vim (non graphical) I get "Copy using X selection"

How do I enable "clipboard sharing" between X and Cocoa?

The xmodmap I am using to paste in X is:

xmodmap -e "keycode 84 = Insert"

where 84 is the numeric keypad's Enter key. So Shift Enter gives me paste.

kwutchak

Posted 2009-08-03T06:30:54.273

Reputation: 397

Is this consistent across X applications? Are you trying it in plain xterm or similar, not vim or something with its own buffers? Which version of OS X are you running? My clipboard is shared across Cocoa and X11 as expected in 10.5.7. – jtb – 2009-08-03T06:40:01.910

Hey jtb, thanks for your help. I use vim and am familiar with the issues around using it's internal buffers. I normally cheat and switch into edit mode then paste from gvim (or MacVim's) edit menu. I've added some extra information to my post to clarify the other parts of your question. – kwutchak – 2009-08-03T06:59:35.483

Ah, thanks for the extra info. I'm seeing mostly what you describe although the paste in step 3 works (using middle click, I don't have Insert mapped). Command-C does seem to copy into the native clipboard if I have text selected in X11, but I see what you mean that using the native X copy/paste operations it's not properly synchronized. – jtb – 2009-08-03T07:24:42.520

For recent version of OSX (I use 10.12), the following works: https://stackoverflow.com/a/9525347/1353267

– Samveen – 2018-06-16T12:26:50.647

Answers

14

You can enable clipboard sync by editing ~/Library/Preferences/org.x.X11.plist and adding the following five boolean keys1 (and checking them).

sync_clipboard_to_pasteboard
sync_pasteboard
sync_pasteboard_to_clipboard
sync_pasteboard_to_primary
sync_primary_on_select

Restart X11 and they should be synchronized properly.

Edit: You can add the keys either with the Property List Editor or with the following Terminal commands:

defaults write org.x.X11 sync_clipboard_to_pasteboard -boolean true
defaults write org.x.X11 sync_pasteboard -boolean true
defaults write org.x.X11 sync_pasteboard_to_clipboard -boolean true
defaults write org.x.X11 sync_pasteboard_to_primary -boolean true
defaults write org.x.X11 sync_primary_on_select -boolean true

An alternative solution is to install the latest version of XQuartz which exposes these options through the X11 Preferences >> Pasteboard panel.

jtb

Posted 2009-08-03T06:30:54.273

Reputation: 2 115

This line typed on my xterm and copied across... ;) This line successfully pasted back into my xterm... ;)

Another case where my wife wouldn't understand how extremely satisfying this fix is!!

Thank you very, very much. – kwutchak – 2009-08-03T07:45:11.737

Thanks for asking. The Mac has me in the habit of hitting Cmd+C to the extent that I didn't realize what I was missing. But I'm quite glad to have this in my config now. – jtb – 2009-08-03T07:49:26.627

I have installed XQuarts and changed the options for it. When I ssh to a remote system with XForwarding enabled (on both systems) and add things to the clipboard using xclip (eg: ls | xclip) the clipboard on my local system (mac os x) does not change. What am I missing? – cwd – 2012-03-29T14:10:35.167

4I think the correct file to write to now is org.macosforge.xquartz.X11 – Keith Smiley – 2014-03-31T20:58:19.373

2

You can also recompile vim-7.3 and add the following to your .vimrc:

set clipboard=unnamed

Then you'll be able to do 'yy' in vim and paste in Cocoa with Cmd+V. And vice versa: copy with Cmd+V in Cocoa and paste in vim with 'p'

user47498

Posted 2009-08-03T06:30:54.273

Reputation: 131

notice that your vim has to be compiled with the clipboard option on in order for this to work. You can see if it is by executing vim --version | grep clipboard. If it says +clipboard, then you are good. If it says -clipboard, this will not work. – egarcia – 2013-08-22T09:50:10.167