5

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.

  • Do you consider to open file1.txt and file2.txt using one instance? E.g. vim file1.txt file2.txt? – kenorb Feb 20 '15 at 12:30
  • @kenorb no. It's written in the question. – name Feb 20 '15 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). – Martin Tournoij Feb 20 '15 at 13:21
  • On which operating system? – hippietrail Feb 20 '15 at 15:53
  • 1
    @hippietrail linux, no gui. – name Feb 21 '15 at 20:07
8

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 for vim-easyclip – nn0p Sep 15 '17 at 5:51
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
  • @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 '15 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 '15 at 0:22
  • @name Please define system clipboard – A B Feb 21 '15 at 22:44
  • @muru this is only assumption that screen is remote. Many folks are using screen/tmux - locally – A B Feb 21 '15 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 '15 at 22:59
1

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

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.