Quickly Convert 4 Spaces (or Tabs) to 2 spaces in Vim

Here’s a handy Vim command that I find myself searching for often. It could easily be made into a function in your vimrc.

" 4 spaces to 2 spaces
%s;^\(\s\+\);\=repeat(' ', len(submatch(0))/2);g

" Tab to 2 spaces
%s/\t/  /g