Last Updated: February 25, 2016
·
1.491K
· benjaminrh

Convert tabs/spaces in Vim

Many people have settings in their .vimrc file for setting up indentation the way they like. This can vary depending on programming language, but most people who've played with their .vimrc have encountered tabstop which sets the width of a <kbd>TAB</kbd>, softtabstop, shiftwidth for indentation, expandtab to handle what happens when you press <kbd>TAB</kbd>, and smarttab.

I've got all these things set too. Until recently, I've used a simple regex (something like :%s/\t/ /g) to change the file's indentation. I did it enough that I mapped it to something easily accessible.

But it turns out that Vim provides a very simple command to change a file's indentation to match your settings. Ladies and gentlemen, allow me to present :retab. Yep, that's it. You learn something new every day if you use Vim.