Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy and paste content from one file to another file in vi

People also ask

How do I copy the contents of a VI file?

Another easy way to copy the entire file if you're having problems using VI, is just by typing "cat filename". It will echo the file to screen and then you can just scroll up and down and copy/paste.

How do you copy the contents of a file to another file in Linux?

The Linux cp command is used for copying files and directories to another location. To copy a file, specify “cp” followed by the name of a file to copy.

How do I copy text from one file to another?

Highlight the text you want to copy. Use the shortcut key combination Ctrl + C on a PC or Command + C on a Mac to copy the text. Move the text cursor to where you want to paste the text. Press Ctrl + V on a PC or Command + V on a Mac to paste the text.

How do I copy and paste in Putty VI?

You can select text (suppose from a vi session) inside of a putty window (remember no CTRL-C requirewd here). Then go to your Windows program and hit the middle mouse button or 'Edit | paste' and the text will be pasted.


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!


Here's one way to do it;

  • Start Vim and open file1 which is the file you're working on.
  • :e file2 which will bring up file2, the file you want to copy lines from.
  • locate the lines you want to copy. If it's three lines, you hit 3yy
  • :b1 this will switch to buffer 1, where file1 is
  • figure out where you want to insert the lines you yanked, and hit p

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.


These 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.


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).