在 vi 中将一个文件的内容复制并粘贴到另一个文件

发布于 2024-10-11 02:52:55 字数 134 浏览 7 评论 0原文

我正在处理两个文件,我需要从一个文件复制几行并粘贴到另一个文件中。我知道如何将 (yy) 复制并粘贴 (p) 到同一文件中。但这不适用于不同的文件。这是怎么做到的?

另外,有没有办法剪切粘贴?我尝试过谷歌搜索,但大多数资源只讨论复制粘贴。

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(20

醉生梦死 2024-10-18 02:52:55

由于您已经知道如何剪切/拉出文本,因此这里有一些将其粘贴回另一个文件的想法:

  • 编辑第一个文件,拉出您想要的文本。然后从 vi 中打开第二个文件 (:e /path/to/other/file) 并将其粘贴
  • 在拆分窗口中一起打开两个文件,并使用 Ctrl 在它们之间导航 + w向上/向下 任一:

    • vi -o /path/to/file1 /path/to/file2
    • 在第一个文件中,Ctrl + w, s

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
墨小墨 2024-10-18 02:52:55

如果您在 Windows 上使用 Vim,则可以使用以下命令访问剪贴板(MS 复制/粘贴):

"*d d -- 剪切一行(或 3dd 剪切三行)

"*yy -- 复制一行(或 3yy 复制三行)

"*p -- 将行粘贴到光标后的行

< kbd>"*P -- 在光标之前的行上粘贴行

允许您在单独的 Vim 窗口之间或 Vim 和 PC 应用程序之间粘贴 ( 记事本Microsoft Word 等)。

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

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

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

绝影如岚 2024-10-18 02:52:55

使用 d 的变体(如 dd)进行剪切。

要将一系列行写入另一个文件,您可以使用:

:<n>,<m> w filename

其中 是指定一系列行的数字(或符号)。

要使用桌面剪贴板,请查看 +g 命令。

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.

日记撕了你也走了 2024-10-18 02:52:55

这是一种方法;

  • 启动 Vim 并打开 file1,这是您正在处理的文件。
  • :e file2 这将打开 file2,即您要从中复制行的文件。
  • 找到您要复制的行。如果是三行,则输入 3yy
  • :b 1 这将切换到缓冲区 1,其中 file1 确定
  • 您要在何处插入您拉出的行,然后点击 p

您也可以查看这两个文件。使用例如 Ctrl + w s 分割屏幕。

至于剪切,d 剪切并将剪切的内容放入复制缓冲区中。 dd 将“剪切”一条线。

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.

梦中楼上月下 2024-10-18 02:52:55

您可以打开另一个文件并输入 :r file_to_be_copied_from。或者你可以缓冲。或者转到第一个文件,在要复制的行上输入 "qY,转到要粘贴的文件并输入 "qP

"buffer_name,复制到缓冲区。Y 是 yank,P 是 put。希望有帮助!

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!

等往事风中吹 2024-10-18 02:52:55

这些都是很好的建议,但如果您知道另一个文件中文本的位置,则可以轻松使用 sed。 <代码>:r! sed -n '1,10 p' < input_file.txt 这将在光标当前位置的已打开文件中插入 10 行。

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.

寄居人 2024-10-18 02:52:55

2017-05 更新:

我刚刚发现如果您将以下行添加到 vimrc 文件中,

设置剪贴板=未命名

那么 Vim 正在使用系统剪贴板。


我刚刚发现猛拉方式不适用于我在不同 Vim 实例窗口之间复制内容的方式。 (至少,根据我的 Vim 知识,它不起作用。我不知道是否有其他方法让它起作用)。

根据我的测试,猛拉方式仅适用于在同一窗口中打开多个文件的方式。

如果你想这样做,你最好使用操作系统剪切复制过去的方式,例如 Ctrl + xCtrl + c(Windows 下)。

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

淡淡の花香 2024-10-18 02:52:55
  1. 确保您编译的 Vim 版本支持剪贴板
    • :echo has('clipboard') 应返回 1
    • 如果返回 0(例如 Mac OS X,至少 v10.11 (El Capitan)、v10.9 (Mavericks) 和 v10.8 (Mountain Lion) - 附带缺乏剪贴板支持的 Vim 版本),您必须安装支持剪贴板的 Vim 版本,例如通过 brew install vim (不要忘记在安装后重新启动终端)
  2. 输入可视模式(V - 多行,v - 纯文本,或 Ctrlv - 块视觉)
  3. 选择您要复制的行
  4. *y - 复制选定的
  5. *p - 粘贴复制的

P.S:

  • 您可以使用 答案,作者:JayG,如果您需要复制并粘贴一行
  • 以方便选择行,您可以将 set mouse+=a 添加到您的 .vimrc - 它将允许您使用鼠标在 Vim 中选择行,而不选择无关的元素(如行号等)注意:它将阻止将鼠标选择的文本复制到Vim 的系统剪贴板
  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.
哥,最终变帅啦 2024-10-18 02:52:55

编辑文件时,在您想要开始和结束的位置使用

ma 标记 - 设置 a 标记

mb - 设置 >b 标记

然后,要将其复制到另一个文件中,只需使用 w 命令:

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

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
三寸金莲 2024-10-18 02:52:55

这些重新映射对我来说就像一个魅力:

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

因此,当我处于视觉模式时,我选择所需的行并按 Ctrl + c,然后按 Ctrl + v 将文本插入接收文件中。你也可以使用 "*y,但我认为有时这很难记住。

这对于将文本从 Vim 复制到剪贴板也很有用。

来源:使用临时文件在会话之间复制和粘贴

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: Copy and paste between sessions using a temporary file

寂寞陪衬 2024-10-18 02:52:55

目标:将一个文件的一部分保存到另一个文件中。

解决方案

  1. 选择您要保存的文本:
    • 将光标置于要开始选择的位置
    • v 选择字符或按大写 V 选择整行
    • 将光标移动到要选择的内容的末尾
  2. 将选定的文本保存到新文件中。输入 :wSpace 和新文件的名称。其实你会看到

    :'<,'>w new.txt

    然后按Enter

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

权谋诡计 2024-10-18 02:52:55

在同一 Vim 实例中打开的两个缓冲区(== 文件)之间复制文本是没有问题的:

只需使用 y 拉入一个缓冲区(假设您之前在可视模式下标记了要复制的区域) ),然后用 p 粘贴到另一个缓冲区中。它也适用于不同的选项卡,只要它们位于同一 Vim 实例中即可。

