[英]vim let search and replace with increment
i wanted to search and replace, implement after the thread here, unfortunately it does not work.我想搜索和替换,在这里线程后实现,不幸的是它不起作用。 gVim find/replace with counter
gVim 用计数器查找/替换
Can anyone help me?谁能帮我?
Working在职的
:let n=[0] | %s/|-\n|/\=map(n,'v:val+1')/g
Not working不工作
:let n=[0] | %s/|-\n|/BLABLA\=map(n,'v:val+1')/g
Why?为什么? how do I mask the functionon?
我如何屏蔽功能?
Example例子
{| class="wikitable"
! Number
! Name
! Type
! Default Value
! Duration
! Activation
! Status
! EDIT
|-
|
| Adobe-Dummy-Cart-Total
| Custom Script
|
| Pageview
| Active
| Approved
| Edit
|-
|
| basePrice
I wana replace我想更换
|-
|
with和
|-
| 1
|-
| 2
:help sub-replace-expression
(emphasis mine) :help sub-replace-expression
(强调我的)
When the substitute string starts with "\\=" the remainder is interpreted as an 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).替换字符串是子表达式(当它以
\\=
开头时)或文字(当它不是时)。
Instead, you need to rewrite the subexpression to concatenate the string programmatically ( :help expr-.
):相反,您需要重写子表达式以编程方式连接字符串(
:help expr-.
):
:let n=[0] | %s/|-\n|/\="BLABLA".map(n,'v:val+1')[0]/g
[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. [0]
获取map
生成的数组的内容是必要的,原因有两个:用数组替换会引入不需要的换行符,并且无法与字符串连接。
For your example though, it may not necessary, if you are not introducing any string besides the number - ie if you don't need that space ( :help /\\zs
):但是,对于您的示例,如果您没有引入除数字之外的任何字符串,则可能没有必要 - 即如果您不需要该空间(
: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
When you use :help sub-replace-expression
, the entire replacement must be a Vimscript expression (that starts with \\=
).当您使用
:help sub-replace-expression
,整个替换必须是 Vimscript 表达式(以\\=
开头)。 You cannot just prepend replacement text (as in BLABLA\\=map(n,'v:val+1')
. Use string concatentation instead. A complication with your map
approach is that this returns a List, not a String, so we need to select the only (counter) element of the List. To refer to the existing matched text, use submatch(0)
instead of \\0
or &
(or avoid the need for a reference by using \\zs
at the end of the pattern instead).您不能只在替换文本之前添加替换文本(如
BLABLA\\=map(n,'v:val+1')
。改用字符串连接。 map
方法的一个复杂之处是它返回一个列表,而不是一个字符串,所以我们需要选择列表的唯一(计数器)元素。要引用现有的匹配文本,请使用submatch(0)
而不是\\0
或&
(或者通过在模式末尾使用\\zs
来避免需要引用)。
:let n=[0] | %s/|-\n|/\=submatch(0) . " " . map(n,'v:val+1')[0]/g
Using \\zs
we can make the solution easier使用
\\zs
我们可以使解决方案更容易
:let c=1 | g/^|-\n|\zs/ s//\=' '.c/g | let c+=1
^ ............ start of line
\n ........... line break
| ........... vertical bar
We are concatenating a space \\=' '
with the counter c
我们将一个空格
\\=' '
与计数器c
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.