Saturday, September 20, 2008

Convert tabs to spaces in Vim

Add the following to ~/.vimrc, /etc/vimrc or /etc/vim/vimrc:

set tabstop=4       " The width of a TAB is set to 4.
                    " Still it is a \t. It is just that
                    " vim will interpret it to be having
                    " a width of 4.

set shiftwidth=4    " Intents will have a width of 4

set softtabstop=4   " Sets the number of columns for a TAB

set expandtab       " Expand TABs to spaces

To convert tabs to spaces in an already existing file, open it in vim and enter these:

:set expandtab :retab

5 comments:

Anonymous said...

Many thanks, that was very useful.

neoisoffline said...

thanks a ton man

Larry C said...

That is a really good solution, it is non-intrusive, non-destructive, and ensures that files edited going forward will have an un-tabbed state.

The :retab command rocks.

Thanks!!!!!

Anonymous said...

was very helpful... thanks

Jamie Swain said...

Thanks for posting this!

Just what i was looking for :)