vi搜索复制粘贴搜索副本

ben*_*ete 2 vi vim search copy paste

我只是想知道是否有人可以帮助如何使用vi执行以下操作.

我有一个文本文件,它可能包含类似的东西

start of text file:
--something1.something2--
--anotherThing1.something2--
end of text file:
Run Code Online (Sandbox Code Playgroud)

如果我想取这行并通过搜索与第一次出现的[A-Za-z0-9]副本相匹配的任何内容转换为缓冲区,然后将其附加到第一个出现之前的同一行 -

start of text file:
--something1.something2.something1--
--anotherThing1.something2.anotherThing1--
end of text file:
Run Code Online (Sandbox Code Playgroud)

是否有一个VI命令来执行此操作?

干杯本

ste*_*anB 5

:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/gc
Run Code Online (Sandbox Code Playgroud)

或者没有要求确认每次更换:

:%s/--\([a-zA-Z0-9]*\).\(.*\)--/--\1.\2.\1--/g
Run Code Online (Sandbox Code Playgroud)

将产生:

--something1.something2.something1--
--anotherThing1.something2.anotherThing1--
Run Code Online (Sandbox Code Playgroud)

从:

--something1.something2--
--anotherThing1.something2--
Run Code Online (Sandbox Code Playgroud)

如果你想复制' - '之后的第一个单词,直到第一个'.'.并追加'.' 和最后一个' - '之前找到的单词.

使用vim.

评论意见:

有人提到当有多个单词时它不起作用等等.我测试了以下内容:

start of text file:
--something1.something2.something3.something4.something5.something6--
--anotherThing1.something2.anotherThing3.anotherThing4.anotherThing5--
end of text file:
Run Code Online (Sandbox Code Playgroud)

用以上表达式替换后:

start of text file:
--something1.something2.something3.something4.something5.something6.something1--
--anotherThing1.something2.anotherThing3.anotherThing4.anotherThing5.anotherThing1--
end of text file:
Run Code Online (Sandbox Code Playgroud)