converting tabs to spaces in vi

Posted by: barbara | Date: Nov 06, 2008 | Category: mac os x python    
This is mostly for my own personal reference - just one of those little things I use frequently and always forget how to set up:

If you're writing Python, then you know that indentation is, uhm, somewhat important. Mkay. I use four space indentation as a standard, but it's kind of a pain with vim, and my space bar is wearing out (one more reason to keep things nice and DRY though).

Anyway, to save myself a few keystrokes and a lot of aggravation, I just added a line to my vimrc:
sudo vi /usr/share/vim/vimrc
set softtabstop=4
Now hitting the tab key inside any file will indent four spaces. Simple but useful.
Comment by Justin Lilly on Nov 08, 2008:
I also have a function that does it too. map ,ft :%s/ / /g<CR> " replace all tabs with 4 spaces for files you didn't write to start :)
Comment by Foobar on Nov 09, 2008:
Justin, :help retab does exactly that
Comment by garrat on Nov 30, 2009:
that's handy and all but it's not converting tabs to spaces, it's simply setting the tab size to 4... to convert tabs to spaces you use :set expandtab and to turn spaces to tabs you :retab! only problem is, which is why i stumbled across this page... if you have expandtab set in .vimrc it doesn't count for auto-indents. yet every page I have read has no simply command for converting tabs to spaces. But I assure you this is a simple regex pattern to do this... annoying everyone is hailing the same buggy command :(
Comment by Datanumen on Mar 12, 2011:
It's been a few years since I've written Python, but for some reason, this little trick has me opening back up my software and writing again! I loved little tips and tricks like this. It's what made it fun for me. -Thomas