I am using Mac OSX 10.7.5, the contents of .vimrc is as follow:

set tabstop=4
set shiftwidth=4
set softtabstop=4
set expandtab
set shiftround  
set smarttab    
set autoindent  
set copyindent  

autocmd FileType make setlocal noexpandtab

What I am trying to do is when I edit normal files like .js, .html I want my tabs to be indented with 4 blank spaces instead of a normal tab.

But when I am editing Makefile, I need it to be a normal tab instead of 4 blank spaces for indentations.

I thought the above setup in .vimrc is going to give me that, but is not working for me as when I am editing Makefile I am still getting 4 blank spaces for indentation.

Not sure what I am doing wrong here?


This is a section of my .vimrc:

" enable filetype detection:
filetype on
filetype plugin on
filetype indent on " file type based indentation

" recognize anything in my .Postponed directory as a news article, and anything
" at all with a .txt extension as being human-language text [this clobbers the
" `help' filetype, but that doesn't seem to prevent help from working
" properly]:
augroup filetype
  autocmd BufNewFile,BufRead */.Postponed/* set filetype=mail
  autocmd BufNewFile,BufRead *.txt set filetype=human
augroup END

autocmd FileType mail set formatoptions+=t textwidth=72 " enable wrapping in mail
autocmd FileType human set formatoptions-=t textwidth=0 " disable wrapping in txt

" for C-like  programming where comments have explicit end
" characters, if starting a new line in the middle of a comment automatically
" insert the comment leader characters:
autocmd FileType c,cpp,java set formatoptions+=ro
autocmd FileType c set omnifunc=ccomplete#Complete

" fixed indentation should be OK for XML and CSS. People have fast internet
" anyway. Indentation set to 2.
autocmd FileType html,xhtml,css,xml,xslt set shiftwidth=2 softtabstop=2

" two space indentation for some files
autocmd FileType vim,lua,nginx set shiftwidth=2 softtabstop=2

" for CSS, also have things in braces indented:
autocmd FileType css set omnifunc=csscomplete#CompleteCSS

" add completion for xHTML
autocmd FileType xhtml,html set omnifunc=htmlcomplete#CompleteTags

" add completion for XML
autocmd FileType xml set omnifunc=xmlcomplete#CompleteTags

" in makefiles, don't expand tabs to spaces, since actual tab characters are
" needed, and have indentation at 8 chars to be sure that all indents are tabs
" (despite the mappings later):
autocmd FileType make set noexpandtab shiftwidth=8 softtabstop=0

" ensure normal tabs in assembly files
" and set to NASM syntax highlighting
autocmd FileType asm set noexpandtab shiftwidth=8 softtabstop=0 syntax=nasm

The section should be self-explanatory, but I suggest you read the vim help on filetype and autocmd.

The most relevant line to you, is probably this one:

autocmd FileType make set noexpandtab shiftwidth=8 softtabstop=0

make sure filetype detection is switched on, though.

  • Thanks for the excellent autocommands! I noticed in this tutorial while learning from your .vimrc that if you don't wrap your autocmds in augroup sections, Vim will read these in and duplicate them. Is this correct? – Joshua Detwiler Jun 23 at 2:20

Instead of doing this with autocmds, you could create your own user filetype plugin for each filetype, and place it in ~/.vim/ftplugin/<filetype>.vim, where <filetype> is the actual file type you want. For example:

mkdir -p ~/.vim/ftplugin
echo "setlocal noexpandtab" > ~/.vim/ftplugin/make.vim

You do need to make sure you have filetype plugins enabled in your ~/.vimrc with the following command:

filetype plugin on
  • This answer makes more sense if you like to keep your .vimrc and .vim directories tidy. – Floby Jun 18 at 8:57

It is simpler to configure vim to always expand tabs, which is what is wanted for all files except makefiles. In makefiles, you can use to insert a tab wherever you want. It won't be expanded.

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.