本文介绍了vim让搜索并替换为增量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想搜索并替换,在此处的线程之后实施,很遗憾,它不起作用. gVim查找/替换为计数器

i wanted to search and replace, implement after the thread here, unfortunately it does not work.gVim find/replace with counter

有人可以帮助我吗?

工作

:let n=[0] | %s/|-\n|/\=map(n,'v:val+1')/g

不起作用

:let n=[0] | %s/|-\n|/BLABLA\=map(n,'v:val+1')/g

为什么?如何屏蔽功能?

Why?how do I mask the functionon?

示例

{| class="wikitable"
! Number
! Name
! Type
! Default Value
! Duration
! Activation
! Status
! EDIT
|-
|
| Adobe-Dummy-Cart-Total
| Custom Script
|
| Pageview
| Active
| Approved
| Edit
|-
|
| basePrice

我要替换

|-
|

|-
| 1

|-
| 2

推荐答案

:help sub-replace-expression(强调我的想法)

您不能做自己想做的事;替换字符串要么是子表达式(以\=开头),要么是文字(如果不是).

You cannot do what you want; the replacement string is either a subexpression (when it starts with \=), or a literal (when it does not).

相反,您需要重写子表达式以编程方式连接字符串(:help expr-.):

Instead, you need to rewrite the subexpression to concatenate the string programmatically (:help expr-.):

:let n=[0] | %s/|-\n|/\="BLABLA".map(n,'v:val+1')[0]/g

[0]接受由map生成的数组的内容是必要的,原因有两个:用数组替换将引入不需要的换行符,并使得无法与字符串连接.

[0] to take the content of the array produced by map is necessary for two reasons: replacing with an array will introduce an unwanted newline, and make concatenation with a string impossible.

但是对于您的示例,如果您没有引入除数字之外的任何字符串,则可能没有必要-即,如果您不需要该空格(:help /\zs):

For your example though, it may not necessary, if you are not introducing any string besides the number - i.e. if you don't need that space (:help /\zs):

:let n=[0] | %s/|-\n|\zs/\=map(n,'v:val+1')[0]/g

当然,您可以将两者结合起来,以针对您的特定情况提供完美的去离子化解决方案:

Of course, you can combine the two, for a perfect demoisturised solution to your specific situation:

:let n=[0] | %s/|-\n|\zs/\=" ".map(n,'v:val+1')[0]/g

这篇关于vim让搜索并替换为增量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-30 04:07