1060

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?

Mateusz Piotrowski
  • 8,029
  • 10
  • 53
  • 79
Hari Menon
  • 33,649
  • 14
  • 85
  • 108
  • 207
    Please see the under-voted answer that clarifies an important fact: **you must have vim compiled with the +clipboard feature for ANY of the below suggestions to work in the first place!** This is NOT the default on most systems. – Neil Traft May 13 '14 at 18:07
  • 11
    @NeilTraft, some answers suggest piping from vim to external programs, so the claim that ANY answer requires +clipboard flag, is wrong. Just pointing out to save future readers from confusion. – Hnatt Oct 17 '14 at 17:54
  • 2
    Possible duplicate of [How to make vim paste from (and copy to) system's clipboard?](http://stackoverflow.com/questions/11489428/how-to-make-vim-paste-from-and-copy-to-systems-clipboard) (Note: the other question is newer, but has better answers IMHO, which I why I marked this one as a dupe) – Martin Tournoij Mar 15 '16 at 00:16
  • 5
    **Make sure you have done** ``sudo apt-get install vim-gnome`` **before applying these commands below** or you will get Invalid Register Error! – Harnirvair Singh Apr 16 '16 at 15:51
  • 9
    @Harnirvair For many sets of readers here, `vim-gnome` is probably overkill or simply unavailable in their OS/distro, whereas I suspect `vim-gtk` and preferably `vim-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). – underscore_d Oct 29 '16 at 00:44
  • 1
    See also https://vi.stackexchange.com/questions/84/how-can-i-copy-text-to-the-system-clipboard-from-vim – icc97 May 27 '17 at 19:03
  • Checkout https://stackoverflow.com/a/65666057/9384511 – ABN Jan 11 '21 at 12:55
  • i use ```cat file.filetype | clip``` on git bash in windows as i need to copy whole file as assignment in school. for sections of a file i have added ```set clipboard=unnamed``` in my _vimrc. – Aritro Shome May 14 '21 at 14:57
  • On Ubuntu make sure and install vim-gui-common even if only using the non gui version of vim, as this seems to also install the clipboard capable non gui vim – Goblinhack Nov 22 '21 at 09:53
  • Besides vim-gnome, `"+y` is also supported by default in neovim on Ubuntu 20.04 – qwr Sep 09 '22 at 05:40

40 Answers40

669

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)

Sudo Bash
  • 373
  • 5
  • 14
