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.

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
name
  • 965
  • 2
  • 8
  • 8

6 Answers6

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.

alxndr
  • 1,346
  • 1
  • 15
  • 26
Steve Vermeulen
  • 1,067
  • 11
  • 13
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>
  • 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 Feb 28 '23 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
A B
  • 387
  • 2
  • 9
  • @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 00:21
  • 5
    @name http://vi.stackexchange.com/questions/84/how-can-i-copy-text-to-the-system-clipboard-from-vim/96#96 - though I think that requires X-forwarding if you're on SSH. – muru Feb 20 '15 at 00: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
  • @AB "if you're on SSH" – muru Feb 21 '15 at 23:28
  • @muru (I think that) X-forwarding is only required if you want to use your local system clipboard over SSH. You can still use the remote machine's system clipboard from within Vim without it. – Rich Nov 30 '18 at 15:41
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

  • 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 '21 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 Feb 12 '21 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 – Athos Tavares Feb 13 '21 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 Feb 13 '21 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.

guest
  • 1
  • 1
-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,]).

kenorb
  • 18,433
  • 18
  • 72
  • 134