How to copy a selection to the OS X clipboard

MacosVimClipboard

Macos Problem Overview


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)

Macos Solutions


Solution 1 - Macos

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

Solution 2 - Macos

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.

Solution 3 - Macos

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

Solution 4 - Macos

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.

Solution 5 - Macos

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

Solution 6 - Macos

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.

Solution 7 - Macos

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"

Solution 8 - Macos

In my case I just had to do :

:set mouse=v

please visit the original solution on superuser.com

Solution 9 - Macos

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.

Solution 10 - Macos

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.

Solution 11 - Macos

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

Solution 12 - Macos

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 root@apple.com
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

Solution 13 - Macos

I used:

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

It worked really well.

Solution 14 - Macos

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.

Solution 15 - Macos

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

Solution 16 - Macos

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

Solution 17 - Macos

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.

Solution 18 - Macos

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.

Solution 19 - Macos

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.

Solution 20 - Macos

What worked for me in my .vimrc

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

Solution 21 - Macos

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

Solution 22 - Macos

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

Solution 23 - Macos

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.

Solution 24 - Macos

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.

Solution 25 - Macos

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)

Solution 26 - Macos

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

Solution 27 - Macos

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

Solution 28 - Macos

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionʞɔıuView Question on Stackoverflow
Solution 1 - MacosGeorge V. ReillyView Answer on Stackoverflow
Solution 2 - MacosrampionView Answer on Stackoverflow
Solution 3 - Macosuser242065View Answer on Stackoverflow
Solution 4 - MacosChris AtLeeView Answer on Stackoverflow
Solution 5 - MacosPaul TomblinView Answer on Stackoverflow
Solution 6 - MacosnikolaView Answer on Stackoverflow
Solution 7 - MacosJames ScrivenView Answer on Stackoverflow
Solution 8 - MacosTelmo DiasView Answer on Stackoverflow
Solution 9 - MacosMatt HughesView Answer on Stackoverflow
Solution 10 - MacosJose AlbanView Answer on Stackoverflow
Solution 11 - MacossongzView Answer on Stackoverflow
Solution 12 - MacosEvanView Answer on Stackoverflow
Solution 13 - Macosuser3133576View Answer on Stackoverflow
Solution 14 - MacosmatpieView Answer on Stackoverflow
Solution 15 - MacosmouvicielView Answer on Stackoverflow
Solution 16 - MacosredactedView Answer on Stackoverflow
Solution 17 - MacosfrblView Answer on Stackoverflow
Solution 18 - MacosAndrés HazardView Answer on Stackoverflow
Solution 19 - MacosYaroslav MelnichukView Answer on Stackoverflow
Solution 20 - MacosmagedView Answer on Stackoverflow
Solution 21 - MacosmarkroxorView Answer on Stackoverflow
Solution 22 - Macosuser518066View Answer on Stackoverflow
Solution 23 - MacosMicah RothenbuhlerView Answer on Stackoverflow
Solution 24 - MacosplowmanView Answer on Stackoverflow
Solution 25 - MacosVineetChiraniaView Answer on Stackoverflow
Solution 26 - MacostomView Answer on Stackoverflow
Solution 27 - MacosBruceView Answer on Stackoverflow
Solution 28 - MacosMatt WilliamsonView Answer on Stackoverflow