Gabe Moothart
  • 31,211
  • 14
  • 77
  • 99
  • 40
    Doesn'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? – Eddy Nov 13 '11 at 18:03
  • 74
    You 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 http://ubuntuforums.org/showthread.php?t=1686955 – Sparhawk Aug 03 '13 at 05:39
  • 89
    If you are using Linux, you should `+` register. For example, if you wanna copy three lines into system's clipboard, use commands like `v3j"+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. – diabloneo Sep 11 '13 at 11:17
  • 10
    On Linux, the * register doesn't work for copying and pasting externally. – weberc2 Dec 05 '13 at 16:08
  • 2
    Also, why is the system clipboard not used by default. You know, since it works in VIM and outside of it as well... – weberc2 Dec 05 '13 at 16:10
  • 15
    weberc2 and diabloneo, the * register is for the selection clipboard, accessible from middle clicking, not the menu paste or ^V – Jim Keener Dec 08 '13 at 03:07
  • 1
    By the way, you need to check if VIM compile with +clipboard features. On Ubuntu you can install vim-gnome to get this feature. – diabloneo May 28 '14 at 11:47
  • 1
    @Sparhawk The Ubuntu forums thread post is now out of date - 'Huge vim' config on Ubuntu now comes with '-xterm_clipboard. – jamesc Jun 30 '14 at 14:51
  • @jamesc Thanks for the info. I've switched distros, but I seem to remember having `gvim` installed. I'm not sure if that works now. – Sparhawk Jun 30 '14 at 15:13
  • Not a perfect place to ask but has anyone got it working for vim in a (gnu-)screen? I can't get it copied, the default screen copy method doesn't work for multiple lines in a split vim window :-/. – 0xc0de Dec 17 '15 at 06:04
  • 4
    On Ubuntu, install vim-gtk will enable +clipboard and +xterm_clipboard features, and then you can use `"+y` to copy content to system clipboard. In other program like Chrome, you can use `Ctrl+v` to paste the content. You need to use middle button of mouse to paste content if you use `"*y` to copy content in vim. – Chen wei Jun 30 '16 at 07:31
  • 1
    I have Ubuntu 16.04 with vim 8.1 (2018 May). `"+y` worked for me but not `"*y`. – Ramesh-X Jul 05 '18 at 05:13
  • 1
    For Ubuntu 18,04 found middle click often didn't work, especially when trying to paste passwords in chrome. Using "+y always has worked. For pasting into vim select then in vim "*p middle click works, no copy command needed for the source, only select. – user2584621 Sep 06 '18 at 19:16
  • For easier use, I mapped this command with `nnoremap c "*y` and `nnoremap v "*p` on vimrc. This can be changed to what your system accepts. Made it easier to remember the command and interact with the outside world :) – Carlos Eduardo Millani Apr 24 '19 at 13:11
  • Thanks for the hint with `vim-gtk` I always wondered why sometimes `y"*` failed to work! Maybe that rectifies emphasing this part a bit. – karlsebal Sep 29 '20 at 07:18
  • Answer good, but long story short, "vim-gtk" is must, it doesnt work without. " is a separate shortcut for a command input, similarly as the key :. "*y copies to the x-selection clipboard , "+y copies to the primary clipboard – FantomX1 Nov 13 '20 at 13:15
  • My god this is difficult. Now I know why I don't use `:set mouse=a` (or dare I say XEmacs). – Sridhar Sarnobat May 14 '22 at 05:25
  • Default desktop Ubuntu should be compiled with this by default. – qwr Sep 12 '22 at 21:02
  • Good news for macOS users. I just tested on the default vim installed in macOS Ventura 13.3.1 and the `*` register works just fine with `:set mouse=a` enabled and all. – Necro May 02 '23 at 22:24
473

