Copy and paste content from one file to another fi

2019-01-16 00:11发布

问题:

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.

回答1:

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


回答2:

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



回答3:

Use the variations of d like dd to cut.

To write a range of lines to another file you can use:

:<n>,<m> w filename

Where <n> and <m> are numbers (or symbols) that designate a range of lines.

For using the desktop clipboard, take a look at the +g commands.



回答4:

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



回答5:

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!



回答6:

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.



回答7:

While editing the file, make marks where you want the start and end to be using

ma - sets the a mark

mb - sets the b mark

Then, to copy that into another file, just use the w command:

:'a,'bw /name/of/output/file.txt


回答8:

These remaps work like a charm for me:

vmap <C-c> "*y     " Yank current selection into system clipboard
nmap <C-c> "*Y     " Yank current line into system clipboard (if nothing is selected)
nmap <C-v> "*p     " Paste from system clipboard

So, when I'm at visual mode, I select the lines I want and press Ctrl + c and then Ctrl + v to insert the text in the receiver file. You could use "*y as well, but I think this is hard to remember sometimes.

This is also useful to copy text from vim to clipboard.

Source: http://vim.wikia.com/wiki/Copy_and_paste_between_sessions_using_a_temporary_file



回答9:

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.



回答10:

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


回答11:

Haven't found simple answer here so I post my own.

Goal: save a piece of one file to another file.

Solution:

  1. Select the text you want to save:
    • Position the cursor where you want to start selection
    • Press v to select characters OR uppercase V to select whole lines
    • Move the cursor to the end of what you want to select
  2. Save selected text to the new file. Type :wSpace and the name of the new file. Actually you'll see

    :'<,'>w new.txt

    Then press Enter



回答12:

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



回答13:

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


回答14:

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



回答15:

Another way could be to open the two files in two split buffers and use the following "snippet" after visual selection of the lines of interest.

:vnoremap <F4> :y<CR><C-W>Wr<Esc>p


回答16:

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.



回答17:

  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


回答18:

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