Copy and paste content from one file to another fi

2019-01-16 00:03发布

I am working with two files, 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't work for different files, how is this done??

Also, is there a way to cut-paste? I have tried googling, but most of the resources only talk about copy-paste.

18条回答
Animai°情兽
2楼-- · 2019-01-16 00:34
  1. Make sure you have Vim version compiled with clipboard support
    • :echo has('clipboard') should return 1
    • if it returns 0 (for example OSX, at least El Capitan, Mavericks and Mountain Lion - comes with Vim version, lacking clipboard support), you have to install 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&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 mouse, while not selecting extraneous elements (like line numbers etc.) NOTICE: it will block ability to copy mouse-selected text to system clipboard from Vim
查看更多
放荡不羁爱自由
3楼-- · 2019-01-16 00:34

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
查看更多
一夜七次
4楼-- · 2019-01-16 00:35

These are all great suggestions, but if you know location of text in another file use sed with ease. :r! sed -n '1,10 p' < input_file.txt This will insert 10 lines in an already open file at the current position of the cursor.

查看更多
兄弟一词,经得起流年.
5楼-- · 2019-01-16 00:42

If you are using VIM in Windows, you can get access to the clipboard (MS copy/paste) using:

"*dd -- cut a line (or 3dd to cut 3 lines)

"*yy -- copy a line (or 3yy to copy 3 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, word, etc).

查看更多
祖国的老花朵
6楼-- · 2019-01-16 00:44

201705 update:

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

set clipboard=unnamed

then VIM is using system clipboard


I just found the yank way won't work on the way where I copy contents between different VIM instance window. (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 wanna do that, you'd better use OS cut-copy-past way such as ctrl+x, ctrl+c (Under Windows).

查看更多
Bombasti
7楼-- · 2019-01-16 00:46

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), then paste into the other buffer with p. 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, 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.

查看更多
登录 后发表回答