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?

4 Answers 4


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?

  • WOW thanks a lot for all this information. Just wondering, but what do you mean by "clipboard support" and "system clipboard"? Jan 6, 2012 at 0:32
  • Also, if you could let me know more about the Vim split-window thing that'd be really really helpful Jan 6, 2012 at 0:32
  • 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
    Jan 6, 2012 at 6:17
  • 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.
    – romainl
    Jan 6, 2012 at 6:23

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

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.

  • That should work, too, but to preserve indentations, you'll need to execute :set paste in the target Vim before pasting.
    – garyjohn
    Jan 3, 2012 at 22:53
  • Shift+"insert"? do you mean the button "insert"? Jan 4, 2012 at 0:56
  • Correct; the insert button.
    – user111228
    Jan 4, 2012 at 15:10

There are several pieces to this puzzle that must work-together to make copy/paste work.

First, the "terminal" must have a way to exchange data from one session to another... Most of the time, this is handled by a GUI manager of some sort... (XORG/Windows/etc...) which implements some sort of "clipboard" functionality.

Second, the "terminal" application itself must be able to interact with whatever clipboard interface exists... to keep all "terminals" in sync...

Third, the shell (bash/ash/dash/csh/etc...) needs to be able to access the terminal's clipboard APIs...

Forth, VIM needs to make use of the clipboard APIs provided by the shell it's running in.

Any one of those failures can mean that your application can't make use of any sort of clipboard between terminals.

All that being said... there are some "standardized" API calls for the clipboard... so assuming each bit has done it's job properly, it should work.

Now... there are some handy tricks to simply get the job done without worrying about all the clipboard crap... which is probably what you want to begin with...

VIM will let you copy text and export it to a file and then on another console import the text from that file, and here's how:

First: make sure you're not in "edit" mode... (press escape key) and move the cursor to the place you want to start copying and press the v key. Go to the end of what you want to select... and press y.

If you want to paste a copy in the same document at this point... just move the cursor to wherever and press p ... but since we're copying to another terminal, we'll need to open a new file someplace else to paste into... like this:

:e ~/temp-file

then press p to paste the text... :w to write it. then do a :bp to close that temporary file & go back to the file you were working on...

Now, on the other terminal, simply go to the spot in the file you want to paste into (not in edit mode)... and do a :r ~/temp-file to read the contents of that file into your current document. When you're all done... make sure to delete the temp file.

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.