50

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

Jjed
  • 13,874
sam
  • 6,831

7 Answers7

54

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.

Jeremy Kerr
  • 27,199
  • For whatever it's worth, if you install GVim (the package might be called something like vim-gnome), the menu entries will very often give you the key shortcuts they will invoke. – zpletan Sep 06 '11 at 16:42
  • 1
    I don't want to install Gvim. How do I do this in vim? – trusktr Mar 22 '13 at 22:32
  • 1
    @trusktr actually this works in vim too. In this Q&A it says you need at least vim 7.3.74 (which means Ubuntu 11.10 or later). – lumbric Mar 28 '13 at 13:43
  • 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 Jan 05 '17 at 09:59
  • "*yy, "*p also works. if you hit :reg, you will see that star registry – elquimista Apr 12 '18 at 07:35
  • the vim-gtk package includes a clipboard enabled version of (terminal) vim: sudo apt install vim-gtk – Niklas Apr 08 '20 at 10:01
25

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.

bhh1988
  • 351
  • 1
    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. – jbrock Aug 04 '16 at 23:13
19

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

Samir Sadek
  • 288
  • 2
  • 4
  • 1
    simple straightforward solution - the old fashion way - thanks this did it for me – serup Jan 05 '17 at 10:04
  • What I need to type in Vim after adding this to the .vimrc? – Vitaly Zdanevich Apr 14 '17 at 06:22
  • 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. – Samir Sadek Apr 15 '17 at 07:28
  • 2
    This is the one works. using "*y seems to be broken on servers – ospider Sep 21 '17 at 04:48
  • 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 Mar 13 '19 at 01:56
  • this seems to insert one extra blank line before the pasted text. How can I fix that? – Tommy Mar 13 '19 at 02:01
  • I just had this issue with WSL without an X server, and I started looking how to do exactly this, and BAM! Thank you :) – ryanjdillon Apr 19 '21 at 12:17
  • 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. – Eric Leschinski Apr 21 '21 at 19:24
  • 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). – DuBistKomisch Jun 10 '22 at 02:47
9

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!

gobernador
  • 103
  • 4
Bill
  • 91
4

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.

muru
  • 197,895
  • 55
  • 485
  • 740
FuzzyAmi
  • 141
  • 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. – Jim Aug 20 '19 at 22:01
2

You will probably all hate this answer. I used to work in vi back in the 80's on a variety of Unix computers and have had the recent occasion to play around in Linux.

My solution to cut and paste is using Putty running on Windows to access my Linux box.

Putty lets you copy any txt in it's window by simply highlighting it. you can then go to another Putty window/session and right-click to paste.

couldn't be easier. and now there's vim. too bad that wasn't around back in my day, it would have helped me fend of the emacs mafia.

1

It's not exactly using yy, but if you select the text you want to copy with mouse (sometimes you may need to use Shift-[drag]), switch to the other terminal window and do a [middle click] or Shift-[middle click] there, the text will be inserted at your current insertion point.

This works for most command-line and GUI programs, not only for vim.

Sergey
  • 43,665