tags:

views:

2396

answers:

11

There must be a really simple solution here that I'm missing:

Say you've got an area selected in vim. How can you copy it into the OS X clipboard?

(Hint: the OS X clipboard can be written to via pipe to /usr/bin/pbcopy)

+2  A: 

double-quote asterisk ("*) before any yank command will yank the results into the copy buffer. That works for Windows and Linux too.

Paul Tomblin
just seems to emit a bell character and no other effect for me
ʞɔıu
In X11, Vim's "* is PRIMARY, "+ is CLIPBOARD, and SECONDARY doesn't get a named register. (Not that anybody uses it...)
ephemient
A: 

command-C? This at least works for the vim launched from within Terminal.app for text selected with mouse.

mouviciel
good naive answer but breaks output if you're using vertically-split subwindows in vim (which is actually the case I have in mind), or if the result is longer than one terminal screen
ʞɔıu
You are right, I didn't think of these cases. I leave my answer just to prevent others to give similar not helpful answers.
mouviciel
+5  A: 

Depending on which version of vim I use, I'm able to use the + register to access the clipboard.

http://vim.wikia.com/wiki/Mac_OS_X_clipboard_sharing may have some ideas that work for you as well.

Chris AtLee
+ is only for X11?
ʞɔıu
A: 

You can use MacVim when you're on a Mac to easily access the clipboard using the standard OS keys.

It's also fully backward compatible with normal Vim, so I don't even have to have a separate .vimrc.

sirlancelot
+6  A: 

If the clipboard is enabled, you can copy a selected region to the clipboard by hitting "*y

To see if it is enabled, o a vim --version and look for +clipboard or -clipboard. For example, it's not enabled by default on my 10.5.6 box:

% which vim
/usr/bin/vim
% vim --version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Nov 11 2008 17:20:43)
Included patches: 1-22
Compiled by [email protected]
Normal version without GUI.  Features included (+) or not (-):
...
-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
...

If it had been compiled with +clipboard, I'd be able to use the "* register to access the system clipboard.

I downloaded the 7.2 source and compiled it (easy as tar xjf vim-7.2.tar.bz && cd vim72 && ./configure && make && sudo make install), and the clipboard was enabled:

% which vim
/usr/local/bin/vim
% vim --version
VIM - Vi IMproved 7.2 (2008 Aug 9, compiled Mar 24 2009 17:31:52)
Compiled by [email protected]
Normal version with GTK2 GUI.  Features included (+) or not (-):
...
+clientserver +clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments
...

However, even after compiling, I couldn't copy to the clipboard when running vim in Terminal.app, only in X11.app.

rampion
This answer is pure gold, I wish I could upvote it more than once!
nfm
I compiled the latest version with +clipboard and now the native clipboard works with it as the * register. Also, setting "set clipboard=unnamed" in your .vimrc file makes the system clipboard the typical one. I actually used http://github.com/adamv/homebrew/blob/duplicates/Library/Formula/vim.rb this homebrew formula to build and compile it.
rado
+5  A: 

For MacVim and Windows Gvim, simply add the following to your ~/.vimrc:

set clipboard=unnamed

Now all operations such as yy, D, and P work with the clipboard. No need to prefix them with "* or "+.

George V. Reilly
This is a great tip. I've been looking all over for it.
andrewj
+1  A: 

For Ubuntu users, the package you want to retrieve for using the clipboard is vim-full. The other packages (vim-tiny, vim) do not include the clipboard feature.

+1  A: 

Fakeclip implements the + and * buffers if they aren't natively supported.

redacted
+1  A: 

If you are using MacPorts you can upgrade your VIM to include clipboard support via:

port install vim +x +x11

Now you use the "+ register to yank your text directly to your Mac clipboard. Works like a charm.

Matt Hughes
A: 

This is what I'm struggling with:

  1. Open a local terminal.
  2. SSH to another machine.
  3. Open a file with vim, or vimx
  4. enter visual selection mode and select some text
  5. Now I want to copy this to my system clipboard...can't seem to do this.

I've tried "+y "*y and set clipboard=unnamed

None of this seems to copy the vim visual blocked text to my system clipboard of any kind...

This would very helpful - but even more so when using vimdiff, where a regular mouse-based selection will not work - or if you have line numbers showing in vim.

thanks.

Dave
A: 

Command-c works for me in both MacVim and in the terminal.

Matt Williamson