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?


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'.

  • 3
    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 '19 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. Aug 13 '19 at 19:00

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 this option is enabled, vi will use spaces 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 ':'

  • 2
    This should be the accepted answer Apr 17 '20 at 23:03
  • I like to keep my tabstop 8 wide... makes spotting the scoundrels way easier. set autoindent is also nice in the real world... for code from the people who disagree with you... they are obviously wrong. Or for monsters that set their soft tabs to 3 or 5.
    – Ray Foss
    Jun 26 at 21:09

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.