41

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

49

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.

  • 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
  • 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 5 '17 at 9:59
  • "*yy, "*p also works. if you hit :reg, you will see that star registry – elquimista Apr 12 '18 at 7:35
20

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.

  • 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 4 '16 at 23:13
9

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

  • simple straightforward solution - the old fashion way - thanks this did it for me – serup Jan 5 '17 at 10:04
  • What I need to type in Vim after adding this to the .vimrc? – Vitaly Zdanevich Apr 14 '17 at 6: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 7:28
  • 1
    This is the one works. using "*y seems to be broken on servers – ospider Sep 21 '17 at 4: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 at 1:56
5

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!

2

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.

  • 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. – Ray Aug 20 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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