Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add quotations around numbers in Vim - Regex?

Tags:

regex

vim

I have been trying, unsuccessfully, to add quotation marks around some numbers in my file using regex. To clarify, let me give an example of what I am trying to do.

Something like myFunction(100) would be changed to myFunction("100").

I thought :100,300s/\([0-9]*\)/"\0" would work but it put quotation marks around spaces as well.

What can I do to fix this?

like image 368
sumowrestler Avatar asked Dec 07 '25 16:12

sumowrestler


1 Answers

You should slightly modify the regular expression:

%s/\(\d\+\)/"\1"

In regular expression, first matched group is \1, not \0. And it looks safer to use \+ instead of *.

like image 174
ntalbs Avatar answered Dec 12 '25 00:12

ntalbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!