Vim 正则表达式替代问题
我试图在 VIM 中将所有多个“-”字符(从行首开始)替换为“=”
pe 将“-----”替换为“=====”
或用“==========”替换“----------”
我创建了这个正则表达式:
%s/^-\{2,}/= ????/g
有谁知道如何复制“=”替换? (“=”后面要加什么)
I'm trying to replace in VIM all multiple "-" characters (from the start of lines) with "="
p.e. replace "-----" with "====="
or replace "----------" with "=========="
I created this regex:
%s/^-\{2,}/= ????/g
Does anyone know how I can replicate the "=" substitution?
(what do I have to put after "=")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
试试这个:
或者:
请参阅
:help sub-replace-\=
了解更多详细信息。Try this:
or:
See
:help sub-replace-\=
for more details.我确信有更好的答案,但实际上,为了简单起见,我会将其作为两个单独的操作来执行:
首先替换所有重复出现的内容,这会将
-----
转换为===-
。然后使用第二个修复剩余部分 (=-
)。不过,如果可能的话,我很想看到更优雅的答案。I'm sure there's a better answer, but practically speaking, I would do this as two separate operations just for simplicity:
First replace all double occurrences, which would turn
-----
into====-
. Then fix the leftovers (=-
) using the second. I would love to see the more elegant answer, though, if it is possible to do.从技术上讲,
%s/-/=/g
可以完成这项工作,但是是在整个文件的每个-
中。如果您要替换的行确实以
-
开头,我会这样做:或者,如果您在第一个
-
之前有一些空格:剩下的问题到达像这样的台词:
它们变成:
要解决这个问题,有很多方法。我不是那位大师建议一种非常通用的方法,但这可能适用于单词之间的破折号:
Technically,
%s/-/=/g
does the job, but on the entire file, in every-
.If the lines you want to substitute do start with
-
I'd do it this way:Or, if you have some space before the first
-
:The remaining problem arrives in lines like this:
They turn into:
To solve that, there are many ways. I not that master to suggest a very general way, but this may work for dashes between words: