11

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
    Commented Feb 20, 2015 at 12:30
  • 1
    @kenorb no. It's written in the question.
    – name
    Commented 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). Commented Feb 20, 2015 at 13:21
  • On which operating system? Commented Feb 20, 2015 at 15:53
  • 1
    @hippietrail linux, no gui.
    – name
    Commented Feb 21, 2015 at 20:07

6 Answers 6

11

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
    Commented Sep 15, 2017 at 5:51
4

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
  • This is great! However, I didn't like that the "yank" mapping replaces the selection with itself; it counts as a change in the undo tree, and flags the file as modified even if you only yanked something. I ended up with :vmap <leader>y :w !wl-copy <CR> <CR> to get rid of the redundant change. This way, the command is "write-only". The extra CR gets rid of vim pausing to show you the command output. Change wl-paste to xsel -i -b for X11 I suppose.
    – marcelm
    Commented Feb 28, 2023 at 18:01
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
    Commented 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
    Commented Feb 20, 2015 at 0:22
  • @name Please define system clipboard
    – A B
    Commented Feb 21, 2015 at 22:44
  • @muru this is only assumption that screen is remote. Many folks are using screen/tmux - locally
    – A B
    Commented 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
    Commented 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
    Commented Feb 12, 2021 at 13:02
  • 1
    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
    Commented 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 Commented Feb 13, 2021 at 10:48
  • 1
    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
    Commented Feb 13, 2021 at 11:31
0

I use a temporary file:

Put these lines in .vimrc for both users:

function! Write2Tmp()
    silent! '<,'>w! /tmp/tmp.txt
    call system('chmod 0777 /tmp/tmp.txt > /dev/null 2>&1')
    let g:amount = line("'>") - line("'<") + 1
endfunction

map <silent> <leader>y :call Write2Tmp()<CR>:echo g:amount." lines yanked"<CR>

map <silent> <leader>p :silent! r /tmp/tmp.txt<CR>

First enter visual mode (Shift-V) to select the lines you want to copy. Then use <leader>y and <leader>p to copy and paste between files.

-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 and acknowledge you have read our privacy policy.

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