Saturday, June 30, 2012

Substituting with an incrementing number with VI

If you need to replace a pattern with an incrementing number in VI, you can use the line() function.

:%s/PATTERN/\=OFFSET+line(".") . "MORE TEXT"/g

Change out PATTERN with what you are searching for.

Change out OFFSET with a number to add or subtract as necessary from the line number to get the correct numerical value.

Change out MORE TEXT with anything else you want to append.  But remember that because you are in code eval mode, you need to append text using the period operator and quotes.

No comments:

Post a Comment