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

后端 未结 18 1507
南方客
南方客 2020-12-04 05:02

I am working with two files, and I need to copy a few lines from one file and paste into another file. I know how to copy (yy) and paste (p) in the same file. But that doesn

18条回答
  •  一个人的身影
    2020-12-04 05:22

    If you want to copy a part of a file and paste that content in the middle of another file, you can do this way.

    :linenumber,linenumber write newfile
    

    Example:

    :2,34 write temp1
    

    Or

    :'mark, 'mark write newfile
    

    Example:

    :'a,'b write temp1
    

    Now the lines are copied to another file. If you want to delete those lines after copying, you can do

    :linenumber1,linenumber2 d
    

    Or

    :'mark1,'mark2 d
    

    Now, go to other file. Then keep the cursor on the line where you wanted to paste.

    Type

    :r!cat temp1
    

    Now, the content of the temp file is pasted here. You can delete the temp file from the command line itself, after pasting the content.

    :!rm temp1
    

    This would help if you wanted to copy and paste several times.

提交回复
热议问题