ʞɔıu
ʞɔıu

Reputation: 48446

How to copy a selection to the OS X clipboard

I have an area selected in Vim. How can I copy it into the OS X clipboard?

(The OS X clipboard can be written to via a pipe to /usr/bin/pbcopy)

Upvotes: 236

Views: 144105

Answers (28)

Micah Rothenbuhler
Micah Rothenbuhler

Reputation: 17

Best way to to use mapping.

For example:

nnoremap C "+y
vnoremap C "+y
nnoremap cC ^"+y$

This will map capital C to yank into the clipboard, without changing anything. Works with any vim.

Upvotes: 0

Andrés Hazard
Andrés Hazard

Reputation: 43

if you have the +clipboard option on your Vim installation (you can check with :version) and you are in visual mode you can do "+y This will yank the selection to the buffer + that is the clipboard.

I have added the following maps to my vimrc and it works fine.

vmap <leader>y "+y : With this I can do leader key follow by y to copy to the clipboard in visual mode.

nmap <leader>p "+p : With this I can do leader key follow by p to paste from the clipboard on normal mode.

PD : On Ubuntu I had to install vim-gtk to get the +clipboard option.

Upvotes: 2

frbl
frbl

Reputation: 1292

My main problem was the default OSX version of Vim. Installing it via homebrew added +clipboard to the build.

You can check with:

vim --version

to see if it says -clipboard or +clipboard.

Upvotes: 2

redacted
redacted

Reputation: 2429

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

Upvotes: 2

user3133576
user3133576

Reputation: 51

I used:

map <C-x> :!pbcopy<CR>
vmap <C-c> :w !pbcopy<CR><CR>

It worked really well.

Upvotes: 4

songz
songz

Reputation: 2092

If your Vim is not compiled with clipboards, you wish to copy selected text instead of entire lines, you do not want to install MacVim or other GUI, the simplest solution is to add this line to your .vimrc:

map <C-c> y:e ~/clipsongzboard<CR>P:w !pbcopy<CR><CR>:bdelete!<CR>

To use it, simply visually select the text you want to copy, and then Control-C. If you want a full explanation of this line read "How to Copy to clipboard on vim".

Upvotes: 5

Matt Hughes
Matt Hughes

Reputation: 1458

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.

Upvotes: 11

rampion
rampion

Reputation: 89123

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

To see if it is enabled, run 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.

Upvotes: 139

Chris AtLee
Chris AtLee

Reputation: 8076

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

"Mac OS X clipboard sharing" may have some ideas that work for you as well.

Upvotes: 84

user518066
user518066

Reputation: 1437

Shift cmd c – set copy mode Drag mouse, select text, cmd c to copy selected text Cmd v to paste

Upvotes: 0

markroxor
markroxor

Reputation: 6506

For Mac - Holding option key followed by ctrl V while selecting the text did the trick.

Upvotes: 0

maged
maged

Reputation: 889

What worked for me in my .vimrc

set clipboard=unnamed
if has("unnamedplus") " X11 support
    set clipboard+=unnamedplus
endif

Upvotes: 1

Bruce
Bruce

Reputation: 2196

I meet the same issue, after install macvim still not working, finally I found a way to solve:

Try to uninstall all vim first,

brew uninstall macvim

brew uninstall --force vim

and reinstall macvim

brew install macvim --with-override-system-vim

Then you can use "*y or "+y, don't have to set clipboard=unnamed

Upvotes: -1

Telmo Dias
Telmo Dias

Reputation: 4178

In my case I just had to do :

:set mouse=v

please visit the original solution on superuser.com

Upvotes: 12

Paul Tomblin
Paul Tomblin

Reputation: 182880

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

Upvotes: 45

Jose Alban
Jose Alban

Reputation: 7956

If you are on MacOS X:

$ brew install vim
$ vim --version
VIM - Vi IMproved 7.4 [...]

Then, add to your .vimrc:

set clipboard=unnamed

Now you just need to be in vim and do :%y, to have all the content copied to your clipboard.

Upvotes: 9

Evan
Evan

Reputation: 7416

Use Homebrew's vim: brew install vim

Mac (as of 10.10.3 Yosemite) comes pre-installed with a system vim that does not have the clipboard flag enabled.

You can either compile vim for yourself and enable that flag, or simply use Homebrew's vim which is setup properly.

To see this for yourself, check out the stock system vim with /usr/bin/vim --version

You'll see something like:

$ /usr/bin/vim --version

VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Nov  5 2014 21:00:28)
Compiled by [email protected]
Normal version without GUI.  Features included (+) or not (-):
... -clientserver -clipboard +cmdline_compl ...

Note the -clipboard

With homebrew's vim you instead get

$ /usr/local/bin/vim --version

VIM - Vi IMproved 7.4 (2013 Aug 10, compiled May 10 2015 14:04:42)
MacOS X (unix) version
Included patches: 1-712
Compiled by Homebrew
Huge version without GUI.  Features included (+) or not (-):
... +clipboard ...

Note the +clipboard

Upvotes: 5

user242065
user242065

Reputation: 1223

You can visually select text and type :w !pbcopy<CR>

Or you can include the below key mappings in your ~/.vimrc file. They cut/copy text in visual mode to the operating system's clipboard.

vmap <C-x> :!pbcopy<CR>  
vmap <C-c> :w !pbcopy<CR><CR> 

source: http://drydevelopment.com/blog/vim-pbcopy-on-os-x

Upvotes: 101

George V. Reilly
George V. Reilly

Reputation: 16333

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 "+.

Upvotes: 265

Yaroslav Melnichuk
Yaroslav Melnichuk

Reputation: 850

I am currently on OS X 10.9 and my efforts to compile vim with +xterm_clipboard brought me nothing. So my current solution is to use MacVim in terminal mode with option set clipboard=unnamed in my ~/.vimrc file. Works perfect for me.

Upvotes: 0

tom
tom

Reputation: 7

on mac when anything else seems to work - select with mouse, right click choose copy. uff

Upvotes: -1

nikola
nikola

Reputation: 5386

On macos 10.8, vim is compiled with -clipboard so to use "*y you'll need to recompile. Luckily brew install vim would compile a new version easily for you and it will be +clipboard.

Upvotes: 27

VineetChirania
VineetChirania

Reputation: 969

Copying to clipboard using register '+' or '*' is not working?

Reason: Your particular version of vim was compiled without clipboard support.Type vim --verion on console and you will see -xterm_clipboard. Installing vim with gui packages solves this issue. On ubuntu you can do this by typing on shell:

sudo apt-get install vim-gui-common

Now again do vim --version on console. Most probably, you would be seeing +xterm_clipboard now!!

So, now you can copy anything to clipboard using register + (like "+yy to copy current line to clipboard)

Upvotes: -1

James Scriven
James Scriven

Reputation: 8154

Visually select the text and type:

ggVG
!tee >(pbcopy)

Which I find nicer than:

ggVG
:w !pbcopy

Since it doesn't flash up a prompt: "Press ENTER or type command to continue"

Upvotes: 18

Matt Williamson
Matt Williamson

Reputation: 40243

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

Upvotes: -2

plowman
plowman

Reputation:

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.

Upvotes: -1

matpie
matpie

Reputation: 17522

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.

Upvotes: 3

mouviciel
mouviciel

Reputation: 67879

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

Upvotes: 2

Related Questions