58

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?

2 Answers 2

55

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
  • 3
    thanks :set expandtab allowed me to use spaces instead of tabs for indentation
    – John
    Aug 2, 2015 at 23:45
  • Is it true that ts is useless when et is set?
    – iBug
    Aug 8, 2019 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, 2019 at 19:00
62

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

4
  • 2
    This should be the accepted answer Apr 17, 2020 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, 2021 at 21:09
  • is there a way to ensure <tab>s are only expanded when pressed from keyboard, and only when at the beginning indentation part of a line? (so I can safely indent with spaces, but any <Tab> typed elsewhere than the leftmost indentation part, or already present in the file, are left untouched? ex: script using <Tab> in some parts ...) Jan 25 at 15:27
  • @OlivierDulac That is probably something you should ask as a question in its own right (So many people are going to hate you :-) (I joke)). But I don't know of any such option personally, but smarttab may (not sure) be what you are looking for. I don't use it so don't know the exact details (which a question may provide). Jan 25 at 17:06

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.