How to copy data between different instances of vim?

86,426

Solution 1

First you must install a clipboard-enabled version of vim. To accommodate users with non-graphical environments (eg Ubuntu Server users), vim and vim-tiny do not hook into this capability. You'll want to install GVim Install GVim, which be launched either graphically or in your terminal. GVim will replace the vim command.

You can yank the text into the 'cut buffer' from the first vim, and then paste the text into the second vim. To copy to the global cut buffer, use the + register. The " key allows you to specify which register to use when doing a yank or paste operation.

In the first vim, yank a line into the + register:

"+yy

then, to paste from the + register in the second vim:

"+p

For more information on the registers available, check out :help registers. You can also use "+p to paste text copied from any other source on your system.

Solution 2

Best solution that worked for me (that doesn't require me to change my keybinding habits) is here: https://stackoverflow.com/questions/9166328/how-to-copy-selected-lines-to-clipboard-in-vim

just put:

set clipboard=unnamedplus

in your .vimrc.

Solution 3

I like the solution of Bill, and I have created a mapping:

vmap <leader>y :w! /tmp/vitmp<CR>                                                                   
nmap <leader>p :r! cat /tmp/vitmp<CR>

the first one in visual mode copy all in /tmp/vitmp and the second one copy the content from /tmp/vitmp in the file

Solution 4

I move around between various Unix-family machines and have found the following sequence always works for me:

In source vi session:

  1. Use ESC m a to mark first line
  2. Use ESC m b to mark last line
  3. Use :'a,'b w! xfer to write out range to a scratch file

In destination session:

  1. Move cursor to insertion line
  2. Use ESC :r xfer to read in text

I know it's not pretty but it always works for me!

Solution 5

Its actually quite simple: install a version of vim that supports clipboard. if your vim does not, get any one of the following:

sudo apt-get install vim-athena
sudo apt-get install vim-gnome
sudo apt-get install vim-gtk

Once installed, just run vim. You can also verify that clipboard is enabled by running:

 vim --version|grep clipboard

you should see +xterm_clipboard.

Share:
86,426

Related videos on Youtube

keviveks
Author by

keviveks

I love Linux &amp; Opensource!!

Updated on September 18, 2022

Comments

  • keviveks
    keviveks over 1 year

    If I use the first vim to copy a line (with yy), how do I paste to another terminal's vim (with p)? Is there any command or settings can do it? Can I copy and paste into the global system clipboard?

    I know the following ways are possible, but I want a simpler one:

    1. I do not want to exit the first vim and reopen the second vim.

    2. I do not want to use separate window (with :sp).

  • trusktr
    trusktr about 11 years
    I don't want to install Gvim. How do I do this in vim?
  • jbrock
    jbrock over 7 years
    I actually needed to add set clipboard=unnamedplus in combination with having GVim installed. However, I was able to use xfce4-terminal for Vim instead of the actual GVim program.
  • serup
    serup over 7 years
    this is somehow not working if you have a vim started inside tmux and another vim started normally outside -- any chance you know how to go around this issue?
  • serup
    serup over 7 years
    simple straightforward solution - the old fashion way - thanks this did it for me
  • Vitaly Zdanevich
    Vitaly Zdanevich about 7 years
    What I need to type in Vim after adding this to the .vimrc?
  • Samir Sadek
    Samir Sadek about 7 years
    You have to set up vim so that you can use the leader key follow by y to copy, leader key followed by p to paste.
  • ospider
    ospider over 6 years
    This is the one works. using "*y seems to be broken on servers
  • elquimista
    elquimista about 6 years
    "*yy, "*p also works. if you hit :reg, you will see that star registry
  • Tommy
    Tommy about 5 years
    oh my. Ive been searching for hours on this issue by using vim in tmux in docker. My normal vimrc which already had unnamed/unnamedplus, did NOT work in vim in tmux in docker. Neither did anything I found on the internet. However THIS seems to work on ALL machines! In docker, on Mac, and on Arch natively. Thank you!!
  • Tommy
    Tommy about 5 years
    this seems to insert one extra blank line before the pasted text. How can I fix that?
  • Jim
    Jim over 4 years
    This answer gets to the heart of the problem which is, does your version of Vim have the clipboard feature enabled? If it doesn't, non of these other answers will make any difference. Do "vim --version" and look for "+clipboard". I think +xterm_clipboard only applies if your want X11 clipboard support but if you're on Ubuntu you'll want this too.
  • Niklas
    Niklas about 4 years
    the vim-gtk package includes a clipboard enabled version of (terminal) vim: sudo apt install vim-gtk
  • ryanjdillon
    ryanjdillon about 3 years
    I just had this issue with WSL without an X server, and I started looking how to do exactly this, and BAM! Thank you :)
  • Eric Leschinski
    Eric Leschinski about 3 years
    Both vim 8.2 over connections such as ssh and tmux try to be smarter than they ought to be, and somewhere along the way they fight each other over ownership and management of the system's clipboard and vim's internal register management system. This answer worked for me with some edits, as in vmap "+y :w! /tmp/filename<CR> here be dragons.
  • Admin
    Admin almost 2 years
    This is genius, works perfectly on servers without clipboards. It only works by lines but that's good enough. You can also simulate P with nmap <leader>P :-1r /tmp/vitmp<CR> (! cat is redundant).