Copy and paste content from one file to another file in vi
Since you already know how to cut/yank text, here are a few ideas for pasting it back into another file:
- Edit the first file, yanking the text you want. Then open your second file from within vi (
:e /path/to/other/file
) and paste it -
Open 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
- From within the first file, Ctrl + w, s
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.).
Use the variations of d
like dd
to cut.
To write a range of lines to another file you can use:
:<n>,<m> w filename
Where <n>
and <m>
are numbers (or symbols) that designate a range of lines.
For using the desktop clipboard, take a look at the +g
commands.
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!