Replace tab with space in Vim

I’m a fan of spaces. I hate tabs, actually. It does not look good on vim, git, … you name it. Unfortunately lots of Linux config files are still using tab, like apache2 and so on. I’m like, why don’t they switch to space completely.

The reason is that, under standard screen (80×24), each tab costs 8. After several indentation, you hardly can see anything or almost every line of config will be break into two lines. Doesn’t look good.

Here is a trick to replace tab with space, and ensure vim will use space by default. If you want to use space as default in your system, put it at the end of /etc/vim/vimrc, otherwise at the end of ~/.vimrc

set expandtab
set tabstop=4
set shiftwidth=4

Of course you can replace 4 by any number of your choice. For me 4 is perfect. 2 is too small and 8 is definitely too big.

But what if you want to replace tab with space in existing files? Open vim and type this command:

:retab

Voila! Happy vim-ing.

Source: http://stackoverflow.com/questions/426963/replace-tab-with-spaces-in-vim