235

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)

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
ʞɔıu
  • 47,148
  • 35
  • 106
  • 149
  • 6
    just visual select, then `"+y`. – zx1986 Jul 04 '18 at 03: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 04 '18 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. – the Tin Man Dec 09 '19 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 "+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. – Neil Griffin Jan 15 '20 at 22:13

28 Answers28

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

George V. Reilly
  • 15,885
  • 7
  • 43
  • 38
  • 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. – Jonathan Dumaine Mar 07 '13 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 '13 at 01: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: http://stackoverflow.com/a/11421329/456584 – user456584 Jun 09 '14 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 06 '15 at 20:35
  • For what it's worth, this doesn't work for me on OS X El Capitan in terminal. – Joseph Hansen Nov 04 '15 at 23:28
  • 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. – George V. Reilly Nov 05 '15 at 01:38
  • I use vim installed via brew within the terminal. Do you think it would be possible to compile vim with the clipboard feature? – Martin Delille Nov 08 '18 at 10:48
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 _www@b77.apple.com
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 rampion@Myrrhy.local
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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
rampion
  • 87,131
  • 49
  • 199
  • 315
  • 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 http://github.com/adamv/homebrew/blob/duplicates/Library/Formula/vim.rb this homebrew formula to build and compile it. – rado Sep 14 '10 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 04 '11 at 09: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 03 '13 at 21:23
  • 3
    you can also get vim compiled with clipboard using homebrew: `brew install vim` – Anton Koval' Apr 28 '15 at 07:16
  • Note that if you install vim through homebrew, it symlinks only `vim` not, `vi`, so `vi` still launches the OS X-included vim. Took me a while to figure out why clipboard still wasn't working. – Johann Aug 11 '15 at 22:25
  • 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 '17 at 11:38
100

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

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
user242065
  • 1,213
  • 1
  • 8
  • 8
  • 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: http://vim.wikia.com/wiki/Mac_OS_X_clipboard_sharing – sdjuan Dec 25 '12 at 21:22
  • I followed this to get this working in tmux: http://superuser.com/questions/231130/unable-to-use-pbcopy-while-in-tmux-session – Wex Jan 22 '15 at 23:38
  • 1
    @Wex Link has been fixed – SnoringFrog Aug 26 '15 at 16:26
  • 2
    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. http://stackoverflow.com/questions/16196826/how-to-filter-visual-selection – Kevin Wheeler Oct 06 '15 at 02:35
  • 1
    @Kevin Wheeler: you can try with following command which can solve non-linewise copy noremap y ::let @a=@" \| execute "normal! vgvy" \| let res=system("pbcopy", @") \| let @"=@a – dragonxlwang Mar 08 '16 at 05: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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Chris AtLee
  • 7,798
  • 3
  • 28
  • 27
  • 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 '12 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 09 '16 at 16:50
  • this answer is the same as this http://stackoverflow.com/a/680271/2652524 – Gujarat Santana Sep 23 '16 at 02: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 '16 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 :) – Nikolay Fominyh Feb 19 '17 at 19:26
  • Yep, from that wikia post, this works for standard vim on OSX: `vnoremap y ::let @a=@" \| execute "normal! vgvy" \| let res=system("pbcopy", @") \| let @"=@a` – hoodslide Jan 17 '19 at 18:53
44

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

Paul Tomblin
  • 179,021
  • 58
  • 319
  • 408
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.

nikola
  • 5,286
  • 3
  • 22
  • 19
  • 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. – shridharama Jan 25 '19 at 23:36
17

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"

James Scriven
  • 7,784
  • 1
  • 32
  • 36
12

In my case I just had to do :

:set mouse=v

please visit the original solution on superuser.com

Community
  • 1
  • 1
Telmo Dias
  • 3,938
  • 2
  • 36
  • 48
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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Matt Hughes
  • 1,458
  • 2
  • 14
  • 17
  • 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 '11 at 08: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.

Jose Alban
  • 7,286
  • 2
  • 34
  • 19
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".

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
songz
  • 2,082
  • 1
  • 14
  • 18
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 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

Evan
  • 7,396
  • 4
  • 32
  • 31
4

I used:

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

It worked really well.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
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.

matpie
  • 17,033
  • 9
  • 61
  • 82
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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
frbl
  • 1,172
  • 11
  • 17
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.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Andrés Hazard
  • 43
  • 1
  • 1
  • 7
2

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

mouviciel
  • 66,855
  • 13
  • 106
  • 140
  • 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 '09 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 '09 at 16:00
2

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

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
redacted
  • 2,429
  • 2
  • 16
  • 10
1

What worked for me in my .vimrc

set clipboard=unnamed
if has("unnamedplus") " X11 support
    set clipboard+=unnamedplus
endif
maged
  • 859
  • 10
  • 24
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.

Community
  • 1
  • 1
Yaroslav Melnichuk
  • 820
  • 1
  • 11
  • 15
0

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

markroxor
  • 5,928
  • 2
  • 34
  • 43
0

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

user518066
  • 1,277
  • 3
  • 23
  • 35
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

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)

VineetChirania
  • 831
  • 1
  • 10
  • 18
-1

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

tom
  • 7
  • 1
-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

Bruce
  • 2,146
  • 2
  • 26
  • 22
-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.

  • 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 '12 at 22:19
-2

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

Matt Williamson
  • 39,165
  • 10
  • 64
  • 72
  • 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 07 '14 at 12:48