开发者

How to search and replace with a counter-based expression in Vim?

开发者 https://www.devze.com 2023-03-04 06:23 出处:网络
Is there a way to insert the value from some sort of counter variable in Vim :substitute command? For instance, to con开发者_JAVA技巧vert this document:

Is there a way to insert the value from some sort of counter variable in Vim :substitute command?

For instance, to con开发者_JAVA技巧vert this document:

<SomeElement Id="F" ... />
<SomeElement Id="F" ... />
<SomeElement Id="F" ... />

to this resulting document:

<SomeElement Id="1" ... />
<SomeElement Id="2" ... />
<SomeElement Id="3" ... />

I imagine, the command would look like so:

:%s/^\(\s*<SomeElement Id="\)F\(".*\)$/\1<insert-counter-here>\2/g

I am using a very recent Windows build, from their provided installer. I strongly prefer not to install any additional tools. Ideally, I'd like to also avoid having to install scripts to support this, but I'm willing to, if it is the only way to do it.


It is possible to have a counter using the substitute-with-an-expression feature (see :help sub-replace-\=). Unfortunately, since the \= construct allows only expressions, the :let command cannot be used, and therefore, a variable cannot not be set the usual way.

However, there is a simple trick to change the value of a variable in expression if that variable is a list or a dictionary. In that case, its contents could be modified by the map() function.

In such a manner, substitution for the case described in the question would look as follows:

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

The tricky part here is in the substitute part of the replacement. Since it starts with \=, the rest of it is interpreted as an expression by Vim. Thus, 'Id="'.map(n, 'v:val+1').'"' is an ordinary expression. Here a string literal 'Id="' is concatenated (using the . operator) with return value of the function call map(n, 'v:val+1'), and with another string, '"'. The map function expects two arguments: a list (as in this case) or a dictionary, and a string containing expression that should be evaluated for each of the items in the given list or dictionary. Special variable v:val denotes an individual list item. So the 'v:val+1' string will be evaluated to a list item incremented by one.

In this case, we can even simplify the command further:

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

The \zs and \ze pattern atoms are used to set the start and the end of the pattern to replace, respectively (see :help /\zs and :help /\ze). That way the whole search part of the substitute command is matched, but only the part between \zs and \ze is replaced. This avoids clumsy concatenations in the substitute expression.

Either of these two short one-liners completely solves the issue.

For frequent replacements, one can even define an auxiliary function

function! Inc(x)
    let a:x[0] += 1
    return a:x[0]
endfunction

and make substitution commands even shorter:

:let n=[0] | %s/Id="\zsF\ze"/\=Inc(n)/g


Vim wiki instructions seems to be the easiest solution (at least for me).

Example below replaces all occurences of PATTERN with REPLACE_[counter] (REPLACE_1, REPLACE_2 etc.):

:let i=1 | g/PATTERN/s//\='REPLACE_'.i/ | let i=i+1

To answer the question it might look like this:

:let i=1 | g/SomeElement Id="F"/s//\='SomeElement Id="'.i.'"'/ | let i=i+1

Alternative solution

If anyone is interested in a solution with %s syntax I would advise to look at the @ib. answer which is:

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


Hmm this it little tricky one. Here is what I got so far. Try these 2 map command in a vim session:

:nmap %% :let X=1<cr>1G!!
:nmap !! /^\s*<SomeElement Id="F"<cr>:s/F"/\=X.'"'/<cr>:let X=X+1<cr>!!

Once that is there press %% to start the fun part :)

It makes your given file as:

<SomeElement Id="1" ... />
<SomeElement Id="2" ... />
<SomeElement Id="3" ... />
<SomeElement Id="4" ... />

Explanation:

First nmap command is mapping following sequences to keystrokes %%:

  • initializing variable X to 1
  • moving to start of first file
  • calling another mapped keystroke !!

Second nmap command is mapping following sequences to keystrokes !!:

  • Search for next occurrence of pattern ^\s*<SomeElement Id="F"
  • If above pattern is found then search and replace F" by variable X and a quote "
  • increment the vim variable X by 1
  • Recursively call itself by making a call to !!
  • Single dot . is used for concatenation of strings in vim, very similar to php

This recursive calls stop when pattern ^\s*<SomeElement Id="F" is not found anymore in the file.


Very simple solution. I've had to do this several times.

:let i=1 | g/^\(\s*<SomeElement Id="\)F\(".*\)$/s//\=submatch(1).i.submatch(2)/ | let i=i+1

Based off of the following tip. http://gdwarner.blogspot.com/2009/01/vim-search-and-replace-with-increment.html


Put this in your vimrc or execute it in your current session:

function! Inc(x) 
  let a:x[0] += 1 
  return a:x[0] 
endfunction

function IncReplace(pos, behind, ahead, rep) 
  let poss=a:pos-1 
  let n=[poss] 
  execute '%s/' . a:behind . '\zs' . a:rep . '\ze' . a:ahead . '/\=Inc(n)/g' 
endfunction

Then execute :call IncReplace(1, 'Id="', '"', 'F')

The first argument is the number you want to start from, the second is what you want to match behind the number, the third is what you want to match ahead of the number and the fourth is what you actually want to replace.


Maybe plugin increment.vim will help

0

精彩评论

暂无评论...
验证码 换一张
取 消