6

I use gnu screen and I run vim file1.txt and vim file2.txt in two windows. How can I copy part of the text from file1.txt and paste it to file2.txt without using temporary files or opening two files under the same vim instance?

Basically I would like to yank in first window and paste in second one. I need shared clipboard.

7
  • Do you consider to open file1.txt and file2.txt using one instance? E.g. vim file1.txt file2.txt?
    – kenorb
    Feb 20, 2015 at 12:30
  • 1
    @kenorb no. It's written in the question.
    – name
    Feb 20, 2015 at 12:44
  • 1
    In tmux, you can start "copy mode" with prefix+[, then space to select stuff, Enter` to copy, then go to the other pane, and use prefix+] to paste ... It's very similar to Vim's visual mode. IIRC screen can do this as well (but I don't remember how, as I haven't used it in years and years). Feb 20, 2015 at 13:21
  • On which operating system? Feb 20, 2015 at 15:53
  • 1
    @hippietrail linux, no gui.
    – name
    Feb 21, 2015 at 20:07

5 Answers 5

9

One way is to just copy it to the system clipboard from the first instance, then copy it from the system clipboard in the second instance. How exactly you would do this depends on your OS and also your vim clipboard setting.

Another option is to use vim-easyclip which has the ability to share one clipboard across all vim instances (including sharing a history of yanks as well). Internally what it does is mirror your clipboard to a temporary file, so it bypasses using your system clipboard entirely.

1
  • 1
    +1 for vim-easyclip
    – nn0p
    Sep 15, 2017 at 5:51
2

I usually end up using xsel to copy to/from the system clipboard:

vmap <leader>y !xsel -i -b && xsel -b <CR>
nmap <leader>p :r !xsel -b <CR>
1

It really depends on your environment. Basically, we're talking about inter-process communications and this is very much OS specific.

You can use:

  1. System clipboard
  2. Tmux or screen buffer
  3. tmp file
7
  • @A B I am on Linux, I don't want to use gnu screen buffer or tmp file. How do I use system clipboard from vim?
    – name
    Feb 20, 2015 at 0:21
  • 5
    @name vi.stackexchange.com/questions/84/… - though I think that requires X-forwarding if you're on SSH.
    – muru
    Feb 20, 2015 at 0:22
  • @name Please define system clipboard
    – A B
    Feb 21, 2015 at 22:44
  • @muru this is only assumption that screen is remote. Many folks are using screen/tmux - locally
    – A B
    Feb 21, 2015 at 22:46
  • @AB I work remotely ssh + screens + vims. I am fine with using remote's host system clipboard and not the client's one.
    – name
    Feb 21, 2015 at 22:59
0

You could solve that by just installing gvim, the grafical version of vim, you don't have to use it or anything. It solves this problem and lets you even put stuff you copied outside vim.

edit: Install gvim and put set clipboard=unnamedplus on your vimrc

4
  • 1
    Welcome to this site! I am really not sure your answer is right: Could you explain how installing gVim would solve the issue of sharing a clipboard between two different vim instances?
    – statox
    Feb 12, 2021 at 13:02
  • I think installing Gvim on some systems enables the +cliipboard feature, allowing the usage of the "*register. I can't test it right now though.
    – Biggybi
    Feb 12, 2021 at 14:04
  • Yes, by installing gvim you enable vim to copy from the +clipboard. But I answer was incomplete, you should also put set clipboard=unnamedplus on your vimrc Feb 13, 2021 at 10:48
  • That's only if you want Vim to copy to the "+ register by default. Without it, you can still use "+yy, for example to copy a line to the system clipboard. If you truly want your answer to be complete, you could explain 1/ why install Gvim 2/ how to use the "+ register 3/ what set clipboard=unnamedplus does.
    – Biggybi
    Feb 13, 2021 at 11:31
-1

In ideal situation it would be great if you could open two files under one instance:

vim file1.txt file2.txt

If not possible, then you can still do it if the files are accessible on the same host, by opening 1st file and:

  1. Copy text in file1.txt (e.g. enter visual mode V, select the text, and yank/copy them by y).

  2. Open 2nd file by :Sex.

    You may also use: :e file2.txt, then if you need to use full path, go to 2nd instance and check by 1,Ctrl+g.

  3. Paste your text (e.g. p).

Above steps can be done opposite, by opening file2.txt, then file1.txt to copy the text, back to the original file and paste it there.

Or simply use screen buffer to copy and paste between screen windows (Ctrl+a,[ and Ctrl+a,]).

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.