51

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
  • 14,064
sam
  • 6,921

7 Answers7

55

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,829
27

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
  • 371
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

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!

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
  • 207,228
FuzzyAmi
  • 141
  • 3
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
  • 44,353