Question

In Vim, I want to do a search and replace that includes:

[0-9]*

And in the replace part, I want whatever was in that vague range to be carried over and used.

For example let's say I have this:

getNumber(42).roundToTenth();
getNumber(43).roundToTenth();
getNumber(44).roundToTenth();
getNumber(45).roundToTenth();
getNumber(46).roundToTenth();
getNumber(47).roundToTenth();

and want to do a search and replace to change it to

DontGetNumber(42).roundDownTenth();
DontGetNumber(43).roundDownTenth();
DontGetNumber(44).roundDownTenth();
DontGetNumber(45).roundDownTenth();
DontGetNumber(46).roundDownTenth();
DontGetNumber(47).roundDownTenth();

How do I do that?

Was it helpful?

Solution

I think maybe you mean [0-9].* (number followed by zero or more characters). You can use capturing parentheses and use the backreference in the substitution.

s/[0-9]\(.*\)/\1/

The \1 will be whatever was captured. Change the replacement expression as needed.

s/getNumber(\([0-9]*\)).*/DontGetNumber(\1).roundDownTenth();

OTHER TIPS

You really want to use search/replace for this?

Personnally, I'd rather do something like :%g/Number/norm! ^~IDont^[fT2sDown^[

use a record with q[register] and fill the part after norm! with c-r [register]

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top