Quickly Convert 4 Spaces (or Tabs) to 2 spaces in Vim
<p>Here’s a handy Vim command that I find myself searching for often. It could easily be made into a function in your vimrc.</p> <div class="highlight"><pre class="highlight viml"><code><span class="c">" 4 spaces to 2 spaces</span> %s;^\<span class="p">(</span>\s\<span class="p">+</span>\<span class="p">)</span>;\<span class="p">=</span><span class="nb">repeat</span><span class="p">(</span><span class="s1">' '</span><span class="p">,</span> <span class="nb">len</span><span class="p">(</span><span class="nb">submatch</span><span class="p">(</span><span class="m">0</span><span class="p">))</span>/<span class="m">2</span><span class="p">)</span>;<span class="k">g</span> <span class="c">" Tab to 2 spaces</span> %s<span class="sr">/\t/</span> /<span class="k">g</span> </code></pre></div>