I am working with two files, and 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
Example: fileA and fileB - start in fileA at line 25, copy 50 lines, and paste to fileB
fileA
Goto 25th line
25G
copy 50 lines into buffer v
"v50yy
Goto fileB
:e fileB
Goto line 10
10G
paste contents of buffer v
"vp
Here's one way to do it;
You could have both files viewable too. Split the screen with e.g. Ctrl + w s.
As for cutting, d cuts and places the cut stuff in the yank buffer. dd will "cut" a line.
Enter command mode and run
:r! sed -n '<start_line_num>, <end_line_num> p' file_to_extract_text_from
E.g to extract lines 20-30
from filename
into the currently opened file
:r! sed -n '20, 30p' filename
2017-05 update:
I just found that if you add the following line into your vimrc file,
set clipboard=unnamed
then Vim is using the system clipboard.
I just found the yank way won't work on the way where I copy contents between different Vim instance windows. (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 want to do that, you'd better use OS cut-copy-past way such as Ctrl + x, Ctrl + c (under Windows).
:echo has('clipboard')
should return 1
0
(for example Mac OS X, at least v10.11 (El Capitan), v10.9 (Mavericks) and v10.8 (Mountain Lion) - comes with a Vim version lacking clipboard support), you have to install a Vim version with clipboard support, say via brew 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 the mouse, while not selecting extraneous elements (like line numbers, etc.) NOTICE: it will block the ability to copy mouse-selected text to the system clipboard from Vim.You can open the other file and type :r file_to_be_copied_from
. Or you can buffer. Or go to the first file, go on the line you want to copy, type "qY
, go to the file you want to paste and type "qP
.
"
buffer_name, copies to the buffer. Y
is yank and P
is put. Hope that helps!