10

Possible Duplicate:
Changing Vim indentation behavior by file type

Hello. So, I switch between '2' and '4' spaces for tabs very often. Usually I use 2 spaces for a tab for HTML files and 4 spaces for a tab for programming. Is there anyway to configure VIM, so it will automatically adjust depending on the file extension? Also, how come VIM indents 8 spaces sometimes, like after I enter an open brace? I have it set to 4 spaces. Thanks.

Community
  • 1
  • 1
Kelp
  • 1,268
  • 5
  • 18
  • 32

2 Answers2

21
set sw=4 ts=4 sts=4                             " Defaults: four spaces per tab "
autocmd FileType html :setlocal sw=2 ts=2 sts=2 " Two spaces for HTML files "

Here are three different options: 'shiftwidth' ('sw') controls number of spaces for automatic indentation and some shifting commands (like << in normal mode), 'tabstop' ('ts') controls visual length of a real tab character, you may want to leave defaults (8 visual cells), 'softtabstop' ('sts') controls what is being inserted/removed when you press <Tab> and <CR>. I suggest you either set it to the value of 'tabstop' or set it alongside with 'expandtab' because in other cases it will produce ugly tabs+spaces indentation.

ZyX
  • 52,536
  • 7
  • 114
  • 135
  • Is there anyway that I add multiple file extensions that will use 2 spaces for a tab in the same line? Or, will I have to create new lines for other file extensions? I also want to include .jade files as having 2 space tabs, but I do not want to clutter my .vimrc file with multiple lines of file extensions that all have 2 spaces as tabs. – Kelp May 16 '11 at 03:38
  • @Kelp You can delimit alternative patterns with comma, so it will look like `autocmd FileType html,xml,whatever :...`. Colon here is optional, I just thing that it makes code a bit more readable. You can also use `:h line-continuation` to move `:setlocal` on the next line after pattern. – ZyX May 16 '11 at 15:32
1

Type :help syntax in vim. This will open a help file giving an overview with subsequent pages/files showing you how to bind file extensions to syntax files where you can :set shiftwidth=2and :set tabstop=2for e. g. HTML files. I guess the syntax files of your installation are responsible for your brace indentation symptom as well.

ofi
  • 364
  • 1
  • 4