On Mac OSX

  • copy selected part: visually select text(type v or V 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 with xclip -i -sel c or xsel -i -b
  • pbpaste using xclip -o -sel -c or xsel -o -b
    -- Note: In case neither of these tools (xsel and xclip) are preinstalled on your distro, you can probably find them in the repos
holms
  • 9,112
  • 14
  • 65
  • 95
Brian
  • 30,156
  • 15
  • 86
  • 87
  • 1
    +1 Works with Lion, though `vim --version` shows `-clipboard` – roplacebo Sep 17 '13 at 23:50
  • 43
    compatible with vim that is shipped with mavericks osx. add to your .vimrc ```vnoremap :w !pbcopy noremap :r !pbpaste``` 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. – davidtingsu Jun 06 '14 at 18:56
  • The only problem so far with this method is that pasting breaks formatting (mac osx mavericks). Seems like Mavericks messed up my Vim installation :-) if you use pbcopy and pbpaste in visual mode it's okay, but if you cannot use pbcopy and then Command + V without breaking formatting – netpoetica Jul 07 '14 at 05:34
  • 10
    On linux this works with :w !xclip -sel c or :w !xsel -b – trve.fahad Jun 21 '15 at 21:17
  • hi, @Zeus77, thanks for the info. I'd like to update the answer. would you please tell me how to past from the clipboard on Linux? Or you can edit the answer by yourself if you want to :) – Brian Jun 22 '15 at 03:54
  • 8
    For Linux Ctrl+C copy: in ~/.vimrc add: `vnoremap :w !xclip -i -sel c` – andy Dec 03 '15 at 11:06
  • 6
    A far more helpful answer than the accepted one. Thank you! – Bryce Johnson Aug 16 '16 at 16:16
  • 3
    I'm in Mac, vim shows +clipboard and either `:w !pbcopy` or `:%w !pbcopy` copies the entire file – Neithan Max Sep 14 '16 at 17:44
  • 1
    @davidtingsu's keybindings will conflict with `` in normal mode to enter visual block selection mode. Limiting it to insert mode only also conflicts with the insert literal char sequence. – Jangari Mar 28 '18 at 03:56
  • @Zeus77 xcopy & xsol don't work for me. They copy all content of file. I use vim 8.0 on ubuntu – MortezaE Oct 31 '18 at 10:48
  • @MortezaE add this to your vimrc `set clipboard=unnamed,unnamedplus`. this will use the OS clipboard for vim's yank operations. and then you can just select in visual mode and yank whatever you want. – trve.fahad Nov 03 '18 at 15:35
  • @Zeus77 it doesn't work, but it's not so important. thanks – MortezaE Nov 03 '18 at 23:26
  • 1
    @MortezaE for that to work you need a build of vim with clipboard support. on linux this can be `gvim`, `vim-gnome` or `vim-athena` – trve.fahad Nov 09 '18 at 10:06
  • 1
    This is how you can verify clipboard support: http://vim.wikia.com/wiki/Accessing_the_system_clipboard#Checking_for_X11-clipboard_support_in_terminal – trve.fahad Nov 09 '18 at 10:09
  • Oh I wasn't aware about it. I was using vim. thank you – MortezaE Nov 09 '18 at 10:51
  • 3
    This copies whole line for me – gadelat Oct 23 '19 at 12:08
  • 2
    I've made an improved version which allows copying only part of the line here: https://vi.stackexchange.com/questions/26745/improve-copy-to-clipboard-using-xclip – Tim Mak Aug 07 '20 at 02:43
  • 1
    `xclip -o -sel -c` is supposed to be `xclip -o -sel c` and the command to copy a selection is not `:w !pbcopy` but instead `:'<,'>w !pbcopy`. – Myridium Jul 03 '21 at 02:47
  • This unfortunately does not work on mac (host) connected to a remote machine (per ssh). – Lars Bilke Dec 17 '21 at 09:03
  • Attaching a key combination to ```:w !xclip -i -sel c\n``` did the trick for me. – Syed M Abbas Haider Taqvi Jan 22 '23 at 17:48
  • `:set mouse=` to reset mouse setting if someone made it impossible to do regular terminal-selection paste. – odinho - Velmont Apr 21 '23 at 13:48
  • If you're getting the entire buffer copied, it's because you didn't first select the text you wanted to copy. Use `v` or `V` to select the lines that will be copied. Then `:w !pbcopy` copies only the selected text. – Rich006 Apr 26 '23 at 12:07
  • To avoid mapping Ctrl-C in your `.vimrc`, you can instead define a `leader` such as the space key. For example: `let mapleader=" "` followed by `vnoremap c :w !pbcopy` – Rich006 Apr 26 '23 at 12:26
249

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.

http://vim.wikia.com/wiki/Accessing_the_system_clipboard

Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55
Gary Willoughby
  • 50,926
  • 41
  • 133
  • 199
  • 50
    As 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 run `vim --version`). This was the case for me (running Kubuntu 12.10). I had to install the `vim-gui-common` package to get the correct functionality. – Jacob Dalton Feb 13 '13 at 08:40
  • 8
    `set clipboard=unnamed` works in Mac OS 10.11.6 (El Capitan) with vim 7.4 – Purplejacket Aug 24 '16 at 17:01
  • 1. Copy on drag 2. mouse select, works in Mac Sierra-10.12.6 and vim-version: 8.0.1800 by specifying following commands in ~/.vimrc file `set mouse=v` and `set clipboard=unnamed` – dkb May 10 '18 at 06:03
  • 2
    You might wish to update your answer. It does not function on Ubuntu 18.04 with vim 8.0. – Luís de Sousa Feb 07 '20 at 15:52
  • 1
    This works for me with nvim 0.4.4. Thanks! To copy a whole file I use ```ggvGGy``` – 1nternetz Oct 23 '20 at 12:47
  • 2
    `set clipboard^=unnamed,unnamedplus` will solve it for Windows and Linux. Not sure about Mac. – André Willik Valenti Jun 25 '21 at 17:59
  • @LuísdeSousa `set clipboard=unnamedplus` works with Ubuntu 18.04 and Vim 8.0 when `vim-gui-common` is installed as Jacob Dalton pointed out. – Sathish Manohar Jul 24 '22 at 07:52
177

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

