有什么办法可以在 vim 中删除而不覆盖你上次的拉动吗?
我喜欢 vim,但一个常见的问题是:
- 猛拉一行
- 转到您想要粘贴的位置
- 删除那里的内容
- 粘贴您猛拉的内容,却发现它粘贴了您刚刚删除的内容
显然,工作流程是先删除,然后猛拉。但如果我不必这么做,那就太好了。有人有这方面的窍门吗? vim 是否有一个运行良好的粘贴缓冲区,或者是否有一个我可以更改的 .vimrc 设置?
I love vim, but one common gotcha is:
- yank a line
- go to where you would like to paste it
- delete what's there
- paste your yank, only to discover that it pastes what you just deleted
Obviously the workflow is delete first, yank second. But it would be reeeeeaaaaaalllly nice if I didn't have to. Anyone have a trick for this? Does vim have a paste buffer that works well, or is there a .vimrc setting I can change?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
传递到
_
寄存器,黑洞。要删除一行而不将其粘贴到寄存器中:
另请参阅
:help registers
。
如果您想一遍又一遍地粘贴某些内容,那么将其拉入“命名”寄存器可能是最安全的。
将一行拉入
a
寄存器。使用"ap
粘贴它。Pass to the
_
register, the black hole.To delete a line without sticking it in the registers:
See also
:help registers
.It's probably safest, if you want to paste something over and over again, to yank it into a "named" register.
Yanks a line into the
a
register. Paste it with"ap
.你拉出的行应该仍然在寄存器 0 中。所以请
粘贴该行(并在需要时删除)
Your yanked line should still be in the register 0. So do
to paste the line (and delete whenever you want)
默认情况下,所有复制和删除操作都会写入未命名的寄存器。但是,最近的猛拉和最近的删除始终(单独)存储在编号寄存器中。 寄存器
0
保存最近的复制。寄存器1-9
保存 9 个最近的删除(其中1
是最新的)。换句话说,删除会覆盖未命名寄存器中最近的复制,但它仍然存在于
0
寄存器中。黑洞寄存器技巧 ("_dd
)在其他答案中提到是有效的,因为它可以防止覆盖未命名的寄存器,但没有必要使用双引号引用寄存器,因此可以像这样粘贴最近复制的文本:
这是一个很好的参考。 :
All yank and delete operations write to the unnamed register by default. However, the most recent yank and most recent delete are always stored (separately) in the numbered registers. The register
0
holds the most recent yank. The registers1-9
hold the 9 most recent deletes (with1
being the most recent).In other words, a delete overwrites the most recent yank in the unnamed register, but it's still there in the
0
register. The blackhole-register trick ("_dd
) mentioned in the other answers works because it prevents overwriting the unnamed register, but it's not necessary.You reference a register using double quotes, so pasting the most recently yanked text can be done like this:
This is an excellent reference:
另一种可能性如下:
V
)p< /code> 粘贴您的行。
这还有一个额外的好处,即缓冲区与替换的内容“交换”,我发现这非常方便
Another possibility is the following:
V
)p
to paste your lines.This also has the added benefit that the buffer is "swapped" with the replaced contents, which I find very conventient
我使用以下映射来使删除黑洞寄存器变得更容易一些:
这样,
dd
变为Rd
,d$
变为R$
。请注意,R 通常会进入替换模式,但我发现我从未使用过它,因此它是“真正删除”功能最容易记住的键。I use the following mapping to make deleting to the black hole register a bit easier:
This way,
dd
becomesRd
andd$
becomesR$
. Note thatR
is normally bound to enter replace mode, but I found that I never used that, so it was the easisest to remember key for a "really remove" feature.我写了这个插件(yankstack.vim)来解决这个问题。它为您提供了类似于 vim 的 Emacs 的 Kill Ring 的东西。您可以猛拉或删除多个内容,进行粘贴,然后在猛拉/删除文本的历史记录中来回循环。我发现这比记住我把东西拉进哪个寄存器更容易。
在我的 .vimrc 中,我有这些映射:
这让我可以按 ALT-p 或 ALT-SHIFT-p 在我的粘贴历史记录中来回循环。
I wrote this plugin (yankstack.vim) to solve this problem. It gives you something like Emacs's kill ring for vim. You can yank or delete multiple things, do a paste, and then cycle back and forth through your history of yanked/killed text. I find its easier than having to remember what register I yanked something into.
In my .vimrc, I have these mappings:
which let me hit ALT-p or ALT-SHIFT-p to cycle back and forth through my paste history.
对于您的具体问题,按照要求,您不能交换最后两个步骤的顺序吗?
当然,我通常使用命名寄存器来处理此类事情,但有时解决方案比首先想到的更简单。
For your specific question, as asked, couldn't you just swap the order of the last 2 steps?
Granted, I usually use a named register for this type of thing, but sometimes the solution is simpler than what first comes to mind.
您可以使用寄存器:
"
例如:
这会将一行拉入寄存器
a
,删除另一行,然后粘贴寄存器一个
You could use registers:
"<register><command>
E.g.:
This yanks a line into register
a
, deletes a different line, and then pastes registera
您还可以尝试以下脚本: ReplaceWithRegister at vim.org/scripts/< /a>
You can also try out the following script: ReplaceWithRegister at vim.org/scripts/
如果您是一个邪恶的用户,您可能会考虑重新映射 X 来执行相当于“_d”的操作。但是,完善实现对我来说有点棘手。尽管如此,我发现
集成得非常好。例如,输入 XX 的功能类似于dd,X$ 到 d$,X0 到 d0 等等...
如果您好奇它是如何工作的,“95”代表“_ 寄存器,所以它只是重新路由您的调用以删除,就好像“_ 已经被按下寄存器。
If you are an evil user, you may consider remapping X to do the equivalent of "_d. However, perfecting the implementation was a little tricky for me. Nonetheless, I found that
integrates very nicely. For example, typing XX will function analogously to dd, as will X$ to d$, X0 to d0, etc...
If you are curious as to how it works, "95" represents the "_ register, so it simply reroutes your call to delete as if "_ had been the register pressed.
我发现所有这些关键组合都很麻烦。相反,我编写了一个函数来打开和关闭 vim 的“副作用”行为,即通过删除操作覆盖缓冲区。这样您就可以将其关闭,自由删除,然后在完成后重新打开。
请在此处查看我的答案: https://stackoverflow.com/a/12649560/778118
要使用它,只需将其放入您的.vimrc
I find all these key combos cumbersome. Instead, I wrote a function to toggle on and off vim's "side effect" behavior of overwriting buffers from delete operations. That way you can just toggle it off, delete freely, then toggle back on when you're done.
See my answer here: https://stackoverflow.com/a/12649560/778118
To use it just put it in your .vimrc
诀窍是你知道你想要抓住一些东西并移动,并且你正在使用“惰性”第一个寄存器(它会被你刚刚删除的任何内容所取代)。
你需要学习在 vim 中“剪切”。
删除之前,请指定与
"
不同的任何寄存器。提示:现在使用:reg
检查您的寄存器,通过按
" 在任何命令之前(显然是在命令模式下)
1
)并删除:"1d
或"1x
甚至"1c
"1p
或"1P
完成,这也有解决用例的优点:从一个地方删除 5 个不同的东西,每个部分都会到达不同的目的地......只需将一个放入
"1
中,另一个放入"2
中,依此类推...转到每个目的地并粘贴。The trick is that you know you want to grab something and move, and you are using the 'lazy' first register (which gets replaced by whatever you just deleted).
You need to learn to "cut" in vim.
Before deleting, specify any register different than the
"
one. Tip: check out your registers with:reg
now, you select a new register by pressing
"
before any command (in command mode, obviously)1
here) and delete:"1d
or"1x
or even"1c
"1p
or"1P
done. this also has the advantage of solving the usecase: delete 5 different things from one place, and each piece goes to a different destination... just put one in
"1
another in"2
and so on... go to each destination and paste.