Is it possible to copy to clipboard directly from Vim? yy
only copies stuff to Vim's internal buffer. I want to copy to the OS's clipboard. Is there any such command in Vim or you can only yank stuff within Vim?
40 Answers
The *
register will do this. In Windows, +
and *
are equivalent. In unix there is a subtle difference between +
and *
:
Under Windows, the * and + registers are equivalent. For X11 systems, though, they differ. For X11 systems, * is the selection, and + is the cut buffer (like clipboard). http://vim.wikia.com/wiki/Accessing_the_system_clipboard
*
is probably what you want most of the time, so I use *
because it functions as I expect it to in both environments.
In Linux distros you have to install vim-gtk
(aka gvim
) first to gain clipboard functionality. This is because non-gtk vim is typically compiled without X11 support. This is to allow it to run on console only machines (often servers).
And for those confused about how to use registers when yanking or putting, you merely write "
then the name of the register. So for copying something to the clipboard register you type "*y
and then to put you type "*p
(credit: Kyle Mathews)
-
42Doesn't work on mac osx lion. After pressing " then *, it makes that noise the mac makes when you can't do something. How do I do it on mac?– EddyCommented Nov 13, 2011 at 18:03
-
76You have to also make sure that vim is compiled with support for the clipboard. The default vim in Ubuntu is not. Try
vim --version|grep .xterm_clipboard -o
and if it's-
then you do not have support. Download a different version as per ubuntuforums.org/showthread.php?t=1686955– SparhawkCommented Aug 3, 2013 at 5:39 -
94If you are using Linux, you should
+
register. For example, if you wanna copy three lines into system's clipboard, use commands likev3j"+y
, and then you can paste them into another application more another VIM. Using*
register under Linux can only copy and paste between different VIM applications. Commented Sep 11, 2013 at 11:17 -
12On Linux, the * register doesn't work for copying and pasting externally.– weberc2Commented Dec 5, 2013 at 16:08
-
18weberc2 and diabloneo, the * register is for the selection clipboard, accessible from middle clicking, not the menu paste or ^V Commented Dec 8, 2013 at 3:07
On Mac OSX
copy selected part: visually select text(type
v
orV
in normal mode) and type:w !pbcopy
copy the whole file
:%w !pbcopy
paste from the clipboard
:r !pbpaste
On most Linux Distros, you can substitute:
pbcopy
above withxclip -i -sel c
orxsel -i -b
pbpaste
usingxclip -o -sel -c
orxsel -o -b
-- Note: In case neither of these tools (xsel
andxclip
) are preinstalled on your distro, you can probably find them in the repos
-
47compatible with vim that is shipped with mavericks osx. add to your .vimrc
vnoremap <C-c> :w !pbcopy<CR><CR> noremap <C-v> :r !pbpaste<CR><CR>
To use this mapping, if you want to copy, highlight text in visual mode and hit Ctrl-c to copy. To paste from the system clipboard, hit Ctrl-v. Commented Jun 6, 2014 at 18:56 -
10On linux this works with :w !xclip -sel c or :w !xsel -b Commented Jun 21, 2015 at 21:17
-
9For Linux Ctrl+C copy: in ~/.vimrc add:
vnoremap <C-C> :w !xclip -i -sel c<CR><CR>
– andyCommented Dec 3, 2015 at 11:06 -
6A far more helpful answer than the accepted one. Thank you! Commented Aug 16, 2016 at 16:16
-
3I'm in Mac, vim shows +clipboard and either
:w !pbcopy
or:%w !pbcopy
copies the entire file Commented Sep 14, 2016 at 17:44
In your vimrc file you can specify to automatically use the system clipboard for copy and paste.
On macOS and Windows set:
set clipboard=unnamed
On Linux set (vim 7.3.74+):
set clipboard=unnamedplus
NOTE: You may need to use an up to date version of Vim for these to work.
-
53As an addendum to your note - you may also have to install extra packages to get this to work. If your vim lacks the
+xterm_clipboard
feature (visible when you runvim --version
). This was the case for me (running Kubuntu 12.10). I had to install thevim-gui-common
package to get the correct functionality. Commented Feb 13, 2013 at 8:40 -
8
set clipboard=unnamed
works in Mac OS 10.11.6 (El Capitan) with vim 7.4 Commented Aug 24, 2016 at 17:01 -
2You might wish to update your answer. It does not function on Ubuntu 18.04 with vim 8.0. Commented Feb 7, 2020 at 15:52
-
1This works for me with nvim 0.4.4. Thanks! To copy a whole file I use
ggvGGy
Commented Oct 23, 2020 at 12:47 -
3
set clipboard^=unnamed,unnamedplus
will solve it for Windows and Linux. Not sure about Mac. Commented Jun 25, 2021 at 17:59
Use the register "+
to copy to the system clipboard (i.e. "+y
instead of y
).
Likewise you can paste from "+
to get text from the system clipboard (i.e. "+p
instead of p
).
-
I use "*, but it seems that there's only a difference in X. Commented Oct 18, 2010 at 17:56
-
4@dash-tom-bang: the
+
/*
difference isSELECTION
vsPRIMARY
copy buffers (can't recall which is which atm)– DaenythCommented Oct 18, 2010 at 18:19 -
3
-
7The double quote character serves as the register selector in vi and vim. Commented Jun 18, 2015 at 2:29
-
1Two sets of shortcut to remember:
"+yy
(copy line to clipboard) and"+yy
(copy line to selection);"+p
(paste from clipboard) and"*p
(paste from selection)."
is to select register which is vim's own internal register by default (the wayyy
andp
would work without referencing any type of register). PS: I wonder if there's a character for Vim's own internal register something like"&yy
which turns toyy
by default.– user4104817Commented May 16, 2017 at 21:20
@Jacob Dalton has mentioned this in a comment, but nobody seems to have mentioned in an answer that vim has to be compiled with clipboard support for any of the suggestions mentioned here to work. Mine wasn't configured that way on Mac OS X by default and I had to rebuild vim. Use this the command to find out whether you have it or not vim --version | grep 'clipboard'
. +clipboard
means you're good and the suggestions here will work for you, while -clipboard
means you have to recompile and rebuild vim.
-
19Or, instead of compiling yourself, you can install a vim package that has already been built with clipboard support. On OS X, Homebrew has this:
brew install vim
. On Ubuntu, you can usesudo apt-get install vim-gnome
. On other (non-GNOME) Linux distros you may rather installvim-gtk
orvim-athena
. Commented May 13, 2014 at 18:06 -
4Thanks, in Arch Linux I had to remove
vim
and installgvim
wiki.archlinux.org/index.php/Vim#Installation Commented May 7, 2015 at 15:38 -
Your answers is explaining why the yank doesn't work with clipboard, with your information alone i could do a better google search and make it work easily. And now it working. It would be better if you also include @NeilTraft comment to your answer so that people doesn't need to do google search. Commented Jan 3, 2016 at 18:32
-
On Mac, right after
brew install vim
I addedset clipboard=unnamed
to .vimrc. Runsource ~/.vimrc
in command line. In .bash_profile I addedalias vim="/usr/local/bin/vim"
then runsource ~/.bash_profile
. Now in vim just usey
andp
to copy/paste– DmitryCommented Feb 23, 2019 at 18:37
Summing up and make it easier for newbies,
To copy the current line, in command mode type:
"*yy
To copy the whole file/buffer, in command mode, first go to the beginning via gg
, then type
"*yG
As noted, this requires +clipboard
out of vim --version
, which indicate the availability of clipboard support, -clipboard
means no.
-
4
"*5yy
says5 lines yanked
but nothing in the clipboard? os/x. Commented Jul 27, 2015 at 3:34 -
2
-
nnoremap <leader>c "*
in.vimrc
works quite well as a "copy" of sorts, followed by a motion to grab the text object you want to copy into your OS clipboard.– cpkCommented Aug 25, 2016 at 20:59 -
4I installed
vim-gtk
to turn-clipboard
into+clipboard
, than"+yy
is working.– Lai32290Commented Jul 5, 2017 at 17:53 -
2
For Ubuntu - July 2018
Use the register "+
to copy to the system clipboard (i.e. "+y
instead of y
).
Likewise you can paste from "+
to get text from the system clipboard (i.e. "+p
instead of p
).
You have to also make sure that vim is compiled with support for the clipboard. Try:
vim --version | grep .xterm_clipboard -o
and if it's -xterm_clipboard
(a minus prefix) then you do not have support.
Here are some instructions for swapping out with a working version of vim that has clipboard support.
$ sudo apt-get purge vim
$ sudo apt-get autoremove (removes any extraneous vim dependency packages from system)
$ sudo apt-get install vim-gnome (or `sudo apt-get install vim-gtk3` for newer Ubuntu versions)
Check again with vim --version | grep .xterm_clipboard -o
and you can confirm the clipboard is now available (ie. +xterm_clipboard
)
-
14You made my day, or maybe my month. After toiling with clipboardless vim on Ubuntu for years, this is an amazing feeling. arcseldon, thank you!! Commented Oct 10, 2018 at 17:22
-
4Besides what was described above, I had to put
set clipboard=unnamedplus
in my.vimrc
file for the yank to work. I am using Ubuntu 18.04.– ElijahCommented Nov 11, 2019 at 18:05 -
Same as Elijah's comment above: I needed to follow this answer and also update .vimrc. Commented Oct 15, 2022 at 18:26
If you are using vim in and old version of macOS, unfortunately the shipped version of vim may and not be compiled with clipboard options. Luckily, homebrew can easily solve this problem.
Install vim:
brew install vim
Install gui verion of vim:
brew install macvim
Restart the terminal to take effect.
Append the following line to
~/.vimrc
set clipboard=unnamed
Now you can copy the line in vim with
yy
and paste it system-wide.
Updated Method:
I was never satisfied with set clipboard method for years. The biggest drawback is it will mess up your clipboard history, even when you use x
for deletion. Here is a better and more elegant solution.
Copy the text [range] of vim into the system clipboard. (Hint: use
v
orV
to select the range first, and then type the colon:
to activate the Ex command)::[line-range]yank +
E.g., to copy/yank lines 5-10 to the system clipboard * register use:
:5,10y *
Paste the content from the system clipboard into vim on a new line:
:put +
Note:
- If you select a word range, the above will not work. use
"*y
or"+y
to save this visual block to clipboard. However this is hard to type, I am still thinking about alternatives. :help "*
or:help *+
for more informationsbrew info vim
will be able to see other options for installing vim. Currently it does not have any other options.
-
--with-override-system-vim
should be--with-override-system-vi
Commented Oct 18, 2017 at 19:49 -
I'd like to try this to avoid installing lots of Xorg stuff on minimal systems, but I am missing something. I get "Not an editor command [range]yank +" after selecting in v mode and then hitting ":" . Commented Dec 26, 2017 at 15:57
This answer contains details specific to macOS users.
Append the following line to ~/.vimrc
:
set clipboard=unnamed
If this does not work, check if your installed version maybe has the clipboard
feature not enabled. When this answer was written (2019), the default vim shipped with macOS did not come with clipboard option enabled. You need that option to access the system clipboard.
To check if your vim has that option enabled use the below command
vim --version | grep clipboard
In the result, you should have +clipboard
. If it is -clipboard
, then your VIM does NOT have the option to access the system clipboard.
You need to MAKE and install your VIM with the option you need. Following are the commands.
# Create the directories you need
$ sudo mkdir -p /opt/local/bin
# Download, compile, and install the latest Vim
$ cd ~
$ git clone https://github.com/vim/vim.git
$ cd vim
$ ./configure --prefix=/opt/local
$ make
$ sudo make install
# Add the binary to your path, ahead of /usr/bin
$ echo 'PATH=/opt/local/bin:$PATH' >> ~/.bash_profile
# Reload bash_profile so the changes take effect in this window
$ source ~/.bash_profile"
The above will install the latest VIM with the option +clipboard
enabled.
Now you can yank text to system clipboard. Below steps explains how to yank.
- In vim command mode
press v
, this will switch you to VISUAL mode. - Move the cursor around to select the text or lines you need to copy.
Press y
, this will copy the selected text to clipboard.- Go to any external application and
CMD + v
to paste.
I use MACBook Pro with macOS Mojave and the above works in it.
I wasn't able to copy to my clipboard's system because I had this in my ~/.vimrc file:
if has('mouse')
set mouse=a
endif
But if you add this line next, it will allow you to simply Ctrl+c to get whatever you have selected into your clipboard.
vmap <C-c> "+y
Original discussion and more details: Copy text out of vim with set mouse=a enabled
-
8I didn't add the line to my .vimrc, but the link you provided was very useful. If you hold down SHIFT while selecting text in vim, you're able to copy text without entering visual mode. So thanks and +1. Commented Nov 4, 2013 at 11:40
-
Shift trick is what I needed. Works as well with editors like terminator that use Ctrl+Shift+c to copy while in the prompt, which is what I tried at first. I did have -xterm_clipboard installed with vim. Commented Feb 12, 2017 at 20:17
-
You can also use
set mouse=nvi
. This omits command line mode. Then you can enter command mode (:) and use your mouse to copy. Hit escape after.– sqqqrlyCommented Feb 15, 2023 at 16:27
In vim under ubuntu terminal only,
press shift + drag mouse
to select a text in vim then ctrl + shift + c
on the terminal
then ctrl + v
on other editor
-
3This works to select only what is visual in vim, what if you want to select the entire file?– user745235Commented Sep 8, 2016 at 12:32
-
@Gerep this method not work for entire file selection. it's a good to select a section of text on vim terminal only and where text displayed on the terminal screen. Commented Sep 20, 2016 at 4:17
-
3This copies line numbers, buffer borders of splits and all that is visible, instead of only the text / code. Commented Dec 5, 2016 at 0:01
This question already has a lot of answers. I am adding my way which I think is quick.
Quickly, you can press V
(Shift + v) to active visual mode. In visible mode, you can use j
and k
to select the text you want to copy. After selection, use
"*y
Now, selected text is copied to clipboard.
-
1For Mac OS X: works simple and perfect. Make sure you have the "+clipboard" turned on, refer to gkb0986 answer for that. Commented Sep 19, 2020 at 10:03
-
-
I like you method but, I am in tmux, and want to paste in another panel, you know how?– MaskeCommented Feb 24, 2023 at 1:29
Keybindings to make it easier
I have been using these for many years now:
nnoremap Y "+y
vnoremap Y "+y
nnoremap yY ^"+y$
You can now just use upper case Y
to copy to clipboard, and lowercase y
won't be affected e.g. as by set clipboard=unnamed
so you can still choose if the copy will go to the clipboard or not.
Tested on ubuntu 21.04, vim 8.2.
-
I was using
vim
on Manjaro Linux and theclipboard
was not supported by the package, therefor keymaps did not work for me, so I replaced myvim
package withvim-clipboard
package then clipboard is available now and keymaps are working. Commented Feb 16, 2022 at 8:02
the solution for me was to install additional vim that has the clipboard option included:
sudo apt-get install vim-gnome
-
3This is what I had to do for Ubuntu 16.04. I tried the other suggestions without success. This was the easiest and did what I expected with "+y. Commented May 10, 2016 at 15:17
-
It works like a charm without any vimrc changes! still valid in Ubuntu 16.04– makerjCommented May 10, 2017 at 6:10
-
The
"+y
suggestion only worked withvim-gnome
. Using Linux Mint 18.2.– SamaursaCommented Sep 28, 2017 at 1:45 -
1Besides vim-gnome, this is also supported by default in neovim on Ubuntu 20.04– qwrCommented Sep 9, 2022 at 5:38
You can find the answers here Arch Wiki
For Linux:
First of all you have to enable Clipboard in your vim version by installing
gvim
.
Next you have to put this line on your .vimrc
file.
set clipboard=unnamedplus
-
+1, was surprised that this step is now required. In all of my previous Arch installation I didn't have to do it. Commented Oct 20, 2017 at 14:34
-
worked for ubuntu 18.04 too and running in tmux. Thanks! Commented Jan 12, 2019 at 2:51
If you are using GVim, you can also set guioptions+=a
. This will trigger automatic copy to clipboard of text that you highlight in visual mode.
Drawback: Note that advanced clipboard managers (with history) will in this case get all your selection history…
If your vim happens to be compiled without +xterm_clipboard
option like it is by default in Debian and I guess Ubuntu, you can pipe selection or entire buffer to external program that handles desktop clipboard. For xclip
(which you may need to install previously), the command will be :w !xclip -sel clip
-
1Thanks for this - it hasn't worked for me on Ubuntu (so confirming your Ubuntu guess is correct) for ages and I didn't know why. Now I can see
-xterm_clipboard
in the flags shown when Ivim --version
.– jamescCommented Jun 30, 2014 at 14:40 -
This helped me find a good source of clipboard support: vimcasts.org/blog/2013/11/getting-vim-with-clipboard-support– jamescCommented Jun 30, 2014 at 14:53
-
You can also add a custom command in your vimrc:
command Xclip :w !xclip -sel clip
, then you can just type:Xclip
– gitaarikCommented Jun 14, 2023 at 15:22
If you have xclip an easy way of copying text to the clipboard is as follows:
- Yank text you want to copy. (
y
command in vanilla vim) - Type in
:call system("xclip -selection clipboard", @")
:call system()
runs a terminal command. It takes two arguments, the first the command, the second what to pipe to that command. For example :echom system("head -1", "Hello\nWorld")
returns Hello (With some padding). echom returns the output of a command, call doesn't.
xclip -selection clipboard
just copies text into the system clipboard as opposed to the default X clipboard, (Accessed by the middle moue button).
@"
returns the last yanked text. " is the default register, but you could use any register. To see the contents of all registers, type :registers
.
-
3This is more robust, it works even when vim is not complied with support for the clipboard and on any OS. For example, on Mac run
:call system("pbcopy",@")
. One can bind this command to a key.– ilija139Commented Sep 4, 2018 at 11:35 -
it works fine, but can I put it in my vimrc, will it work. It is not working for me. Commented Apr 21, 2020 at 19:06
I've been struggling with this for months till now on MacOsX using keyboard shortcuts. I know question isn't about using keyboard shorts. But this might help someone with the same concern.
I found that if you uncheck:
View -> Allow Mouse Reporting
from Terminal menu, you'll be able to copy to clipboard using
command + c
again.
My solution was putting the following line to .vimrc:
map <C-y> :w !xclip -sel c <CR><CR>
The script copies the selected line (trough visual mode) or the file content (if none is selected) to the clipboard using Ctrl + y. I'm using Manjaro Linux if that matters.
-
-
Thanks, this works well! Just needed a way to quickly copy my selection to the clipboard as it spanned beyond the terminal view.– jaseeeyCommented Dec 10, 2020 at 3:46
-
I'm a Vim newby but, to copy all the text to system clipboard (e.g if you want to paste it to a word processor or another text editor like gedit, mousepad etc...), in normal mode:
ggVGy
or, more simply:
:%y
As suggested, I also installed vim-gtk and put
set clipboard=unnamedplus
in my .vimrc
and everything works fine
If you want to copy only a portion of text, use visual mode (v), select the text you want to copy and press y.
Finally, I suggest a clipboard program like Clipman (my favorite), Clipit, Parcellite or similar.
(I'm using vim 8.0 in Debian Stretch Xfce)
FORGIVE ME FOR MY ENGLISH! :-)
- Put
set clipboard=unnamed
in yourvimrc
. - Select what you want to copy in
Visual
mode (Press v to enter). - Back to
Normal
mode (Press escape[esc]), press y to copy. - If you want to paste something from OS's clipboard, press p/P in Vim
Normal
mode.
Maybe someone will find it useful. I wanted to stay independent from X clipboard, and still be able to copy and paste some text between two running vims. This little code save the selected text in temp.txt file for copying. Put the code below into your .vimrc. Use CTRL-c CTRL-v to do the job.
vnoremap :w !cp /dev/null ~/temp.txt && cat > ~/temp.txt
noremap :r !cat ~/temp.txt
I'm on mac osx (10.15.3) and new to vim. I found this so frustrating and all the answers on here too complicated and/or didn't apply to my situation. I ended up getting this working in 2 ways:
key mapping that uses pbcopy: works on the old version of vim that ships with mac.
Add
vmap '' :w !pbcopy<CR><CR>
to your ~/.vimrc
Now you can visually select and hit''
(two apostrophes) to copy to clipboardInstall newer version of vim so I can access the solution most recommended in other answers:
brew install vim
alias vim=/usr/local/bin/vim
(should add this to your ~/.bashrc or equivalent)
Now you can visually select and hit"+yy
to copy to clipboard
In case you don't want to use any graphical interface for vim and you prefer to just stick with terminal emulator there may be a much simpler approach to this problem. Instead of using yank or anything like this, first take a look at documentation of terminal you use. I've been struggling with the same issue (trying to use +clipboard and xclip and so on) and in the end it turned out that in my terminal emulator it's enough to just press shift and select any text you want to copy. That's it. Quite simple and no need for messing with configuration. (I use urxvt by the way).
I had issue because my vim was not supporting clipboard:
vim --version | grep clip
-clipboard +insert_expand +path_extra +user_commands
+emacs_tags -mouseshape +startuptime -xterm_clipboard
I installed vim-gnome (which support clipboard) and then checked again:
vim --version | grep clipboard
+clipboard +insert_expand +path_extra +user_commands
+emacs_tags +mouseshape +startuptime +xterm_clipboard
Now I am able to copy and paste using "+y and "+p respectively.
Besides vim-gnome, "+y is also supported by default in neovim on Ubuntu 20.04.
If you don't want to install a new program, you could always do the lazy method of cat file.txt
or gedit file.txt
and copy from there.
I wrote a simple line in my .vimrc to get copy working. Hope this helps someone. My vim is not installed with Clipboard support, unfortunately, so none of these suggestions worked for me. Basically, paste this line in your .vimrc:
map <C-c> y:e ~/clipsongzboard<CR>P:w !pbcopy<CR><CR>:bdelete!<CR>
If you'd like to read details about what this does, you can read about this on my blog
-
1
-
this does not seem to copy anything to the clipboard when using as instructed (on linux ubuntu 16.04, no clipboard support), and if multiple files are opened in a vim session, will close the tab where the actions were taken (visual select and C-c). Commented Jun 7, 2018 at 3:07
For some international keyboards, you may need to press "+Space to get a "
.
So in those case you would have to press "Space+y or "Space*y
I saw many answers on this question and the way to make this work was a combination of many.
The steps I followed to make vim copy to system clipboard are
- Uninstall vim using
sudo apt remove vim
. (I was too lazy to find how to re-compile it with the +clipboard support. - Install a different vim package called vim-athena using
sudo apt install vim-athena
that ships with +clipboard. - Add to
~/.vimrc
the following line:set clipboard=unnamedplus
. - Source the file by entering command mode and typing
source %
. - Save and exit.
Note: I am using Ubuntu 20.04.
sudo apt-get install vim-gnome
before applying these commands below or you will get Invalid Register Error!vim-gnome
is probably overkill or simply unavailable in their OS/distro, whereas I suspectvim-gtk
and preferablyvim-gtk3
are more likely to exist and pull fewer dependencies, while still providing clipboard integration (at least for those still on X11; I'm not sure how this all interacts with Wayland).