Cascabel
  • 479,068
  • 72
  • 370
  • 318
Amardeep AC9MF
  • 18,464
  • 5
  • 40
  • 50
  • I use "*, but it seems that there's only a difference in X. – dash-tom-bang Oct 18 '10 at 17:56
  • 4
    @dash-tom-bang: the `+`/`*` difference is `SELECTION` vs `PRIMARY` copy buffers (can't recall which is which atm) – Daenyth Oct 18 '10 at 18:19
  • 3
    Yeah star is the selection, plus is the clipboard. – dash-tom-bang Oct 18 '10 at 19:27
  • 7
    The double quote character serves as the register selector in vi and vim. – Amardeep AC9MF Jun 18 '15 at 02:29
  • 1
    Two 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 way `yy` and `p` 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 to `yy` by default. –  May 16 '17 at 21:20
  • I also recall that in Vim you can have many internal registers and assign them to **letters** (I think it would be something like `"ayy` to copy into the `a` register) but I'm not very much informed about that :( –  May 16 '17 at 21:20
  • This is what I use (macOS v.10.12.6) and it works just fine. Wish this was higher up because it's a lot less complicated answer for mac users. – astrocat1997 Aug 31 '17 at 01:38
  • Is there any way to make `yy` copy things directly into the system buffer? –  Oct 11 '20 at 08:25
  • When people say "register" what do they mean? I find that term confusing :/ Sorry, I'm new to vim. – J86 Feb 22 '21 at 14:12
  • Any ideas why this solution might have stopped working? I always used `"+y` to copy to clipboard in nvim with Ubuntu, but recently it stopped working. Vim (and nvim) say `282 lines yanked into "+` when doing it, but then when I Ctrl+P somewhere, it does not paste it – lpares12 May 12 '23 at 13:13
94

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

gkb0986
  • 3,099
  • 1
  • 24
  • 22
  • 19
    Or, 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 use `sudo apt-get install vim-gnome`. On other (non-GNOME) Linux distros you may rather install `vim-gtk` or `vim-athena`. – Neil Traft May 13 '14 at 18:06
  • 4
    Thanks, in Arch Linux I had to remove `vim` and install `gvim` https://wiki.archlinux.org/index.php/Vim#Installation – michalzuber May 07 '15 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. – Yana Agun Siswanto Jan 03 '16 at 18:32
  • On Mac, right after `brew install vim` I added `set clipboard=unnamed` to .vimrc. Run `source ~/.vimrc` in command line. In .bash_profile I added `alias vim="/usr/local/bin/vim"` then run `source ~/.bash_profile`. Now in vim just use `y` and `p` to copy/paste – Dmitry Feb 23 '19 at 18:37
78

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.

ryenus
  • 15,711
  • 5
  • 56
  • 63
66

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)

arcseldon
  • 35,523
  • 17
  • 121
  • 125
  • 13
    You made my day, or maybe my month. After toiling with clipboardless vim on Ubuntu for years, this is an amazing feeling. **arcseldon, thank you!!** – evanrmurphy Oct 10 '18 at 17:22
  • 4
    Besides 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. – Elijah Nov 11 '19 at 18:05
  • Same as Elijah's comment above: I needed to follow this answer and also update .vimrc. – josephdpurcell Oct 15 '22 at 18:26
