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

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

提交回复
热议问题