Vim 在文本放置时或放置后立即更改文本
最近,我一直在y
anking和p
放置大量需要进行一些更改的代码(通常只是简单的替换)。我可以在粘贴后手动选择它,或者对于较长的块,我想我可以查看粘贴的行数(20 新行
)并使用 20:s...,但考虑到它是 vim,似乎应该有一种更简单/更快的方法来做到这一点。
那么有没有一种方法可以在输入文本时选择或执行替换呢?
Lately I've been y
anking and p
utting a lot of code that needs to be altered somewhat (usually just a simple substitution). I can manually select it after it's pasted in, or for longer blocks I suppose I could look at the number of lines pasted (20 new lines
) and use 20:s...
, but given that it's vim, it seems like there should be an easier/faster way to do this.
So is there a way to either select or execute a substitution on text as it's being p
ut?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
执行
p
后,[
和]
标记指的是粘贴区域的开始和结束行号(适用于<代码>y 以及)。有关说明,请参阅'[
和']
的帮助。因此,您可以使用这些标记来形成
:s
的工作范围,如:'[,']s///
。然后,这将适用于刚刚拉出或粘贴的区域。当然,它并不短,但如果你关心它,你可以映射它。也许类似于nnoremappp:'[,']s/
。Immediately after the execution of the
p
, the[
and]
marks refer to the start and end line numbers of the pasted region (applies duringy
as well). See the help for'[
and']
for explanation.Thus, you can use these marks to form the range on which to work the
:s
, as:'[,']s///
. This will then work on the region just yanked or pasted. Sure, it's not short, but if you care about it you can map it. Perhaps something likennoremap <Leader>p p:'[,']s/
.克里斯·摩根已经发布了最佳解决方案。但您也可以直接对寄存器中的文本进行某些类型的操作。复制的默认寄存器是
"
,因此您可以执行以下操作:然后粘贴更改的寄存器文本。与多行寄存器文本中的行上下文相关的操作并不容易,因为文本中的文本register 是一个具有一个开始模式 (
^
) 和一个结束模式 ($
) 的单个字符串,但我只是想把它扔在那里 。这是我有时会做的事情。
Chris Morgan already posted the best solution. But you can also do some kinds of manipulation directly on the text in the register. The default register for yank is
"
, so you can do something like:Then paste the altered register text. Not as easy for manipulation related to line-context in multi-line register text, since the text in register is a single string with one begin pattern (
^
) and one end ($
). But still can be useful.Just thought I'd throw it out there, since this is something I sometimes do.