How can I copy text to the system clipboard from Vim?

  • Is there a way to copy a block of text to the system clipboard, so I can paste it in another program?

    This has already been asked and answered on SO. You may find more and different information there.

    I have been using this plugin on macvim christoomey/vim-system-copy and really love it. It works really well with motions `cpip`, `cpi{`.

    What you really need is EasyClip. It will do just that and so much more...

    I can not add answer but a comment. If you only want to copy a text you can also use `sed` :). `sed -n 5p file`; only 5th line. `sed -n -e 5p -e 8p file`; only 5th and 6th lines. `sed -n 5,8p file`; lines between 5th and 8th. It will print the lines in console. You can easily copy by selecting text.

  • For X11-based systems (ie. Linux and most other UNIX-like systems) there are two clipboards which are independent of each other:

    • PRIMARY - This is copy-on-select, and can be pasted with the middle mouse button.
    • CLIPBOARD - This is copied with (usually) ^C, and pasted with ^V (It's like MS Windows).

    OS X and Windows systems only have one clipboard.

    For X11 systems there are also number of tools that synchronize these clipboards for you; so if they appear to be the same, you may have one of them running.

    Vim has two special registers corresponding to these clipboards:

    • * uses PRIMARY; mnemonic: Star is Select (for copy-on-select)
    • + uses CLIPBOARD; mnemonic: CTRL PLUS C (for the common keybind)

    On Windows & OS X there is no difference between + and *, since these systems only have a single clipboard, and both registers refer to the same thing (it doesn't matter which one you use).

    You can use these registers as any register. For example, using the PRIMARY clipboard * with the y and p commands:

    • "*yy
    • "*p

    You could maybe use this as more convenient keybinds:

    noremap <Leader>y "*y
    noremap <Leader>p "*p
    noremap <Leader>Y "+y
    noremap <Leader>P "+p
    

    If you want to "automatically" interface with the system's clipboard instead of referring to it manually all the time, you can set the clipboard variable:

    • Set it to unnamed to use * (PRIMARY, on select)
    • Set it to unnamedplus to use + (CLIPBOARD, ^C)

    Now, just using yy will go to the system's clipboard, instead of Vim's unnamed register, and p will paste the system's clipboard.

    You can also assign to these registers just like any register with let:

    • :let @+=42
    • :let @*=42

    The clipboard setting has some more options (such as exclude filters); but these are the basics. See :help 'clipboard' for the full story ;-)

    gVim

    If you use gVim, you can get copy-on-select behaviour when using :set guioptions+=a.
    This is enabled by default on X11 systems (copies to PRIMARY), but not on MS Windows & OSX (as selecting any text would override your clipboard).

    No +clipboard?

    Vim requires the +clipboard feature flag for any of this to work; you can check if your Vim has this by using :echo has('clipboard') from within Vim (if the output is 0, it's not present, if it's 1, it is), or checking the output of vim --version for +clipboard.

    Most Linux distributions ship with a "minimal" Vim build by default, which doesn't have +clipboard, but you can usually install it:

    • Debian & Ubuntu: Install vim-gtk or vim-gnome.
    • Fedora: install vim-X11, and run vimx instead of vim (more info).
    • Arch Linux: install gvim (this will enable +clipboard for normal vim as well).

    You could also use xclip, xcopy, or xsel to copy text to the clipboard; see the following questions for solutions:

    SSH

    You can also use a clipboard on remote machines if you enable X11 forwarding over SSH. This is especially useful with the above tip since you can then use xclip to access your desktop's clipboard. The Vim on the machine you're ssh-ing to will still need the +clipboard feature.

    This requires the ForwardX11Trusted setting, and should only be done with trusted servers, as this gives the server almost complete control over your X11 session:

    $ ssh -XY myhost
    

    To make these settings persistent (so you don't need to add -XY every time), you could do something like this in your ~/.ssh/config:

    # Do **NOT** set this globally; it gives the server complete control over
    # your X11 session.
    Host myhost
        ForwardX11 yes
        ForwardX11Trusted yes
    

    Neovim

    Neovim revamped the clipboard support. The built-in interface was removed and replaced with a system that call an external utility such as xclip, xsel, or pbcopy/pbpaste.

    It should automatically pick up these utilities and use them. On OS X pbcopy and pbpaste should be available by default, on Linux you probably want to install xclip, as that's the most widely available (there are actually two versions of xsel with incompatible flags. This is really stupid).

    Also see :help clipboard in Neovim.

    ssh -Y implies -X so -X can be omitted.

    For some international keyboards, you may need to press `"` to get a `"`. So in those case you would have to press `"+y` or `"*y` to copy.

    A very interesting side effect of learning this (a few years ago) for me has been the realization that `"` can select many named registers. `"+` is not magic, it's just register `+`. So for example, if you want to copy three things at the same time and paste each of them one by one, you can `"1y`, `"2y` and `"3y` and later `"1p`, `"2p` and `"3p`. This is really powerful and non-existent in almost any other editor. Another interesting usage is to look at stored macros. For example, if you record with `qq`, but you realize you need to fix it, you can: in a temp line `"qp`, fix it, and `"qd`.

    Just a note: OS X has two separate clipboards. One is accessed using Cmd-C and Cmd-V and the other using the emacs keybindings Ctrl-K and Ctrl-Y.

    @shahbaz -- probably better to use the letter registers for such a purpose, since the numbered ones get changed whenever text is deleted.

    @evilsoup, good to know! I usually use `y`, `u`, `i`, `o`, `p` (for no reason), but thought to use more meaningful letters for the sake of the comment. Clearly not a good choice.

    as thorough as this looks, i cant figure out how to copy from vim, on a split screen (horizontal) so i can only copy from one screen, using visual block. I tried adding the `noremap`s and the `set clipboard=unpamedplus` but cant figure out how to use it. am i supposed to use visual block, press the `+` key? Can you elaborate on how to use this?

    I think you're looking for the `mouse` setting @blamb? Try `:set mouse=a`. Also see `:help 'mouse'` and `:help mouse-using`.

    if you think that will work as an alternative thats one thing, but i was trying to copy text, without mouse, but using visual block, then press a key on my keyboard (a key `+` being easier to debug key combos, e.g. `"*yy`) then i should be able to paste using normal means into another application. Im open for anything, as long as i can get one panes worth of data into the clipboard (using gnu screen to make matters more difficult)

    I'm not entirely sure if I follow what you're trying to do @blamb, or what exactly isn't working. I assumed you're already using the mouse by the way you phrased the question, but clearly I misunderstood that. I blame the Elvis juice ;-) I think it might be best if you post a new question which goes in to a bit more detail.

    @hildred for me (on macOS Mojave), I had to use `-XY`, `-Y` alone was not enough.

    How could I enable copy paste functionality from a Neovim code editor to a python interpreter sheet opened in a split window (I am using Byobu)? I am using Neovim as my code editor on the web-browser based SSH shell on Google Compute Engine

    since the standard MacOS vim doesn't support + / * registers you can alias your vim to MacVim's vim bin here (has same support as the gui) /Applications/MacVim.app/Contents/bin/vim

    Better mnemonic: `+` ( like a cross ) is how it crosses into your operating system

    Copy & paste as described in the question is meant for desktop. It is true that installing package `gvim` alongside `vim` gives you clipboard functionalities. but in file browsers you are then offered two text editors to open files! And this looks ugly. This is why it is better to install only packages `vim` and `vim-gui-common`. Later one provides the clipboard functionalities without the need for installing `gvim`. This is on Linux Debian 10.

    The instructions here for X11 only function if vim is compiled with the `+clipboard` option, which is many distros (e.g. Debian) is not the case.

License under CC-BY-SA with attribution


Content dated before 6/26/2020 9:53 AM