tags:

views:

6793

answers:

5

I would like to convert tab to spaces in gvim. I added the following codes to my _vimrc:

set tabstop=2

It works to stop at 2 spaces but it still looks like one tab key is inserted (I tried to use h key to count spaces afterwards). Not sure what should I do to make gvim to convert tab to spaces?

+3  A: 

Try

set expandtab

for soft tabs.

To fix pre-existing tabs:

:%s/\t/  /g

I used two spaces since you already set your tabstop to 2 spaces.

Hank Gay
That fix up will insert two spaces where only one is required.
Jonathan Leffler
instead of doing the substitution, you can do what Nick suggested above - ie retab. That will retab all your existing tabs as the number of spaces set in your tabstop.
Gowri
+25  A: 

IIRC, something like:

set tabstop=2 shiftwidth=2 expandtab

should do the trick. If you already have tabs, then follow it up with a nice global RE to replace them with double spaces.

D.Shawley
If you're on Linux/Unix, you can expand the tabs by typing %!expand -2
Paul Tomblin
Oops, that's ":%!expand -t2"
Paul Tomblin
or you can just use :retab
rampion
+28  A: 

Once you've got expandtab on as per the other answers, the extremely convenient way to convert existing files according to your new settings is:

:%retab
ʞɔıu
perfect! This saved my bacon earlier this morning.
Michael Gorsuch
+2  A: 

If you want to keep your \t equal to 8 spaces then consider setting:

   set softtabstop=2 tabstop=8 shiftwidth=2

This will give you two spaces per <TAB> press, but actual \t in your code will still be viewed as 8 characters.

pk
A: 

Add following lines to your .vimrc

set expandtab
set tabstop=4
set shiftwidth=4
map <F2> :retab <CR> :wq! <CR>

Open a file in vim and press F2 The tabs will be converted to 4 spaces and file will be saved automatically.

anish