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.

Justin Lilly - 2008-11-08 00:22:52

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 :)

Foobar - 2008-11-09 10:51:18

Justin, :help retab does exactly that

garrat - 2009-11-30 15:51:47

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 :(