在使用Vim时,我经常想用我刚拉出的代码块替换代码块。

但是当我删除要替换的代码块时,该代码块本身就会进入寄存器,它会擦除我刚刚删除的代码块。所以我已经养成了拉拽,然后插入,然后删除我不想要的东西的习惯,但对于大块的代码,这变得很混乱,试图保持插入的块和要删除的块分开。

那么,在Vim中替换文本的最灵活、最快的方法是什么呢?

是否有一种方法可以删除文本而不将其放入寄存器? 有一种说法吗?“替换下一个单词”或“替换到下一段” 还是使用多寄存器特性的最佳方式?


当前回答

对于你给出的具体例子,如果我理解了问题,那么这可能是可行的:

*Highlight what you want to put somewhere else
*delete (d)
*Highlight the code that you want it to replace
*paste (p)

其他回答

我把以下内容放在我的vimrc中:

noremap  y "*y
noremap  Y "*Y
noremap  p "*p
noremap  P "*P
vnoremap y "*y
vnoremap Y "*Y
vnoremap p "*p
vnoremap P "*P

现在我从剪贴板寄存器中插入和放入,而不必关心默认寄存器中发生了什么。另外一个好处是,我可以轻松地从其他应用中粘贴。我失去了一些功能,我知道(我不再能够管理剪贴板寄存器和默认寄存器的不同复制信息),但我只是无法跟踪多个寄存器/剪贴板。

有一个简单的映射是很方便的,它可以让你用缓冲区替换当前选择。

例如,当你把这个放在你的。vimrc

" it's a capital 'p' at the end
vmap r "_dP

然后,在复制一些东西到寄存器(即用“y”)后,你可以选择你想要替换的文本,并简单地敲击键盘上的“r”。所选内容将被当前寄存器替换。

解释:

vmap - mapping for visual mode
"_d - delete current selection into "black hole register"
P - paste

我找到了一个非常有用的映射:

xnoremap p "_dP

删除的文本被放入“黑洞寄存器”,而被拉拽的文本仍然存在。

来源:http://vim.wikia.com/wiki/Replace_a_word_with_yanked_text

Noremap mm m

and

Noremap mx "_x

是我的处理方式。

如果你用的是Vim,你会有视觉模式,就像选择,但有分离模式,这是vi/ Vim的基础。

您要做的是使用可视模式选择源,然后弹出,然后再次使用可视模式选择目标范围,然后粘贴到默认缓冲区中的文本。

例子:

在文本文件中使用:

1| qwer
2| asdf
3| zxcv
4| poiu

使用以下序列:ggVjyGVkp,你将以:

1| qwer
2| asdf
3| qewr
4| asdf

解释道:

gg: go to first line V: start visual mode with whole lines j: go down one line (with the selection started on the previous lines this grows the selection one line down) y: yank to the default buffer (the two selected lines, and it automatically exits you from visual mode) G: go to the last line V: start visual mode (same as before) k: go up one line (as before, with the visual mode enabled, this grows the selection one line up) p: paste (with the selection on the two last lines, it will replace those lines with whatever there is in the buffer -- the 2 first lines in this case)

这有一点不方便,把最后一个块放在缓冲区,所以它不需要重复粘贴相同的东西,所以你会想要保存源代码到一个命名缓冲区,如“ay”(缓冲区称为“a”)和粘贴,如“ap”(但如果你在编程,你可能不想多次粘贴而是创建一个函数并调用它,对吗?对吧?)。

如果你只使用vi,那么你必须使用不可见的标记,而不是视觉模式,他标记更多关于这个,我很抱歉,但我不太擅长这种不可见标记的东西,我很受视觉模式的污染。