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?
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?
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
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 "
If your Vim doesn't have those features but you have gvim installed, one solution is to run gvim in terminal mode as
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
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
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:
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.