如何在 Vim 中复制一个单词并将其粘贴到另一个单词上?
我知道如何复制一个单词,但我似乎覆盖了剪贴板中的内容,因为当我尝试复制一个单词时,它似乎不起作用。
要复制单词,我可以使用
bye
如何复制单词?
I know how to copy a word, but I seem to overwrite what is in my clipboard because, when I try to copy over a word, it does not seem to be working.
To copy a word, I can use
bye
How to copy over a word?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
也许您只想这样做:
这将直观地选择一个新单词,然后粘贴到它上面。
现在,如果你不想在执行此操作时丢失寄存器,你也可以放入你的 vimrc:
Perhaps you just want to do this:
which will visually select a new word, and paste over it.
Now, if you don't want to lose your register when doing this, you can also put in your vimrc:
像往常一样复制源单词(例如,使用
yiw
)并用于粘贴到目标单词。
由于可视模式下的
p
命令(请参阅:help v_p
)不会改变编号寄存器
0
包含最近的文本yank 命令,可以反复粘贴相同的复制单词。
Copy the source word as usual (e.g., with
yiw
) and useto paste over the destination word.
Since the
p
command in Visual mode (see:help v_p
) does not alterthe numbered register
0
containing the text from the most recentyank command, the same copied word can be pasted over and over again.
执行此操作一次:
ciw0
然后,要始终使用您提取的文本替换单词,请执行以下操作:
.
您可以像这样使用搜索:
/foo< ;Cr>
.nnn
它之所以有效,是因为:
ciw
替换内部单词,并且0
使用最近拉出的寄存器来替换它,然后也使用.
。遗憾的是,如果您直观地选择要替换的文本并使用
.
,则此方法不起作用。请注意,如果您最初使用视觉选择来选择要替换的文本并执行了
c0
,那么之后.
将替换与中包含的相同长度的字符视觉选择。Do this once:
ciw<C-r>0
Then to replace words always using the text you yanked do:
.
You can use search with it like this:
/foo<Cr>
.n.n.n
It works because:
ciw
replaces inner word and<C-r>0
uses the most recently yanked register to replace it, which.
then also uses.Sadly, this does not work if you visually select text you wish to replace and use
.
.Note that if you originally used visual selection to select the text to replace and did
c<C-r>0
then after that.
will replace the same length of characters as was included in the visual selection.您可以使用寄存器 0,其中包含刚刚覆盖的“剪贴板”文本。对于您的示例,您可以拉出要粘贴的文本,将光标放在单词“bye”中的某处,然后输入
ciw
[cut in word;删除光标下的单词并进入插入模式]ctrl-r 0
[register 0;从寄存器 0 粘贴文本]您可以使用
:disp
查看所有寄存器中的内容。正如 Daenyth 所说,您可以使用"x
[del/cut/yank command] 拉入特定寄存器,并在命令模式下使用"xp
或 ctrl 粘贴-r x 来自插入/替换。You could use register 0, which contains the just-overwritten "clipboard" text. For your example, you could yank the text to paste, put the cursor somewhere in the word "bye", and type
ciw
[cut in word; deletes the word under the cursor and goes to insert mode there]ctrl-r 0
[register 0; paste text from register 0]You can see what's in all the registers with
:disp
. As Daenyth says, you can yank into a particular register with"x
[del/cut/yank command] and paste with"xp
from command mode, orctrl-r x
from insert / replace.当您删除一个单词时,它会被放入
"
寄存器中,这是用于粘贴的默认寄存器,因此当您删除要替换的单词时,它将取代中前一个单词的位置” 注册。但是,前一个单词将在寄存器
0
中,前一个单词将在1
中,依此类推 - 您可以随时通过运行:reg 并查看寄存器的内容。因此,要替换单词,您可以首先复制第一个单词(
。 (您也可以先删除单词 (yiw
),然后“更改”要替换的单词(ciw
),然后从寄存器插入<通过按ctrl-r
,0
,code>0diw
),然后从寄存器0
中粘贴:"0p
。When you delete a word it is put in the
"
register which is the default register for pasting, so when you delete the word you want to replace, it will take the previous word's place in the"
register. However, the previous word will be in register0
, the one before in1
and so on – you can at any time see this by running:reg
and see the registers' contents. So, to replace a word you can first copy the first word (yiw
), then “change” the word you want to replace (ciw
) and then insert from register0
by hittingctrl-r
,0
. (You could also first delete the word (diw
) and then paste from register0
:"0p
.简单的解决方案是相反:首先粘贴新单词,然后删除旧单词。
The easy solution is to do it the other way around: first paste the new word, then delete the old one.
yiw
- 复制单词ciw
- 剪切一个单词然后将光标移到要粘贴的位置并按
p
yiw
- Copies a wordciw
- Cuts a wordThen take the cursor wherever you want to paste and press
p
您可以通过在寄存器前面添加
"a
(其中a
是寄存器的名称)来y
ank到寄存器中。请参阅如何使用 vim 寄存器b"aye
You can
y
ank into a register by prepending it with"a
(wherea
is the name of the register). See How to use vim registersb"aye