I am working with two files, I need to copy a few lines from one file and paste into another file. I know how to copy (yy) and paste(p) in the same file. But that doesn't work for different files, how is this done??
Also, is there a way to cut-paste? I have tried googling, but most of the resources only talk about copy-paste.
:echo has('clipboard')
should return1
0
(for example OSX, at least El Capitan, Mavericks and Mountain Lion - comes with Vim version, lacking clipboard support), you have to install Vim version with clipboard support, say viabrew install vim
(don't forget to relaunch your terminal(s) after the installation)P.S:
set mouse+=a
to your.vimrc
- it will allow you to select lines in vim using mouse, while not selecting extraneous elements (like line numbers etc.) NOTICE: it will block ability to copy mouse-selected text to system clipboard from VimEnter command mode and run
E.g to extract lines
20-30
fromfilename
into the currently opened fileThese are all great suggestions, but if you know location of text in another file use sed with ease.
:r! sed -n '1,10 p' < input_file.txt
This will insert 10 lines in an already open file at the current position of the cursor.If you are using VIM in Windows, you can get access to the clipboard (MS copy/paste) using:
"*dd -- cut a line (or 3dd to cut 3 lines)
"*yy -- copy a line (or 3yy to copy 3 lines)
"*p -- paste line(s) on line after the cursor
"*P -- paste line(s) on line before the cursor
The lets you paste between separate VIM windows or between VIM and PC applications (notepad, word, etc).
201705 update:
I just found that if you add following line into your vimrc file,
then VIM is using system clipboard
I just found the yank way won't work on the way where I copy contents between different VIM instance window. (At least, it doesn't work based on my VIM knowledge. I don't know if there is another way to enable it to work).
The yank way only works on the way where multiple files are opened in the same window according to my test.
If you wanna do that, you'd better use OS cut-copy-past way such as ctrl+x, ctrl+c (Under Windows).
Copying text between two buffers (== files) that are opened in the same instance of VIM is no problem: simply yank in one buffer with y (assuming you marked a to-copy area in visual mode before), then paste into the other buffer with p. Also works with different tabs as long as they're in the same instance of VIM.
How to open two files in the same instance of VIM depends on your system:
vim file1 file2
--remote-silent
option to ensure that all files are getting opened in the same instanceIf you opened the two files in two different instances of VIM, then you have to go with the system clipboard: in the first VIM, yank the text into the system clipboard using
"+y
(again, mark the area to be yanked in visual mode before), then go to the second VIM and paste the clipboard there:"+p
.