title: How to reformat a source file to go from 2 space indentations to 3?
body: <p>This question is nearly identical to <a href="https://stackoverflow.com/questions/5217058/vim-reformat-a-python-file-to-have-4-space-indentations">this question</a> except that I have to go to three spaces (company coding guidelines) rather than four and the accepted solution will only double the matched pattern. Here was my first attempt:</p>
  
  <pre><code>:%s/^\(\s\s\)\+/\1 /gc 
  </code></pre>
  
  <p>But this does not work because four spaces get replaced by three. So I think that what I need is some way to get the count of how many times the pattern matched "+" and use that number to create the other side of the substitution but I feel this functionality is probably not available in Vim's regex (Let me know if you think it might be possible).</p>
  
  <p>I also tried doing the substitution manually by replacing the largest indents first and then the next smaller indent until I got it all converted but this was hard to keep track of the spaces:</p>
  
  <pre><code>:%s/^              \(\S\)/                     \1/gc
  </code></pre>
  
  <p>I could send it through Perl as it seems like Perl might have the ability to do it with its <a href="http://perldoc.perl.org/perlre.html#Extended-Patterns" rel="nofollow noreferrer">Extended Patterns</a>. But I could not get it to work with my version of Perl. Here was my attempt with trying to count a's:</p>
  
  <pre><code>:%!perl -pe 'm&lt;(?{ $cnt = 0 })(a(?{ local $cnt = $cnt + 1; }))*aaaa(?{ $res = $cnt })&gt;x; print $res'
  </code></pre>
  
  <p>My last resort will be to write a Perl script to do the conversion but I was hoping for a more general solution in Vim so that I could reuse the idea to solve other issues in the future.</p>