Vim: Terminal-to-terminal copy&paste?

12,501

Solution 1

A lot of things are missing from your question: Which terminal emulator do you use? On which platform? Is Vim compiled with clipboard support?

I'm on Ubuntu, running Vim 7.3 with clipboard support in Gnome Terminal so YMMV.

I'm able to select a block of code with V in one Vim instance in one terminal window and put it in another Vim instance in another terminal window simply with a middle-click. Without the mouse I'm able to yank the selection to the system clipboard with "*y in the first window and put with "*p in the second window.

You might need to do :set paste before pasting your code block and :set nopaste after.

Also, why do you use two different Vim instances in two different terminal windows when Vim has split windows buit-in?

Solution 2

You should be able to copy from one terminal holding Shift while marking with the mouse and pasting to the other terminal with either middle-click or Shift+insert.

Solution 3

If you're using a standard terminal such as GNOME Terminal or xterm and a properly-configured Vim, this should just work. That is, you should be able to use your mouse to select the text you want to copy, then click the middle mouse button in the target Vim and the text will be pasted.

If the block is too large to be easily selected with the mouse, you can select the text from the keyboard instead. Move the cursor to the first line of the block, type V, move the cursor to the bottom of the block and type "*y. Then you can paste that text either with the mouse as before, or by typing "*p, or by executing

:put *

If this doesn't just work, the most likely reason is that the Vim you're using in a terminal was not compiled with support for X, so it can't communicate with the X server. You can find this out by executing

:echo has("x11") has("xterm_clipboard")

If your Vim was compiled with both of those features, the result will be "1 1".

If your Vim doesn't have those features but you have gvim installed, one solution is to run gvim in terminal mode as

gvim -v
Share:
12,501

Related videos on Youtube

Dark Templar
Author by

Dark Templar

Updated on September 18, 2022

Comments

  • Dark Templar
    Dark Templar over 1 year

    I am trying to copy a large block of code (with indentations preserved) from one vim terminal into another. I was wondering if there was a straightforward way to do this?

    Would this be considered a vim functionality, or a property of the terminal?

  • garyjohn
    garyjohn over 12 years
    That should work, too, but to preserve indentations, you'll need to execute :set paste in the target Vim before pasting.
  • Dark Templar
    Dark Templar over 12 years
    Shift+"insert"? do you mean the button "insert"?
  • Admin
    Admin over 12 years
    Correct; the insert button.
  • Dark Templar
    Dark Templar over 12 years
    WOW thanks a lot for all this information. Just wondering, but what do you mean by "clipboard support" and "system clipboard"?
  • Dark Templar
    Dark Templar over 12 years
    Also, if you could let me know more about the Vim split-window thing that'd be really really helpful
  • romainl
    romainl over 12 years
    Vim can be compiled with or without clipboard support: if "with", Vim is able to talk to the system's clipboard and you can yank/put stuff in and out of Vim to/from other apps. If "without", well you can't do any of it. You can check what options were used at compile time with :version: +option means it's available, -option means it's not. Try :help clipboard for more info.
  • romainl
    romainl over 12 years
    Windows in Vim is too deep a topic for a SU comment. <C-W>s or :split will split your window in two horizontally and <C-W>v or :vsplit will split your window in two vertically. Use <C-W>hjkl to navigate between window and so on. As usual, :help window is your friend.