osx - How can I copy into the Mac clipboard from vim?

06
2014-04
  • Reefersleep

    I can paste from vim in one terminal window to vim in another, but not to anything outside vim. I've read numerous posts on the subject, and nothing I've found has helped. Copy/pasting from MacVim works, but I prefer using VIM.

    I'm using OSX 10.7.5 and Vim 7.4, installed via MacPorts.

    vim --version 
    

    shows +clipboard, +x11, +xterm_clipboard.

    I've tried "+y, "*y combined with both visual selection and range selection.

    In my ~/.vimrc , I've got

    set clipboard=unnamedplus,unnamed,autoselect
    

    EDIT:

    Solution found! Now, y, yy and so on work perfectly! Whatever I yank in Vim, I can paste outside, and whatever I command-c outside, I can p in Vim.

    The solution: As per FDinof's suggestion, I reinstalled via MacPorts WITHOUT x11, instead just doing

    sudo -v port install vim +huge

    For the record, I still have

    set clipboard=unnamedplus,unnamed,autoselect

    in my ~/.vimrc

    Thank you for your help!

  • Answers
  • FDinoff

    x11 isn't needed for copying to the clipboard since mac doesn't use x11. Recompile vim without x11. My guess is that you are copying to the x11 clipboard which you don't know how to access from the mac side.

    In macports using the huge variant is sufficient to get clipboard support working.

    port install vim +huge
    
  • Antony

    From memory in Vim you can use pbcopy and pbpaste the same way as any other external command. To copy the current line to the clipboard type:

    :.!pbcopy
    

    to copy lines 1 to 50

    :1,50!pbcopy
    

    To copy the contents of the clipboard into the current vim bufer use:

    :r !pbpaste
    

  • Related Question

    osx - Sharing the Mac OS X clipboard with the internal X server
  • kwutchak

    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.


  • Related Answers
  • jtb

    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.

  • user47498

    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'