How to copy data between different instances of vim?
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 , 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:
- Use
ESC m a
to mark first line - Use
ESC m b
to mark last line - Use
:'a,'b w! xfer
to write out range to a scratch file
In destination session:
- Move cursor to insertion line
- 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
.
Related videos on Youtube
Comments
-
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 (withp
)? 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:
I do not want to exit the first vim and reopen the second vim.
I do not want to use separate window (with
:sp
).
-
trusktr about 11 yearsI don't want to install Gvim. How do I do this in vim?
-
jbrock over 7 yearsI 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 over 7 yearsthis 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 over 7 yearssimple straightforward solution - the old fashion way - thanks this did it for me
-
Vitaly Zdanevich about 7 yearsWhat I need to type in Vim after adding this to the .vimrc?
-
Samir Sadek about 7 yearsYou 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 over 6 yearsThis is the one works. using
"*y
seems to be broken on servers -
elquimista about 6 years
"*yy
,"*p
also works. if you hit:reg
, you will see that star registry -
Tommy about 5 yearsoh 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 about 5 yearsthis seems to insert one extra blank line before the pasted text. How can I fix that?
-
Jim over 4 yearsThis 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 about 4 yearsthe
vim-gtk
package includes a clipboard enabled version of (terminal) vim:sudo apt install vim-gtk
-
ryanjdillon about 3 yearsI 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 about 3 yearsBoth
vim 8.2
over connections such asssh
andtmux
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 invmap "+y :w! /tmp/filename<CR>
here be dragons. -
Admin almost 2 yearsThis is genius, works perfectly on servers without clipboards. It only works by lines but that's good enough. You can also simulate
P
withnmap <leader>P :-1r /tmp/vitmp<CR>
(! cat
is redundant).