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

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

    Example: fileA and fileB - start in fileA at line 25, copy 50 lines, and paste to fileB

    fileA
    
    Goto 25th line
    
    25G
    
    copy 50 lines into buffer v
    
    "v50yy
    
    Goto fileB
    
    :e fileB
    
    Goto line 10
    
    10G    
    
    paste contents of buffer v
    "vp
    
    0 讨论(0)
  • 2020-12-04 05:15

    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.

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

    Enter command mode and run

    :r! sed -n '<start_line_num>, <end_line_num> p' file_to_extract_text_from
    

    E.g to extract lines 20-30 from filename into the currently opened file

    :r! sed -n '20, 30p' filename
    
    0 讨论(0)
  • 2020-12-04 05:18

    2017-05 update:

    I just found that if you add the following line into your vimrc file,

    set clipboard=unnamed

    then Vim is using the system clipboard.


    I just found the yank way won't work on the way where I copy contents between different Vim instance windows. (At least, it doesn't work based on my Vim knowledge. I don't know if there is another way to enable it to work).

    The yank way only works on the way where multiple files are opened in the same window according to my test.

    If you want to do that, you'd better use OS cut-copy-past way such as Ctrl + x, Ctrl + c (under Windows).

    0 讨论(0)
  • 2020-12-04 05:20
    1. Make sure you have the Vim version compiled with clipboard support
      • :echo has('clipboard') should return 1
      • if it returns 0 (for example Mac OS X, at least v10.11 (El Capitan), v10.9 (Mavericks) and v10.8 (Mountain Lion) - comes with a Vim version lacking clipboard support), you have to install a Vim version with clipboard support, say via brew install vim (don't forget to relaunch your terminal(s) after the installation)
    2. Enter a visual mode (V - multiline, v - plain, or Ctrlv - block-visual)
    3. Select line(s) you wish to copy
    4. "*y - to copy selected
    5. "*p - to paste copied

    P.S:

    • you can replace steps 2-5 with the instructions from the answer by JayG, if you need to copy and paste a single line
    • to ease selecting lines, you can add set mouse+=a to your .vimrc - it will allow you to select lines in Vim using the mouse, while not selecting extraneous elements (like line numbers, etc.) NOTICE: it will block the ability to copy mouse-selected text to the system clipboard from Vim.
    0 讨论(0)
  • 2020-12-04 05:21

    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!

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