在 vi 中将一个文件的内容复制并粘贴到另一个文件
我正在处理两个文件,我需要从一个文件复制几行并粘贴到另一个文件中。我知道如何将 (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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(20)
由于您已经知道如何剪切/拉出文本,因此这里有一些将其粘贴回另一个文件的想法:
:e /path/to/other/file
) 并将其粘贴在拆分窗口中一起打开两个文件,并使用 Ctrl 在它们之间导航 + w、向上/向下 任一:
vi -o /path/to/file1 /path/to/file2
Since you already know how to cut/yank text, here are a few ideas for pasting it back into another file:
:e /path/to/other/file
) and paste itOpen 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
如果您在 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.).
使用
d
的变体(如dd
)进行剪切。要将一系列行写入另一个文件,您可以使用:
其中
和
是指定一系列行的数字(或符号)。要使用桌面剪贴板,请查看
+g
命令。Use the variations of
d
likedd
to cut.To write a range of lines to another file you can use:
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.这是一种方法;
您也可以查看这两个文件。使用例如 Ctrl + w s 分割屏幕。
至于剪切,d 剪切并将剪切的内容放入复制缓冲区中。 dd 将“剪切”一条线。
Here's one way to do it;
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.
您可以打开另一个文件并输入
: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 andP
is put. Hope that helps!这些都是很好的建议,但如果您知道另一个文件中文本的位置,则可以轻松使用 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.2017-05 更新:
我刚刚发现如果您将以下行添加到 vimrc 文件中,
那么 Vim 正在使用系统剪贴板。
我刚刚发现猛拉方式不适用于我在不同 Vim 实例窗口之间复制内容的方式。 (至少,根据我的 Vim 知识,它不起作用。我不知道是否有其他方法让它起作用)。
根据我的测试,猛拉方式仅适用于在同一窗口中打开多个文件的方式。
如果你想这样做,你最好使用操作系统剪切复制过去的方式,例如 Ctrl + x、Ctrl + c(Windows 下)。
2017-05 update:
I just found that if you add the following line into your vimrc file,
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).
:echo has('clipboard')
应返回1
0
(例如 Mac OS X,至少 v10.11 (El Capitan)、v10.9 (Mavericks) 和 v10.8 (Mountain Lion) - 附带缺乏剪贴板支持的 Vim 版本),您必须安装支持剪贴板的 Vim 版本,例如通过brew install vim
(不要忘记在安装后重新启动终端)P.S:
set mouse+=a
添加到您的.vimrc
- 它将允许您使用鼠标在 Vim 中选择行,而不选择无关的元素(如行号等)注意:它将阻止将鼠标选择的文本复制到Vim 的系统剪贴板。:echo has('clipboard')
should return1
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 viabrew install vim
(don't forget to relaunch your terminal(s) after the installation)P.S:
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.编辑文件时,在您想要开始和结束的位置使用
ma
标记 - 设置a
标记mb
- 设置>b
标记然后,要将其复制到另一个文件中,只需使用
w
命令:While editing the file, make marks where you want the start and end to be using
ma
- sets thea
markmb
- sets theb
markThen, to copy that into another file, just use the
w
command:这些重新映射对我来说就像一个魅力:
因此,当我处于视觉模式时,我选择所需的行并按 Ctrl + c,然后按 Ctrl + v 将文本插入接收文件中。你也可以使用 "*y,但我认为有时这很难记住。
这对于将文本从 Vim 复制到剪贴板也很有用。
来源:使用临时文件在会话之间复制和粘贴
These remaps work like a charm for me:
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
目标:将一个文件的一部分保存到另一个文件中。
解决方案:
将选定的文本保存到新文件中。输入 :wSpace 和新文件的名称。其实你会看到
:'<,'>w new.txt
然后按Enter
Goal: save a piece of one file to another file.
Solution:
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
在同一 Vim 实例中打开的两个缓冲区(== 文件)之间复制文本是没有问题的:
只需使用 y 拉入一个缓冲区(假设您之前在可视模式下标记了要复制的区域) ),然后用 p 粘贴到另一个缓冲区中。它也适用于不同的选项卡,只要它们位于同一 Vim 实例中即可。
如何在同一个 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:
vim file1 file2
--remote-silent
option to ensure that all files are getting opened in the same instanceIf 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
.我的场景是我需要将中间的 n 行(n 未知)从文件 1 复制到文件 2。
My scenario was I need to copy n number of lines in middle, n unknown, from file 1 to file 2.
如果您想复制文件的一部分并将该内容粘贴到另一个文件的中间,可以这样做。
示例:
或
示例:
现在这些行被复制到另一个文件中。
如果您想在复制后删除这些行,您可以执行
“或者
现在,转到其他文件”。
然后将光标保持在要粘贴的行上。
现在键入
,临时文件的内容将粘贴到此处。
粘贴内容后,您可以从命令行本身删除临时文件。
如果您想多次复制和粘贴,这会有所帮助。
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.
Example:
Or
Example:
Now the lines are copied to another file.
If you want to delete those lines after copying, you can do
Or
Now, go to other file.
Then keep the cursor on the line where you wanted to paste.
Type
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.
This would help if you wanted to copy and paste several times.
示例:fileA 和 fileB - 从 fileA 的第 25 行开始,复制 50 行,然后粘贴到 fileB
Example: fileA and fileB - start in fileA at line 25, copy 50 lines, and paste to fileB
下面的选项在大多数情况下都有效,也适用于稍后粘贴。
拉出的行将存储在缓冲区
'x'
中。它可以在编辑中的任何地方使用。
要将行粘贴到其他文件中,
示例:
在当前编辑中键入以下命令
完成操作
打开文件 1 :
-现在打开当前编辑中的第二个文件
-移动光标到要粘贴的行
--缓冲区中的行
'*a*'
将被复制到当前光标位置之后The below option works most of time and also for pasting later.
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,
Example:
Type the below command in the current edit
Complete operation
open file 1 :
-now open the second file from the current edit
-move the cursor to the line where you have to paste
--Lines from the buffer
'*a*'
will be copied after the current cursor pos进入命令模式并运行
E.g 将
filename
中的20-30
行提取到当前打开的文件中Enter command mode and run
E.g to extract lines
20-30
fromfilename
into the currently opened file另一种方法可能是在两个分割缓冲区中打开两个文件,并在视觉选择感兴趣的行后使用以下“片段”。
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.
在
.vimrc
中设置viminfo
:这将在
~/.viminfo
中保留最多 1,000 行拉出的内容(您的“寄存器”)当你退出 Vim 时文件。当您启动 Vim 时它会恢复,因此您可以将其粘贴到另一个文件中。有关所有内容,请参阅
viminfo
文档它可以在会话中持续存在的其他内容。Set
viminfo
in your.vimrc
: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.最后我找到了它:
要打开第二个文件,请使用:
要导航到这两个文件,请使用:
Finally I found it:
To open the second file use:
To navigate into the both files use: