236

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)

4
  • 7
    just visual select, then "+y.
    – zx1986
    Jul 4, 2018 at 3:37
  • 2
    The vim that ships with OSX doesn't have +clipboard or +xterm-clipboard. You can verify this with vim --version | grep clipboard. It's possible to resolve this with brew install vim --with-client-server.
    – LondonRob
    Jul 4, 2018 at 15:20
  • 1
    Or use MacVim, which offers a command-line version which can by symlinked in your ~/bin and then ~/bin can be inserted into your path ahead of the normal system-installed vim and synonyms. Dec 9, 2019 at 20:23
  • Building on what @zx1986 wrote, I did three things: 1) Added the following to my ~/.bashrc in order to alias vi/vim to MacVim's terminal mode: alias vi='mvim -v' 2) Added the following to ~/.vimrc, which maps Ctrl+c (not Cmd+c): vnoremap <C-c> "+y 3) Configured Keyboard Maestro to type the key sequence ^c (Ctrl+C) when running in the terminal (in my case iTerm2) This makes it possible to use Cmd+c to copy text to the OSX clipboard so that it can be pasted in other OSX applications. Jan 15, 2020 at 22:13

28 Answers 28

264

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

7
  • 2
    I've spent all morning figuring out how to separate y, d and P commands from ⌘-C and ⌘-V that MacVim enables for you and finally came across this. Sure enough, I had added this to my vimrc when I was a vim juvenile and it has frustrated me ever since. Mar 7, 2013 at 21:19
  • 2
    For those new to MacVim, it is usable from the terminal with the -v flag (mvim -v). I have mine aliased to vim and mvim.
    – pk-nb
    Jul 29, 2013 at 1:55
  • 3
    As mentioned by @VincentG in his comment above, this indeed works with terminal-based versions of Vim, but those using tmux within the terminal will need to follow one of the solutions detailed here: stackoverflow.com/a/11421329/456584
    – user456584
    Jun 9, 2014 at 15:09
  • 3
    this should be the accepted answer -- as long as :echo has("clipboard") says 1, which is the case for MacVim, and I have a vi alias that runs mvim -v instead
    – jdsumsion
    Mar 6, 2015 at 20:35
  • 1
    @Joseph, if :version shows you -clipboard then the build of Vim doesn't support the clipboard feature. I was talking about MacVim, which is a GUI app. Nov 5, 2015 at 1:38
139

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.

6
  • 3
    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 github.com/adamv/homebrew/blob/duplicates/Library/Formula/… this homebrew formula to build and compile it.
    – rado
    Sep 14, 2010 at 17:47
  • Hmm, not sure why Vim didn't show up in my Homebrew, but I copied it down to /usr/local/Library/Formula, ran brew install vim, and it failed with "ld: library not found for -lruby-static". I'm using RVM and was on 1.8.7. When I switched to 1.9.2 the error was "ld: library not found for -lruby.1.9.1". The solution was to use system Ruby by first executing rvm use system.
    – justsee
    Mar 4, 2011 at 9:37
  • 9
    A little easier than searching for the word "clipboard" in what is usually a long list of features: do :echo has("clipboard"). If it shows 0, it's not compiled in. If it shows 1, it is.
    – ravron
    Jul 3, 2013 at 21:23
  • 3
    you can also get vim compiled with clipboard using homebrew: brew install vim Apr 28, 2015 at 7:16
  • 4
    Beware! I had a +clipboard version, but had installed --with-client-server which enables +x11, and trying to copy/paste to x11 in terminal vim on macOS didn't work :) I had to reinstall without +clientserver.
    – jacwah
    Jul 26, 2017 at 11:38
101

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

5
  • 7
    You can also select text from the command line like :1,10w !pbcopy to copy lines 1-10 to the clipboard. The w is important along with the space after it to keep original text in place. The source link above is now broken but there is a discussion here: vim.wikia.com/wiki/Mac_OS_X_clipboard_sharing
    – sdjuan
    Dec 25, 2012 at 21:22
  • I followed this to get this working in tmux: superuser.com/questions/231130/…
    – Wex
    Jan 22, 2015 at 23:38
  • 1
    @Wex Link has been fixed Aug 26, 2015 at 16:26
  • 3
    Note this seems to only work for selecting whole lines. It doesn't seem to work if you only want to select part of a line. stackoverflow.com/questions/16196826/… Oct 6, 2015 at 2:35
  • 1
    @Kevin Wheeler: you can try with following command which can solve non-linewise copy noremap <silent> <leader>y :<CR>:let @a=@" \| execute "normal! vgvy" \| let res=system("pbcopy", @") \| let @"=@a<CR> Mar 8, 2016 at 5:02
