paste the contents of one file into another file using Vi

98,853

Solution 1

One solution would be to:

cat originalfile.txt >> newfile.txt

and then remove the original file. It would simplify for us if you let us know exactly what you are trying to achieve.

Solution 2

Open both files:

vi fileToChange fileToCopyFrom

Start in the file you want to delete content from permanently to replace with new content. (move between files with :n and :N) To delete all the contents, press esc if needed and type

dG

now save the change and move to the other file

:wn

(replace n with N if you started in the second file - you will see what I mean when you try)

In the other file, type

dG

this cuts the text so you can paste it. Now move back to the other file, without saving the change in this file (so the content will not be deleted)

:N!

and paste the new content:

p

If you want to undo something you can press u

Solution 3

there is many way to do this:

if you just want to replace one file content with other file content then you can do like :

  1. copy command :

    cp file anotherfile
    
  2. cat command:

    cat file > anotherfile
    
  3. If you want to use editor then you can use gedit editor :

    gedit file
    

select complete file content Ctrl+ a and open another file with gedit and replace content

gedit anothefile

there is many editor and method to replace content. you have to decide which one is comfortable for you.

Share:
98,853

Related videos on Youtube

Avani badheka
Author by

Avani badheka

Updated on September 18, 2022

Comments

  • Avani badheka
    Avani badheka over 1 year

    How can I copy content from one file, and in another file somehow delete the original content and then paste the contents of the first file using vi editor? Or can it be done using another editor easily?

    • Sergiy Kolodyazhnyy
      Sergiy Kolodyazhnyy over 7 years
      So if you have file A and B , what is the result you want ? Contents of file A become same as contents of file B ?
  • Felipe
    Felipe over 2 years
    This is what I want. And it works for binary files too! Nice.