[英]Vim copy and paste
My previous question seems to be a bit ambiguous, I will rephrase it: 我之前的问题似乎有点含糊不清,我会改写它:
I have a file like this: 我有一个这样的文件:
copythis abc
replacethis1 xyz
qwerty replacethis2
hasfshd replacethis3 fslfs
And so on...
NOTE: replacethis1, replacethis2, replacethis3, ... could be any words 注意: replacethis1,replacethis2,replacethis3,...可以是任何单词
How do I replace "replacethis1","replacethis2","replacethis3",.. word by "copythis" word by using minimum vim commands. 如何使用最小vim命令将“replacethis1”,“replacethis2”,“replacethis3”,... word替换为“copythis”字样。
One way I can do is by these steps: 我能做的一种方法是通过以下步骤:
Is there a better way to do this in VIM (using less number of vim commands)? 有没有更好的方法在VIM中执行此操作(使用较少数量的vim命令)?
Since you changed your question, I'd do it this way: 既然你改变了问题,我会这样做:
Move to the first "replacethis1" and type cw (change word), then type "copythis" manually. 移动到第一个“replacethis1”并键入cw(更改单词),然后手动键入“copythis”。
Move to the next "replacethis", hit . 移动到下一个“replacethis”,点击。 (repeat last operation)
(重复上一次操作)
Move to the next "replacethis", hit ., 移动到下一个“replacethis”,点击。,
and so on, and so on. 等等等等。
If "copythis" is a small word, I think this is the best solution. 如果“copythis”是一个小词,我认为这是最好的解决方案。
需要包含数字,每行可能有多个实例:
:%s/replacethis\d/copythis/g
Given that "replacethis[1-3]" can be arbitrary unrelated words, the quickest/simplest way to do this globally would be: 鉴于“replacethis [1-3]”可以是任意无关的单词,全球最快/最简单的方法是:
:%s/replacethis1\|replacethis2\|replacethis3/copythis/g
(Note that you need to use \\| to get the pipes to function as "or". Otherwise, vim will look for the literal | character.) (注意,您需要使用\\ |来使管道充当“或”。否则,vim将查找文字字符。)
:%s/copythis/replacethis/g
To replace all occurrences of copythis with replacethis. 用replacethis替换所有出现的copythis。 Or you can specify a range of line numbers like:
或者您可以指定一系列行号,例如:
:8,10 s/copythis/replacethis/g
Note, the /g on the end will tell it to replace all occurrences. 注意,末尾的/ g将告诉它替换所有出现的事件。 If you leave that off it will just do the first one.
如果你把它关掉它就会做第一个。
create this mapping: 创建此映射:
:map z cwcopythis^[ :map z cwcopythis ^ [
( ^[ is the escape character, you can type it in vim using Ctrl+V Ctrl+[ ) (^ [是转义字符,您可以使用Ctrl + V Ctrl + [在vim中键入它)
go to each word you want to replace and press z 转到要替换的每个单词,然后按z
I've been struggling with this for a long time too, I think I just worked out the cleanest way: 我也一直在努力解决这个问题,我想我只是用最干净的方式解决了这个问题:
Use whichever command is cleanest to put copythis into register r: 使用最干净的命令将copythis放入寄存器r:
/copythis
"rye
Then go to the replacement and replace it with the contents of r: 然后去替换并用r的内容替换它:
/replacethis
cw<CTRL-R>r<ESC>
Then you can just nnnnnnn
for the rest of them, or if they're wildly different just go to the beginning of each and hit .
然后你就可
nnnnnnn
其余的人提供帮助,或者如果他们的情况完全不同,那就去每个人的开头然后点击.
The key is replacing and pasting in one step so you can use .
关键是一步替换和粘贴,以便您可以使用
.
later. 后来。
You can remap eg the m
key in normal mode to delete the word under the cursor and paste the buffer: :nnoremap m "_diwP
. Then you can just copy the desired word, move the cursor anywhere onto the to-be-replaced word and type m
. 您可以在正常模式下重新映射例如
m
键以删除光标下的单词并粘贴缓冲区:: :nnoremap m "_diwP
。然后您可以复制所需的单词,将光标移动到要替换的单词上的任意位置类型m
。
EDIT: Mapping to m is a bad idea since it is used to mark locations. 编辑:映射到m是一个坏主意,因为它用于标记位置。 But you can use eg
;
但你可以使用eg
;
anyway. 无论如何。
如果你需要多次做同样的动作 - 用一行的第一个单词和下一行的第二个单词交换,我说你可以录制一个宏并在需要时调用它
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.