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

ID : 10128

viewed : 45

Tags : vimcopy-pastevicutvimvi

Top 5 Answer for vim - Copy and paste content from one file to another file in vi

vote vote


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
vote vote


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

vote vote


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.

vote vote


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!

vote vote


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.

Top 3 video Explaining vim - Copy and paste content from one file to another file in vi