44

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.

  1. Copy the text [range] of vim into the system clipboard. (Hint: use v or V 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 * 
    
  2. Paste the content from the system clipboard into vim on a new line:

    :put +
    

Note:

  1. 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.
  2. :help "* or :help *+ for more informations
  3. brew info vim will be able to see other options for installing vim. Currently it does not have any other options.
anonymous
  • 1,372
  • 1
  • 17
  • 22
  • `--with-override-system-vim` should be `--with-override-system-vi` – maffo Oct 18 '17 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 ":" . – jeremyjjbrown Dec 26 '17 at 15:57
32

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

Community
  • 1
  • 1
jcarballo
  • 27,395
  • 3
  • 28
  • 28
  • 8
    I 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. – Stefan van den Akker Nov 04 '13 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. – Anders Elmgren Feb 12 '17 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. – sqqqrly Feb 15 '23 at 16:27
31

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.

  1. In vim command mode press v, this will switch you to VISUAL mode.
  2. Move the cursor around to select the text or lines you need to copy.
  3. Press y, this will copy the selected text to clipboard.
  4. Go to any external application and CMD + v to paste.

I use MACBook Pro with macOS Mojave and the above works in it.

Cornelius Roemer
  • 3,772
  • 1
  • 24
  • 55
Joseph
  • 877
  • 8
  • 20
28

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

Supawat Pusavanno
  • 2,968
  • 28
  • 21
  • 3
    This works to select only what is visual in vim, what if you want to select the entire file? –  Sep 08 '16 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. – Supawat Pusavanno Sep 20 '16 at 04:17
  • 3
    This copies line numbers, buffer borders of splits and all that is visible, instead of only the text / code. – Zelphir Kaltstahl Dec 05 '16 at 00:01
26

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.

Jogendra Kumar
  • 894
  • 11
  • 16
20

the solution for me was to install additional vim that has the clipboard option included: 

sudo apt-get install vim-gnome
AbcAeffchen
  • 14,400
  • 15
  • 47
  • 66
alex
  • 201
  • 2
  • 2
19

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.

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
  • I was using `vim` on Manjaro Linux and the `clipboard` was not supported by the package, therefor keymaps did not work for me, so I replaced my `vim` package with `vim-clipboard` package then clipboard is available now and keymaps are working. – adnan ahmady Feb 16 '22 at 08:02
18

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

HOuadhour
  • 193
  • 1
  • 7
16

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…

Benoit
  • 76,634
  • 23
  • 210
  • 236
13

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

Hnatt
  • 5,767
  • 3
  • 32
  • 43
  • 1
    Thanks 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 I `vim --version`. – jamesc Jun 30 '14 at 14:40
  • This helped me find a good source of clipboard support: http://vimcasts.org/blog/2013/11/getting-vim-with-clipboard-support/ – jamesc Jun 30 '14 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` – gitaarik Jun 14 '23 at 15:22
13

If you have xclip an easy way of copying text to the clipboard is as follows:

  1. Yank text you want to copy. (y command in vanilla vim)
  2. 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.

John AZ1
  • 177
  • 2
  • 9
  • 3
    This 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. – ilija139 Sep 04 '18 at 11:35
  • it works fine, but can I put it in my vimrc, will it work. It is not working for me. – thenakulchawla Apr 21 '20 at 19:06
8

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.

GO.exe
  • 646
  • 7
  • 13
7

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! :-)

6
  1. Put set clipboard=unnamed in your vimrc.
  2. Select what you want to copy in Visual mode (Press v to enter).
  3. Back to Normal mode (Press escape[esc]), press y to copy.
  4. If you want to paste something from OS's clipboard, press p/P in Vim Normal mode.
BigPa
  • 71
  • 2
  • 4
6

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.

4

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

4

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:

  1. 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 clipboard

  2. Install 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

Dharam
  • 153
  • 1
  • 1
  • 6
3

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

alagris
  • 1,838
  • 16
  • 31
3

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.

me_astr
  • 922
  • 1
  • 14
  • 21
2

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

songz
  • 2,082
  • 1
  • 14
  • 18
  • 1
    Buyer beware. On mac, this has weird effects. – 4Z4T4R Feb 12 '14 at 00:44
  • 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). – calocedrus Jun 07 '18 at 03:07
2

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

Arthur F
  • 51
  • 1
2

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.

qwr
  • 9,525
  • 5
  • 58
  • 102
1

for OSX, like the 10342 answers above made clear, you need to make sure that vim supports the clipboard feature, said the the one that comes pre-shipped with OSX does NOT support clipboard, and that if you run brew install vim it would work.

Except that running vi will still make you run the preshipped OSX version, not the one you installed from brew.

to get over this, I simply aliased my vim command to the brew version, not the OSX default one:

alias vim="/usr/local/Cellar/vim/8.0.1100_1/bin/vim"

and now i'm golden

abbood
  • 23,101
  • 16
  • 132
  • 246
1

In my instance selecting text by highlighting (clicking and dragging) with my mouse was causing vim to enter into visual mode.

