用粘贴缓冲区的内容替换单词?
我需要在文件中进行一堆单词替换,并希望使用 vi 命令来完成,而不是使用诸如 :%s///g
这样的 EX 命令。
我知道这是替换当前光标位置处的单词的典型方法:cw
但是有没有办法用未命名寄存器的内容作为替换文本并且不覆盖寄存器?
I need to do a bunch of word replacements in a file and want to do it with a vi command, not an EX command such as :%s///g
.
I know that this is the typical way one replaces the word at the current cursor position: cw<text><esc>
but is there a way to do this with the contents of the unnamed register as the replacement text and without overwriting the register?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
我认为“粘贴”是指未命名的(复制/放置/更改/删除/替换)寄存器,对吧? (因为这是会被更改命令覆盖的寄存器。)
通常通过键入
"
然后输入寄存器的名称(单个字符)来指定寄存器,例如"ay
然后"ap
拉入寄存器a
,然后放入寄存器a
的内容。更改命令也是如此。在这种情况下,如果您不希望使用更改命令删除的文本出现在任何地方,可以使用黑洞寄存器"_
:"_cw
然后进入插入模式。 ,您可以按 ctrl-R,然后按您想要的寄存器(可能是"
)以放入该寄存器的内容。"*
- 选择寄存器(中键粘贴)"+
- 剪贴板寄存器(也可能通过终端使用 ctrl-shift-v 进行访问)""< /code> - vim 的默认(未命名) yank/put/change/delete/substitute 寄存器。
简短回答:
"_cw^R"
编辑:正如其他人所建议的那样,您当然可以使用不同的寄存器来进行将文本放入默认寄存器的复制(或其他操作)。不过,您并不总是首先想到这一点,因此最好执行单个更改命令而不会将其破坏。虽然还没有完全被吹走。有编号的寄存器
"0
到"9
:I'm thinking by "paste" you mean the unnamed (yank/put/change/delete/substitute) register, right? (Since that's the one that'd get overwritten by the change command.)
Registers are generally specified by typing
"
then the name (single character) of the register, like"ay
then"ap
to yank into registera
, then put the contents of registera
. Same goes for a change command. In this case, if you don't want the text you remove with the change command to go anywhere, you can use the black hole register"_
:"_cw
. Then once in insert mode, you can hit ctrl-R followed by the register you want (probably"
) to put in the contents of that register."*
- selection register (middle-button paste)"+
- clipboard register (probably also accessible with ctrl-shift-v via the terminal)""
- vim's default (unnamed) yank/put/change/delete/substitute register.Short answer:
"_cw^R"
Edit: as others are suggesting, you can of course use a different register for the yank (or whatever) that got your text into the default register. You don't always think of that first, though, so it's nice to do a single change command without blowing it away. Though it's not totally blown away. There are the numbered registers
"0
through"9
:使用这篇文章中的信息,我形成了这个有用的映射。我选择“cp”,因为它表示“更改粘贴”
nmapcp "_cw"
编辑:
我也更进一步并支持任何动议。
要获得与上面命令等效的命令,可以使用 cpw 来表示“更改粘贴单词”
Using the information in this post, I have formed this useful mapping. I chose 'cp' because it signifies "change paste"
nmap <silent> cp "_cw<C-R>"<Esc>
EDIT:
Also I took this a step further and supported any motion.
To get the equivalent of command above it would be
cpw
for "change paste word"如果光标位于要替换为未命名寄存器内容的单词上,则可以使用
viwp
。v
切换到可视模式,iw
选择内部字,p
将寄存器的内容放在其位置。在实践中,当我需要将一个单词(函数名称等)替换为另一个单词时,我将移至要用作替换的单词,
yiw
将内部单词拉入未命名寄存器,然后移至我要替换的单词,并用viwp
替换它。用一个单词替换另一个单词的非常快速的方法。如果您搜索 (/
) 要替换的单词来找到它,则只需按n
即可找到您需要替换的下一个匹配项。显然不能替代使用:%s/find/replace/g
,但对于一些快速替换来说它会很方便,特别是如果您的寄存器中已经有新单词的话。If your cursor is on the word you want to replace with the contents of the unnamed register, you can use
viwp
.v
switches to visual mode,iw
selects the inner word, andp
puts the contents of the register in its place.In practice, when I need to replace one word (function name, etc.) with another, I'll move to the one to use as a replacement,
yiw
to yank the inner word to the unnamed register, then move to the word I'm replacing, andviwp
to replace it. Pretty quick way of substituting one word for another. If you searched (/
) for the word you're replacing to get to it, you can then just hitn
to get to the next occurrence you need to replace. Obviously no substitute for using:%s/find/replace/g
, but for a couple of quick substitutions it can be handy, especially if you already have the new word in a register.为此,您可以使用 vim 的可视模式。例如复制一个单词:
ye
,然后用复制的单词覆盖另一个单词:vep
You can use the visual mode of vim for this. e.g. copy a word:
ye
and then overwrite another one with the copied word:vep
如果您使用命名寄存器(即使用
"ay
或"ad
等来填充粘贴寄存器),您可以执行类似的操作cwa
它将用寄存器
a
的内容替换该单词。据我所知,您不能使用默认寄存器,因为当您cw
时,它将填充该命令剪切的单词。If you make use of a named register (ie. use
"ay
or"ad
, etc., to fill your paste register), you can do something likecw<CTRL-R>a<esc>
Which will replace the word with the contents of register
a
. As far as I can tell, you can't use the default register because when youcw
it'll be filled with the word that was cut by that command.你是指系统粘贴缓冲区还是vi寄存器?
如果您想使用系统粘贴缓冲区,那么您可以执行
dw"+P
-"
选择寄存器,然后"+
是系统粘贴缓冲区。否则复制到非默认寄存器,用
"ay
复制到寄存器a
,然后替换某些内容,执行dw"aP< /代码>
Do you mean the system paste buffer or the vi register?
If you want to use the system paste buffer then you are fine and could do
dw"+P
-"
chooses a register, and"+
is the system paste buffer.Otherwise copy into the non-default register with say
"ay
to copy into registera
and then to replace something dodw"aP
您可以使用
yw
来拉取单词,然后您可以使用viwp
更改被拉出的单词,以拉取内部单词并粘贴先前拉出的单词。You can use
yw
to yank the word, then you can change the word with yanked word byviwp
to yank inner word and paste previously yanked word.如果您使用 neovim,您可以在可视模式下使用
P
,例如,它将用
"
寄存器的内容覆盖当前单词,而不更改寄存器。它也适用于其他寄存器:
将用寄存器的内容替换单词
"a
参见 https://neovim.io/doc/user/change.html#v_P
If you use neovim you can use
P
in visual mode, for examplewhich will overwrite the current word with the content of the
"
register without changing the register.It also works with other registers:
will replace the word with content of register
"a
See https://neovim.io/doc/user/change.html#v_P
您可以使用寄存器来实现以下目的:
在寄存器中首先放置替换文本
"ay
其中
a
是寄存器名称,那么您可以使用该寄存器替换
ve"ap
You can use registers for that:
first place replacement text in register
<mark some text>"ay
where
a
is register namethen you can use that register in replacement
ve"ap
或者您可以执行 Shift+v-p (选择整行并粘贴到其位置)
Or you could do Shift+v-p (select the whole line and paste in its' place)
我看到很多变化,但我一直在寻找最懒的方法...
您刚刚更改并想要复制的单词上的任何位置:yiw
移动到您何时更改/粘贴的第一个单词:viwP
和如果其他需要同样的治疗: 。
诀窍是大写 P 不会用刚刚删除的文本替换默认寄存器。
I see a lot of variation but i'm always looking for the laziest way to do it...
Anywhere on the word you just changed and want to copy over : yiw
Move to the first word you when to change/paste : viwP
And if other need the same treatment : .
The trick is upper case P doesn't replace the default register with the just deleted text.