I need to put a comma between any two digits in a string of digits. For example, I want this


to become this


The approach I took was the following


Strangely enough, the substitution command above when applied to the string in the example gives


If I apply the substitution command once again, it gives what I want. But why just one instance of the command don't work?

The vim version I'm using is

VIM - Vi IMproved 8.0 (2016 Sep 12, compiled Mar 18 2020 18:29:15)

1 Answer 1


why just one instance of the command don't work?

Obviously, the string is parsed as (12)(34)(56)(78)(91)0 --> 1,2 3,4 5,6 7,8 9,1 0

A possible solution is s/\d\d\@=/\0,/g. See :h /\@=.

  • This \@= thing is very nifty. Didn't know of it, though. Thanks!
    – Larara
    Sep 1, 2020 at 5:50
  • Or s/\d\ze./&,/g. A little shorter, maybe a little more efficient (not that one would notice with such small inputs). On the con side, you want to make sure there are only numbers (otherwise s/\d\ze\d/&,/g).
    – B Layer
    Sep 24, 2020 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.