While in vi how can I pull in / insert / paste the contents of another file

9,760

Solution 1

Type the following

:r filename_to_paste

This will paste the contents of the file after the line on which the cursor is present.


If you need to copy/paste smaller range of lines/block of text from one file to other, you can also do the following assuming one file is opened in vim already

  • Open 2nd file using :sp (split) or :vsp(vertical split)
  • do a normal yy (yank) commands in the other file
  • do p (paste) command back in the 1st file, as the register (place where yanked text is stored) is common to both the files.
  • Press Ctrl+w twice to switch between splited files.

Solution 2

That :r filename is the shortest for whole files. If you want only one more small portions, I always go to the source file and use "a57Y "b12Y to copy specific lines to registers (registers "a and "b in these examples), which I can retrieve with "ap and "bp in the destination file.

Share:
9,760

Related videos on Youtube

Michael Durrant
Author by

Michael Durrant

rails ruby rspec rock

Updated on September 18, 2022

Comments

  • Michael Durrant
    Michael Durrant over 1 year
    • I am editing file 'A' in vi.
    • I have another file 'B' whose contents I want to 'paste' into my current location.
    • Other than actual mouse copy and pasting, is there a command that I can use that will pull in the content of another file to my current edit location.
    • dchirikov
      dchirikov over 11 years
      Perhaps this is what you want?
  • Michael Durrant
    Michael Durrant over 11 years
    Looks good. Will accept soon.