9

I'm using Linux system. Two vims are opened in two terminals. Now I'd like to copy text from A to B. One way to do so is to select text in A, use mouse to copy, then switch to B, in the insert mode, command <c-r>+ will paste the text.

Is there anyway to perform it without using mouse? like "+yy in A, when switch to B, o<c-r>+? I explored couple of combinations in vain. And I searched for "copy across terminals", but find nothing.

user @statox suggests one solution using visual mode, i.e. v$"+y in A, when switch to B, o<c-r>+.

Is there any simple way that can copy the text in A without entering into v mode? Admittedly entering into v-mode does not involve too many key strikes, just wonder if there is better way. Eg. if the cursor is at the middle of a line, yy copies the whole line (to register "), while 0v$y does the same thing with extra key stroke.

Or, my question can be reduced to if there is any way with fewer key strokes than 0v$"+y for copy such that I can use <alt-tab>o<c-r>+ to paste.

  • What is your exact problem? You seem to have the solution: select you text with visual mode (why would would use the mouse?) put it in the clipboard register with "+y and in the other vim instance put the clipboard register "+p. Or duplicate of this? – statox Sep 2 '15 at 0:55
  • You don't have to enter visual mode to yank something. You can follow y with a motion (:help motion.txt) or an object (:help object-select). If you yank and paste across Vim instances often, you may find that setting clipboard to include unnamed or unnamedplus useful. See :help 'clipboard'. Then copying between instances should just work. – garyjohn Sep 2 '15 at 2:14
  • 1
    The point of my comment wasn't the visual mode, actually I have trouble to understand your problem: at first I thought it was the fact to copy some text from one terminal to another, now it seems that it's about using as few keystrokes as possible to yank into the clipboard register. If that is the second case the comment of Garyjohn is relevant. – statox Sep 2 '15 at 3:53
  • 1
    0v$ is the same as V. And it's easy to make a mapping for "+y: :nnoremap <Leader>y "+y. So that reduces it to V\y. – Martin Tournoij Sep 2 '15 at 9:18
  • @Carpetsmoker your solution is good. I'd like to take it as the optimal solution if you paste it to the answer region. It seems that without key map, the best we can do for the copy in my question is V"+y, 4 keystrokes. – user3813057 Sep 2 '15 at 13:05
8

You can check if Vim is built with clipboard support with:

:echo has('clipboard')

If the output is 1, the following should be enough to achieve your goal:

"*y    in source Vim
"*p    in destination Vim

You can make it all a lot more seamless by synchronizing the clipboard register and the default register with this setting in your vimrc:

set clipboard^=unnamed

If the output of :echo has('clipboard') is 0, consider installing a proper Vim like vim-gtk or vim-gnome (the name of the package may differ from distro to distro).

1

I think the solution you already mention in your question is the correct one. If you want to reduce the number of keystrokes, you can simply create a map, e.g.

vmap <leader>y "+y
vmap <leader>d "+d
nmap <leader>p "+p
nmap <leader>P "+P
vmap <leader>p "+p
vmap <leader>P "+P

See the second entry here.

1

To extend @romainl's answer, (with clipboard support) you can use registers that are shared with system wide clipboard.

"+y    To yank/copy from a vim-terminal
"+p    To paste in destination vim-terminal

"*y    To yank/copy from a vim-terminal
"*p    To paste in destination vim-terminal

By executing these commands, you are yanking/copying/storing selected contents in registers, and pasting them to destination Vim-terminal. These two special registers + and * are shared with the system wide clipboard (and hence many other programs such as web browser, gedit etc). Register + is associated with copy/paste buffer and * is associated with selection buffer (one that inserts a copy of selection with a middle click on a text field in, say, Ubuntu) and can be pasted using ctrl + v (ctrl + shift + v on gnome-terminal).

1

1) On Fedora I use the following setup:

dnf install vim-X11

vim-X11 comes with clipboard support - maybe in other distros it is similar.

2) Set an alias in my .bashrc

alias vim="vimx"

3) In .vimrc:

set clipboard=unnamedplus

Now the usual yank in Vim goes into the system clipboard and every Ctrl-C outside of vim can be pasted in vim.

0

If you are using a Mac(OSX), you can install bpcopy(using brew install pbcopy --see http://brew.sh/ for more details). Then run the following(straight form commandline) without touching the mouse:

EXAMPLE:

1) In TERMINAL# 001:

cat /tmp/stuff.txt | pbcopy

2) You can switch to terminal #002 (without using the mouse) by using the built-in hot keys in OSX's terminal application: command + i

3) In TERMINAL# 002: Open the new file in vim

vi newfile

4) Put vim in insert mode with i

5) Insert the text from the clipboard with command + V (to paste from the clipboard on a Mac)

6) Enjoy the rest of your day!

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.