用粘贴缓冲区的内容替换单词?

发布于 2024-08-25 11:04:01 字数 179 浏览 6 评论 0原文

我需要在文件中进行一堆单词替换,并希望使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

疯了 2024-09-01 11:04:02

我认为“粘贴”是指未命名的(复制/放置/更改/删除/替换)寄存器,对吧? (因为这是会被更改命令覆盖的寄存器。)

通常通过键入 " 然后输入寄存器的名称(单个字符)来指定寄存器,例如 "ay 然后 "ap 拉入寄存器 a,然后放入寄存器 a 的内容。更改命令也是如此。在这种情况下,如果您不希望使用更改命令删除的文本出现在任何地方,可以使用黑洞寄存器 "_: "_cw 然后进入插入模式。 ,您可以按 ctrl-R,然后按您想要的寄存器(可能是 ")以放入该寄存器的内容。

  • "* - 选择寄存器(中键粘贴)
  • "+ - 剪贴板寄存器(也可能通过终端使用 ctrl-shift-v 进行访问)
  • ""< /code> - vim 的默认(未命名) yank/put/change/delete/substitute 寄存器。

简短回答:"_cw^R"

编辑:正如其他人所建议的那样,您当然可以使用不同的寄存器来进行将文本放入默认寄存器的复制(或其他操作)。不过,您并不总是首先想到这一点,因此最好执行单个更改命令而不会将其破坏。虽然还没有完全被吹走。有编号的寄存器 "0"9

Vim 用复制和删除命令中的文本填充这些寄存器。

编号寄存器 0 包含来自最近的 yank 命令的文本,除非该命令用 ["x] 指定了另一个寄存器。

编号寄存器 1 包含由最近的删除或更改命令删除的文本,除非该命令指定了另一个寄存器或文本少于一行(然后使用小的删除寄存器)。使用以下移动命令的删除运算符是一个例外:%(, )`、<代码>/、<代码>?、<代码>n、<代码>N、<代码>{和<代码>}.那么寄存器 "1 总是被使用(这是 Vi 兼容的)。如果删除是在一行内,那么 "- 寄存器也会被使用。

随着每次连续的删除或更改,Vim 会将寄存器 1 的先前内容移入寄存器 2,将寄存器 2 移入寄存器 3,依此类推,并丢失先前的内容。
寄存器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 register a, then put the contents of register a. 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:

Vim fills these registers with text from yank and delete commands.

Numbered register 0 contains the text from the most recent yank command, unless the command specified another register with ["x].

Numbered register 1 contains the text deleted by the most recent delete or change command, unless the command specified another register or the text is less than one line (the small delete register is used then). An exception is made for the delete operator with these movement commands: %, (, ), `, /, ?, n, N, { and }. Register "1 is always used then (this is Vi compatible). The "- register is used as well if the delete is within a line.

With each successive deletion or change, Vim shifts the previous contents of register 1 into register 2, 2 into 3, and so forth, losing the previous
contents of register 9.

谷夏 2024-09-01 11:04:02

使用这篇文章中的信息,我形成了这个有用的映射。我选择“cp”,因为它表示“更改粘贴”

nmapcp "_cw"

编辑:

我也更进一步并支持任何动议。

要获得与上面命令等效的命令,可以使用 cpw 来表示“更改粘贴单词”

"This allows for change paste motion cp{motion}
nmap <silent> cp :set opfunc=ChangePaste<CR>g@
function! ChangePaste(type, ...)
    silent exe "normal! `[v`]\"_c"
    silent exe "normal! p"
endfunction

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"

"This allows for change paste motion cp{motion}
nmap <silent> cp :set opfunc=ChangePaste<CR>g@
function! ChangePaste(type, ...)
    silent exe "normal! `[v`]\"_c"
    silent exe "normal! p"
endfunction
情话墙 2024-09-01 11:04:02

如果光标位于要替换为未命名寄存器内容的单词上,则可以使用 viwpv 切换到可视模式,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, and p 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, and viwp 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 hit n 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.

指尖上的星空 2024-09-01 11:04:02

为此,您可以使用 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

寄人书 2024-09-01 11:04:02

如果您使用命名寄存器(即使用 "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 like

cw<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 you cw it'll be filled with the word that was cut by that command.

青丝拂面 2024-09-01 11:04:02

你是指系统粘贴缓冲区还是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 register a and then to replace something do dw"aP

百合的盛世恋 2024-09-01 11:04:02

您可以使用yw来拉取单词,然后您可以使用viwp更改被拉出的单词,以拉取内部单词并粘贴先前拉出的单词。

You can use yw to yank the word, then you can change the word with yanked word by viwp to yank inner word and paste previously yanked word.

a√萤火虫的光℡ 2024-09-01 11:04:02

如果您使用 neovim,您可以在可视模式下使用 P,例如,

viwP

它将用 " 寄存器的内容覆盖当前单词,而不更改寄存器。

它也适用于其他寄存器:

"aviwP

将用寄存器的内容替换单词 "a

参见 https://neovim.io/doc/user/change.html#v_P

If you use neovim you can use P in visual mode, for example

viwP

which will overwrite the current word with the content of the " register without changing the register.

It also works with other registers:

"aviwP

will replace the word with content of register "a

See https://neovim.io/doc/user/change.html#v_P

悲凉≈ 2024-09-01 11:04:02

您可以使用寄存器来实现以下目的:

在寄存器中首先放置替换文本
"ay

其中 a 是寄存器名称,

那么您可以使用该寄存器替换

ve"ap

You can use registers for that:

first place replacement text in register
<mark some text>"ay

where a is register name

then you can use that register in replacement

ve"ap

痴者 2024-09-01 11:04:02

或者您可以执行 Shift+v-p (选择整行并粘贴到其位置)

Or you could do Shift+v-p (select the whole line and paste in its' place)

赢得她心 2024-09-01 11:04:02

我看到很多变化,但我一直在寻找最懒的方法...

您刚刚更改并想要复制的单词上的任何位置: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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文