2

Is it possible to search pattern like this: some_name{number}: for example r001. And then incrementally increment each search result number by one. For example for such input:

r001
...
r001
...
r001
...

Desired output will be:

r002
... 
r003
...
r004
Vardan Hovhannisyan
  • 1,101
  • 3
  • 17
  • 40
  • Very common question, same as [1](http://stackoverflow.com/q/5942360) [2](http://stackoverflow.com/q/8073780) [3](http://stackoverflow.com/q/9076379), addressed in many Vim tips [4](http://vim.wikia.com/wiki/Making_a_list_of_numbers) [5](http://vim.wikia.com/wiki/Step_increment_and_replace) and Vim scripts [6](http://www.vim.org/scripts/script.php?script_id=145) [7](http://www.vim.org/scripts/script.php?script_id=189) [8](http://www.vim.org/scripts/script.php?script_id=670). – glts Jul 30 '13 at 17:53

1 Answers1

3

Following would replace each number in the entire file with an incrementing number starting from two.

let i=2|g/\v\d+$/s//\=i/|let i=i+1

Output

r2
...
r3
...
r4
...

Breakdown

let i=2     : Initializes variable i to 2
g/\v\d+$/   : a global command to search for numbers at the end of lines
s//\=i/     : a substitute command to replace the search matches with 
              the contents of i
let i=i+1   : increment i for the next match

All that's left to do is incorporate the commands to pad with zero's: Vim: padding out lines with a character

Community
  • 1
  • 1
Lieven Keersmaekers
  • 57,207
  • 13
  • 112
  • 146