sponsor Vim development Vim logo Vim Book Ad

basic Tip #12: Converting tabs to spaces

 tip karma   Rating 600/239, Viewed by 11200 

created:   February 24, 2001 18:02      complexity:   basic
author:   Yegappan      as of Vim:   5.7


To insert space characters whenever the tab key is pressed, set the
'expandtab' option:

        set expandtab

With this option set, if you want to enter a real tab character use
Ctrl-V<Tab> key sequence.

To control the number of space characters that will be inserted when
the tab key is pressed, set the 'tabstop' option.  For example, to
insert 4 spaces for a tab, use:

        set tabstop=4

After the 'expandtab' option is set, all the new tab characters entered
will be changed to spaces.  This will not affect the existing tab
characters.  To change all the existing tab characters to match the
current tab settings, use

        :retab

To change the number of space characters inserted for indentation, use
the 'shiftwidth' option:

        set shiftwidth=4

For example, to get the following coding style,
        - No tabs in the source file
        - All tab characters are 4 space characters

use the following set of options:

        set tabstop=4
        set shiftwidth=4
        set expandtab

Add the above settings to your .vimrc file.

To get more help on these options, use
:help tabstop
:help shiftwidth
:help expandtab

 rate this tip  Life Changing Helpful Unfulfilling 

<<Completing words quicky in insert mode | Incremental search >>

Additional Notes

[email protected], February 27, 2001 7:05
Also, see the :help smarttab option
[email protected], February 27, 2001 8:42
but tabs are better :-)
Dirk Ruediger <[email protected]>, March 6, 2001 3:57
I'm also using

set list listchars=tab:»·,trail:·

to "highlight" any tabs and all trailing spaces -- to catch them :).
[email protected], August 14, 2001 20:47
To turn off expandtab for editing makefiles, I put the following in my .vimrc:

au FileType make setlocal noexpandtab
[email protected], December 13, 2002 1:45
This feature is a "life preserver", when you edit Python code.
[email protected], May 15, 2003 15:40
To use this mode only for python add the following to ~/.vimrc

autocmd FileType * set tabstop=2|set shiftwidth=2|set noexpandtab
autocmd FileType python set tabstop=4|set shiftwidth=4|set expandtab

[email protected], June 30, 2003 19:48
:retab

This is what I've been looking for!
I use spaces... for printing only ;-)
[email protected], July 10, 2003 15:07
I also add the following line:

set softtabstop=4   " makes the spaces feel like real tabs

This makes the backspace key treat the four spaces like a tab (so one backspace goes back a full 4 spaces).  

I love vim.
[email protected], December 3, 2003 10:25
Read this if you're interested in this tip. It explains how tabs are actually much better than spaces, contrary to many's opinion.

http://derkarl.org/why_to_tabs.html
misof, December 16, 2003 14:54
:!man expand
:%!expand
why shouldn't you use an external tool made especially for this purpose?
[email protected], December 28, 2003 8:41
I was just about to give up on Vim,
        because of backspacing in insert mode.
Wow, thanks.
Also, check "Vi Improved- Vim" (Steve Oualline); I've just used the index.
   page 94 - a nice clear explanation on "backspace"
            He says: use ":set backspace=indent,eol,start",
                               instead of ":set backspace=2.
   pages 265-268 - a story on tab/retab/space/etc.;
            I got somewhat confused here over softtabstop/tabstop/shiftwidth.
      
[email protected], January 21, 2004 21:40
Is there any way to make vim treat spaces as tabs when not in insert mode?  My co-workers prefer code to be saved with spaces (which makes no sense to me) so I would like to use these options.  The problem is that when I position my cursor on a line, and hit insert, it begins at that space, which may not line up with the tab stop.  I want cursor movement on whitespace to act like there are tabs there.

Is that possible?
[email protected], July 23, 2004 3:38
yeah, this is slow as hell (try it on a 700+ mb file). there are severall unix tools that do it MUCH faster (tr, the mentioned expand,...)
[email protected], September 29, 2004 9:37
[email protected], the author of that page utterly fails to make any sort of convincing argument.
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to [email protected] after searching the archive. Help Bram help Uganda.
Sponsored by Web Concept Group Inc. SourceForge Logo