Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell vim to add commas to a number, e.g. change 31415926 to 31,415,926

Tags:

vim

I have a very large number (a couple hundred digits long), and I'd like to use vim to add commas to the number in the appropriate manner, i.e. after each group of three digits, moving from right to left. How can I do this efficiently?

like image 354
Tanner Swett Avatar asked Jun 22 '13 22:06

Tanner Swett


1 Answers

Taken from here

Substitue command that adds commas in the right spot.

:%s/\(\d\)\(\(\d\d\d\)\+\d\@!\)\@=/\1,/g

This uses a zero width lookahead to match any number that isn't followed by groups of three numbers followed by one number. (or 3n+1 numbers)

So the numbers that match in are marked with ^. These are then replaced with a comma after it the match.

31415926
 ^  ^

Which replaces to

31,415,926
like image 173
FDinoff Avatar answered Sep 23 '22 06:09

FDinoff