[macos] 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)

This question is related to macos vim clipboard

The answer is


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


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


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


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.


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.


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


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.


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


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


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"


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.


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


I used:

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

It worked really well.


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.


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)


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


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


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


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.


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.


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


In my case I just had to do :

:set mouse=v

please visit the original solution on superuser.com


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.


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.


What worked for me in my .vimrc

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

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


Examples related to macos

Problems with installation of Google App Engine SDK for php in OS X dyld: Library not loaded: /usr/local/opt/openssl/lib/libssl.1.0.0.dylib dyld: Library not loaded: /usr/local/opt/icu4c/lib/libicui18n.62.dylib error running php after installing node with brew on Mac Could not install packages due to an EnvironmentError: [Errno 13] How do I install Java on Mac OSX allowing version switching? Git is not working after macOS Update (xcrun: error: invalid active developer path (/Library/Developer/CommandLineTools) Can't compile C program on a Mac after upgrade to Mojave You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory. (mac user) How can I install a previous version of Python 3 in macOS using homebrew? Could not install packages due to a "Environment error :[error 13]: permission denied : 'usr/local/bin/f2py'"

Examples related to vim

Why does using from __future__ import print_function breaks Python2-style print? How to run vi on docker container? How can I install MacVim on OS X? Find and replace strings in vim on multiple lines Running Python code in Vim How do I set the default font size in Vim? Move cursor to end of file in vim Set encoding and fileencoding to utf-8 in Vim How to select all and copy in vim? Why I've got no crontab entry on OS X when using vim?

Examples related to clipboard

In reactJS, how to copy text to clipboard? Copy output of a JavaScript variable to the clipboard Leave out quotes when copying from cell How to Copy Text to Clip Board in Android? How does Trello access the user's clipboard? Copying a rsa public key to clipboard Excel VBA code to copy a specific string to clipboard How to make vim paste from (and copy to) system's clipboard? Python script to copy text to clipboard Copy to Clipboard for all Browsers using javascript