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条回答
乱世女痞
2楼-- · 2019-01-16 00:19

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

查看更多
甜甜的少女心
3楼-- · 2019-01-16 00:21

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.

查看更多
一夜七次
4楼-- · 2019-01-16 00:22

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
查看更多
We Are One
5楼-- · 2019-01-16 00:24

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.

查看更多
做自己的国王
6楼-- · 2019-01-16 00:24

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
查看更多
Rolldiameter
7楼-- · 2019-01-16 00:24

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

查看更多
登录 后发表回答