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

后端 未结 18 1407
南方客
南方客 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

    Copying text between two buffers (== files) that are opened in the same instance of Vim is no problem:

    Simply yank in one buffer with y (assuming you marked a to-copy area in visual mode before), and then paste into the other buffer with p. It also works with different tabs as long as they're in the same instance of Vim.

    How to open two files in the same instance of Vim depends on your system:

    • On Win32, there's an option in the context menu saying Edit with one vim if you select two or more files
    • When you're on the console, you can achieve it with vim file1 file2
    • If you use Vim as editor for another tool, be sure to specify the --remote-silent option to ensure that all files are getting opened in the same instance

    If you opened the two files in two different instances of Vim, then you have to go with the system clipboard: in the first Vim instance, yank the text into the system clipboard using "+y (again, mark the area to be yanked in visual mode before), then go to the second Vim and paste the clipboard there: "+p.

    0 讨论(0)
  • 2020-12-04 05:22

    My scenario was I need to copy n number of lines in middle, n unknown, from file 1 to file 2.

    :'a,'bw /name/of/output/file.txt
    
    0 讨论(0)
  • 2020-12-04 05:22

    The below option works most of time and also for pasting later.

     "xnyy
    x - buffer name
    n - number of line to Yank - optional
    

    The lines yanked will be stored in the buffer 'x'. It can be used anywhere in the edit.

    To paste line(s) in the other file,

    :e filename&location
    

    Example: Type the below command in the current edit

    :e /u/test/Test2.sh
    and paste using "xP
    P - before cursor
    p - after cursor
    

    Complete operation

    open file 1 :

    vi Test1.sh
    
    a10yy
    
    -Yanked 10 lines
    

    -now open the second file from the current edit

    *:e /u/test/Test2.sh*
    

    -move the cursor to the line where you have to paste

    *"ap*
    

    --Lines from the buffer '*a*' will be copied after the current cursor pos

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-04 05:26

    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
    0 讨论(0)
  • 2020-12-04 05:28

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

    0 讨论(0)
提交回复
热议问题