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
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), and then paste into the other buffer with p. It 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 instance, 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
.
My scenario was I need to copy n number of lines in middle, n unknown, from file 1 to file 2.
:'a,'bw /name/of/output/file.txt
The below option works most of time and also for pasting later.
"xnyy
x - buffer name
n - number of line to Yank - optional
The lines yanked will be stored in the buffer 'x'
.
It can be used anywhere in the edit.
To paste line(s) in the other file,
:e filename&location
Example: Type the below command in the current edit
:e /u/test/Test2.sh
and paste using "xP
P - before cursor
p - after cursor
Complete operation
open file 1 :
vi Test1.sh
a10yy
-Yanked 10 lines
-now open the second file from the current edit
*:e /u/test/Test2.sh*
-move the cursor to the line where you have to paste
*"ap*
--Lines from the buffer '*a*'
will be copied after the current cursor pos
If you want to copy a part of a file and paste that content in the middle of another file, you can do this way.
:linenumber,linenumber write newfile
Example:
:2,34 write temp1
Or
:'mark, 'mark write newfile
Example:
:'a,'b write temp1
Now the lines are copied to another file. If you want to delete those lines after copying, you can do
:linenumber1,linenumber2 d
Or
:'mark1,'mark2 d
Now, go to other file. Then keep the cursor on the line where you wanted to paste.
Type
:r!cat temp1
Now, the content of the temp file is pasted here. You can delete the temp file from the command line itself, after pasting the content.
:!rm temp1
This would help if you wanted to copy and paste several times.
Since you already know how to cut/yank text, here are a few ideas for pasting it back into another file:
:e /path/to/other/file
) and paste itOpen both files together in a split window and navigate between them using Ctrl + w, Up/Down either by:
vi -o /path/to/file1 /path/to/file2
If you are using Vim on Windows, you can get access to the clipboard (MS copy/paste) using:
"*dd -- cut a line (or 3dd to cut three lines)
"*yy -- copy a line (or 3yy to copy three 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, Microsoft Word, etc.).