84

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.

6
  • 5
    For folks with VIM compiled without +clipboard the link above is an excellent resource to give workarounds w/o recompiling VIM
    – sdjuan
    Dec 25, 2012 at 21:29
  • VERY IMPORTANT DETAIL!!! If you have Vim-version 7.3 then this might not work for you, but if you have Vim-version 7.4 then it does (and sometimes you don't even have to do the whole "+y ). And you check this by just writing >vim in the terminal and then the version-number is when you start it up.
    – Zeth
    Sep 9, 2016 at 16:50
  • this answer is the same as this stackoverflow.com/a/680271/2652524 Sep 23, 2016 at 2:24
  • 1
    The "+ register exists only for X11 (unless you build vim with +X11 support, which will install a X11 server XQuartz in OS X), while OS X doesn't use X11 as its windows system.
    – nn0p
    Nov 27, 2016 at 18:20
  • What to do, if both - vim and macvim have +clipboard. And clipboard=unnamed added to ~/.vimrc? Looks like real pain, tried all workarounds and currently have to use spacemacs, when I know, that I'll have to use global clipboard :) Feb 19, 2017 at 19:26
45

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

6
  • 11
    just seems to emit a bell character and no other effect for me
    – ʞɔıu
    Mar 24, 2009 at 15:45
  • 4
    In X11, Vim's "* is PRIMARY, "+ is CLIPBOARD, and SECONDARY doesn't get a named register. (Not that anybody uses it...)
    – ephemient
    Mar 24, 2009 at 15:53
  • Perfectly. Really helps, especially with block selection (ctrl-v).
    – Sonique
    Jan 28, 2015 at 17:32
  • Since the question was specifically about Mac OS X, adding that it works for Mac "too" is unnecessary. Aug 12, 2015 at 16:47
  • @PaulTomblin okay, fair enough :-) Aug 14, 2015 at 0:03
27

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.

1
  • 1
    Something to keep in mind - quite bizarrely, brew install vim with updates vi, and not vim. So you might have to alias things differently after installing vim with homebrew. Jan 25, 2019 at 23:36
18

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"

1
  • Still copies the whole line
    – Karl Pokus
    Apr 28, 2018 at 19:39
12

In my case I just had to do :

:set mouse=v

please visit the original solution on superuser.com

0
11

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.

1
  • 1
    This is just what I needed. One just needs to make sure that $PATH is correct and you're getting /opt/local/bin/vim not the Apple vim.
    – Gareth
    Dec 13, 2011 at 8:58
9

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.

1
  • content is yanked but I can not paste outside of VIM.
    – prayagupa
    Apr 23, 2017 at 1:54
5

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

2
  • 1
    Then how to make the copy? Jul 4, 2015 at 2:35
  • with brew's version of vim I was able to do what I wanted: I highlighted the lines using V then :w !pbcopy and it worked
    – davidpm4
    Jan 31, 2016 at 21:43
5

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

1
  • but its also giving me 2 black lines after the text copied Mar 5, 2020 at 16:52
4

I used:

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

It worked really well.

3

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.

1
  • I use MacVim and then symlinked the command-line vim synonyms into my ~/bin to override the defaults. Dec 9, 2019 at 20:38
2

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

2
  • 6
    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
    Mar 24, 2009 at 15:49
  • 3
    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
    Mar 24, 2009 at 16:00
2

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

2

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.

2

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.

1

What worked for me in my .vimrc

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

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.

0

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

0

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

0

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.

-1

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
  • vim-full is no longer an ubuntu package name. the default vim package (7.3.154) has a minimal set of features, including -clipboard. Installing vim-gtk or vim-nox will provide a +clipboard version.
    – spazm
    Jun 11, 2012 at 22:19
-1

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)

-1

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

-1

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

-2

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

1
  • Traditional copy and paste does not work with Vim buffers and can completely mess up your formatting. For example, copying 15 lines of nested objects with Cmd + C and then pasting with Cmd + V will cause it to lose an understanding of the nesting. This very likely depends on your settings in your .vimrc and can be very particular
    – netpoetica
    Jul 7, 2014 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.