如何在 Vim 中拉出一行文本并将其内联粘贴?
比如说,我有以下几行:
thing();
getStuff();
我想使用 yy
命令获取 getStuff()
,然后转到 thing()
,将光标放在 (
上,然后通过 p
命令进行粘贴,但由于我拉动了整行,p
将粘贴getStuff()
回到原来的位置,
我知道您可以首先将光标移动到该 getStuff()
行的开头,然后通过从那里剪切字符直到其结尾。 ^D
命令 - 然后 p 将执行我想要的操作,但是,我发现输入 ^D
更加乏味。 ?
有没有办法将行粘贴到yy
上
Say, I have the following lines:
thing();
getStuff();
I want to take getStuff()
using the yy
command, go forward to thing()
, placing the cursor on (
, and paste via the p
command, but since I yanked the whole line, p
will paste getStuff()
right back where it was.
I know you can first move the cursor to the beginning of that getStuff()
line and cut the characters from there until its end via the ^D
commands—then p will do what I want. However, I find typing ^D
to be much more tedious than yy
.
Is there a way to yy
, but paste the line inline instead?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题是 yy 正在复制整行,包括换行符。另一种方法是从行首复制到行尾,然后粘贴。
^y$
// 感谢:测试员和 Idan Arye 对 Vim 高尔夫的改进。
The problem is that yy is copying the entire line, including the newline. An alternative would be to copy from the beginning to the end of the line, and then paste.
^y$
// Credit to: tester and Idan Arye for the Vim golf improvements.
使用 yiw (“复制内部单词”)而不是 yy猛拉您想要的内容:
yy 是逐行猛拉,将抓取包括回车符在内的整行,您可以如果您查看
:registers
中用作粘贴源的未命名寄存器(""
),就可以看到。请参阅:help ""
:yiw 的另一个好处是您没有位于您正在拉动的“单词”的前面!
Use yiw ("yank inner word") instead of yy to yank just what you want:
yy is line-wise yank and will grab the whole line including the carriage return, which you can see if you look at the unnamed register (
""
) in:registers
which is used as the source for pastes. See:help ""
:An additional benefit to yiw is that you don't have to be at the front of the "word" you are yanking!
简化对相同文本模式进行操作的例程的一种方法
是定义模仿文本对象选择命令的映射。
下面的两对映射 — 一对用于可视模式,另一对用于
操作员挂起模式——提供一种选择当前所有内容的方法
除了换行符 (
al
) 之外的行,以及来自当前行的第一个非空白字符到最后一个非空白字符
字符,包括 (
il
)。因此,不要使用 yy 来复制以下行的内容:
是按字符粘贴(而不是按行),然后可以
使用
yal
或yil
命令进行复制,然后使用p
命令像往常一样粘贴。
One way to simplify the routine of operating on the same text patterns
is to define mappings that mimic text-object selection commands.
The two pairs of mappings below—one for Visual mode and another for
Operator-pending mode—provide a way to select everything on the current
line except for the new line character (
al
), and everything from thefirst non-blank character of the current line through the last non-blank
character, inclusively (
il
).Thus, instead of using
yy
to copy the contents of a line thatis to be pasted character-wise (and not line-wise), one can then
use the
yal
oryil
commands to yank, followed by thep
commandto paste, as usual.
效率较低但简单的方法:
A less efficient, but simple method: