Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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

share|improve this question

4 Answers 4

up vote 15 down vote accepted

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.

share|improve this answer
    
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 6 '11 at 16:42
    
I don't want to install Gvim. How do I do this in vim? –  trusktr Mar 22 '13 at 22:32
    
@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

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

just put:

set clipboard=unnamedplus

in your .vimrc.

share|improve this answer

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.

share|improve this answer

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!

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.