21

I am coding some html templates in JADE files. I want to auto indent code with spaces instead of tab. I tried shiftwidth=2, tabstop=2, but no matter what, it's still indents using tabs instead of spacebar, which causes the JADE compile to throw errors.

How can I auto indent large sections of code using spaces instead of tabs?

25

You are looking for the expandtab option. When this option is set, spaces are always used. You can put set expandtab in your vimrc to always have this option set when vim starts. If you want to only enable this option for specific languages, see this question. For help on this option, see :help 'expandtab'.

  • 1
    thanks :set expandtab allowed me to use spaces instead of tabs for indentation – John Aug 2 '15 at 23:45
  • Is it true that ts is useless when et is set? – iBug Aug 8 at 12:59
  • @iBug Not if you are creating the file yourself, but it might be useful to have it set if you are editing existing files that have tabs in them. See vim.fandom.com/wiki/Indenting_source_code for more on indentation, I found it a lot clearer than vim's help files. – EvergreenTree Aug 13 at 19:00
20

You want expandtab.

But there are usually a couple of options you want to set at the same time.

If you add the following to your ~/.vimrc file

" tabstop:          Width of tab character
" softtabstop:      Fine tunes the amount of white space to be added
" shiftwidth        Determines the amount of whitespace to add in normal mode
" expandtab:        When on uses space instead of tabs
set tabstop     =4
set softtabstop =4
set shiftwidth  =4
set expandtab

Alternatively you can execute these manually from normal mode by proceeding them with ':'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.