如何在同一个 Vim 实例中打开两个文件取决于您的系统:

  • 上下文菜单中会有一个选项显示使用一个 vim 进行编辑
  • 在 Win32 上,如果您选择两个或多个文件, 如果您使用Vim
  • 作为其他工具的编辑器,请务必指定 --remote-silent 选项以确保所有文件在同一个实例中打开

如果您在两个不同的 Vim 实例中打开了这两个文件,那么您必须使用系统剪贴板:在第一个 Vim 实例中,使用 "+ 将文本拖入系统剪贴板y(再次标记之前在可视模式下要拉取的区域),然后转到第二个 Vim 并将剪贴板粘贴到那里:"+p

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

︶ ̄淡然 2024-10-18 02:52:55

我的场景是我需要将中间的 n 行(n 未知)从文件 1 复制到文件 2。

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

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
眼波传意 2024-10-18 02:52:55

如果您想复制文件的一部分并将该内容粘贴到另一个文件的中间,可以这样做。

:linenumber,linenumber write newfile

示例:

:2,34 write temp1

:'mark, 'mark write newfile

示例:

:'a,'b write temp1

现在这些行被复制到另一个文件中。
如果您想在复制后删除这些行,您可以执行

:linenumber1,linenumber2 d

“或者

:'mark1,'mark2 d

现在,转到其他文件”。
然后将光标保持在要粘贴的行上。

现在键入

:r!cat temp1

,临时文件的内容将粘贴到此处。
粘贴内容后,您可以从命令行本身删除临时文件。

:!rm temp1

如果您想多次复制和粘贴,这会有所帮助。

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.

朕就是辣么酷 2024-10-18 02:52:55

示例:fileA 和 fileB - 从 fileA 的第 25 行开始,复制 50 行,然后粘贴到 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

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
一生独一 2024-10-18 02:52:55

下面的选项在大多数情况下都有效,也适用于稍后粘贴。

 "xnyy
x - buffer name
n - number of line to Yank - optional

拉出的行将存储在缓冲区'x'中。
它可以在编辑中的任何地方使用。

要将行粘贴到其他文件中,

:e filename&location

示例:
在当前编辑中键入以下命令

:e /u/test/Test2.sh
and paste using "xP
P - before cursor
p - after cursor

完成操作

打开文件 1 :

vi Test1.sh

a10yy

-Yanked 10 lines

-现在打开当前编辑中的第二个文件

*:e /u/test/Test2.sh*

-移动光标到要粘贴的行

*"ap*

--缓冲区中的行'*a*'将被复制到当前光标位置之后

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

勿挽旧人 2024-10-18 02:52:55

进入命令模式并运行

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

E.g 将 filename 中的 20-30 行提取到当前打开的文件中

:r! sed -n '20, 30p' filename

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
梦境 2024-10-18 02:52:55

另一种方法可能是在两个分割缓冲区中打开两个文件,并在视觉选择感兴趣的行后使用以下“片段”。

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

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
您的好友蓝忘机已上羡 2024-10-18 02:52:55

.vimrc 中设置 viminfo

set viminfo=%,<1000,'10,/50,:100,h,f0,n~/.viminfo

这将在 ~/.viminfo 中保留最多 1,000 行拉出的内容(您的“寄存器”)当你退出 Vim 时文件。当您启动 Vim 时它会恢复,因此您可以将其粘贴到另一个文件中。

有关所有内容,请参阅 viminfo 文档它可以在会话中持续存在的其他内容。

Set viminfo in your .vimrc:

set viminfo=%,<1000,'10,/50,:100,h,f0,n~/.viminfo

This will persist up to 1,000 lines of yanked stuff (your "registers") in the ~/.viminfo file when you quit Vim. It's restored when you start Vim, so you can paste it in a different file.

See the viminfo documentation for all the other stuff it can persist across sessions.

仙女山的月亮 2024-10-18 02:52:55

最后我找到了它:

要打开第二个文件,请使用:

:vsp destination_file.txt

要导航到这两个文件,请使用:

ctrl+w w

Finally I found it:

To open the second file use:

:vsp destination_file.txt

To navigate into the both files use:

ctrl+w w
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文