Sunday, February 8, 2009

Vim|Convert tabs to spaces in Vim

Alan Haggai Alavi's Web Log: Convert tabs to spaces in Vim: "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"


##################
# Tabs and Spaces
##################
An endless argument in editors is whether to insert tabs or spaces while
writing code. In order to satisy people on both sides, vim can be set to do
exactly what people want. The 'expandtab' option when unset inserts tabs when
you press the key and when set inserts spaces.

':set expandtab' - for spaces
':set noexpandtab' - for tabs

For people trying to meet the people on the other side, we can do the
following steps to..

1) Convert tabs to spaces
-------------------------
':set expandtab'
':%retab!'

2) Convert spaces to tabs
-------------------------
'set noexpandtab'
':%retab!'

In order to visualize the above actions, it is useful to do ':set list'
which turns on invisible characters (spaces, tabs, end of line etc).

No comments: