Vim Tips Wiki
Advertisement

Previous TipNext Tip

Tip: #971 - Substitute with incrementing numbers

Created: August 13, 2005 10:51 Complexity: advanced Author: Ciaran McCreesh <ciaranm--AT--gentoo.org> Version: 6.0 Karma: 36/19 Imported from: Tip#971

Say you have a document:


foo bar 
bar foo 
bar 
foo 


and you want to replace the first foo with blah_1, the second with blah_2, the third with blah_3 and so on. There are a number of options. The most obvious is:


:let i=1

Comments

alas, setreg() not found in vim 6.1

Anonymous , August 14, 2005 4:25


The canonical way to solve this is by way of a function. Define something like

fun CountUp()
let ret = g:i
let g:i = g:i + 1
return ret
endf

Then you can simply say

let i = 1 | %s/foo/\="bar_" . CountUp()/g

pagaltzis--AT--gmx.de , August 15, 2005 13:27


Advertisement