On a mac anyway, the easiest solution is to use fn + mouse click and drag to avoid entering into visual mode.

If you wish to avoid this behavior by default you can edit your vimrc

MochiJump
  • 319
  • 3
  • 10
1

I have struggled a lot in copying to clipboard. Inside Vim it is quite simple using visual mode but if you want to copy to the clipboard things are quite messsed. I have simple method of copying using xclip utility. For this you must have to install xclip first.

for the whole file it is very simple

xclip -sel clip filename

but if you want to copy only a particular range of line numbers
tail -n +[n1] filename | head -n [n2] | xclip -sel clip

you can make use of ~/.bashrc to simplify this

#rangecp copy lines from n1 to n2 from a given file to clipboard
function rangecp()
{
    if [ -f $1 ]
    then
        if [ ! -z $3 ]
        then
            diff=$(($3 - $2 + 1))
            #diff=`expr $3 - $2 + 1`
            tail -n +$2  $1 | head -n $diff | xclip -sel clip
        elif [ ! -z $2 ]
        then
            tail -n +$2  $1 | xclip -sel clip
        else
            echo "Provide a range from [n1] lines to [n2] lines"
        fi
    else
        echo "[ $1 ] file doesn't exist"
    fi
}

then

source ~/.bashrc
How to use

rangecp filename.txt 50 89
rangecp filename.txt 50
Prince Kumar
  • 331
  • 2
  • 4
1

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

  1. Uninstall vim using sudo apt remove vim. (I was too lazy to find how to re-compile it with the +clipboard support.
  2. Install a different vim package called vim-athena using sudo apt install vim-athena that ships with +clipboard.
  3. Add to ~/.vimrc the following line: set clipboard=unnamedplus.
  4. Source the file by entering command mode and typing source %.
  5. Save and exit.

Note: I am using Ubuntu 20.04.

alexandrosangeli
  • 262
  • 2
  • 13
0

selecting with the the help of the mouse and right-click copy worked in my case.

I didn't want the line numbers included so I :set nonumber before copying.

Neithan Max
  • 11,004
  • 5
  • 40
  • 58
Aakash
  • 21,375
  • 7
  • 100
  • 81
0

Nothing above worked for me on my windows laptop.

Ctrl+C was fine for copying.. but I needed Shift+Insert to Paste !

(A good reason to always get a laptop where Insert can always be accessed without pressing a secondary key)

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
0

In linux with gnome (ubuntu, xubuntu etc) install vim-gnome and you will be able to use VISUAL to select and then Ctrl + C normally and paste in other applications with Ctrl + V

sudo apt install vim-gnome
Sergio Abreu
  • 2,686
  • 25
  • 20
0

For Mac OS X, in the terminal:

  • run vim --version | grep clipboard to check if the clipboard is enabled(with +)

vim --version | grep clipboard

  • add the code below into the .vimrc file

if has("clipboard")
  set clipboard=unnamed " copy to the system clipboard

  if has("unnamedplus") " X11 support
    set clipboard+=unnamedplus
  endif
endif
  • restart terminal & vim

reference: Setting up Vim to yank to clipboard on Mac OS X

Feuda
  • 2,335
  • 30
  • 28
-1

if you connect to the unix system thru putty then follow the steps below to copy content of a file

  • Highlight text you want to copy ( you'll be able to copy page by page )
  • press ctrl+c ( it'll copy the text to clipboard
  • paste in any external editor Done
Pratik
  • 337
  • 2
  • 8
-1

I want to supplement a way to copy the line to the clipboard and use it on the function.

here is an example that you open the URL in the browser

let g:chrome_exe = 'C:/.../Google/Chrome/Application/chrome.exe'
function OpenURL()
    " copy the line and put it in the register *
    normal "*yy
    :execute "silent !start ".g:chrome_exe." ".getreg("*")
endfunction

map ,url :call OpenURL()<CR>
Carson
  • 6,105
  • 2
  • 37
  • 45
-2

Shift+Ctrl+C if you are in graphical mode of Linux, but first you need to select what you need to copy.

enter image description here

prosti
  • 42,291
  • 14
  • 186
  • 151