Ramashish Baranwal wrote:
Hi,
I would like to know how to define file specific settings (based on
file extension/type). I would prefer modifying my .vimrc file instead
of language specific .vim files. I am trying to achieve something
like-
if file-extension is .c or .cpp
# expand tabs to spaces
set sw=4 ts=4 expandtab
else if filetype is Makefile
# don't expand tabs to spaces
noexpandtab
endif
Any help would be appreciated. :)
Thanks,
Ram
Well, the canonical way to do this is by means of after-plugins:
~/.vim/after/ftplugin/c.vim (for c and cpp) or ~/.vim/after/ftplugin/make.vim
for makefiles -- replace ".vim" by "vimfiles" on Windows). You can also do it
by means of autocommands for the FileType event. Place them after the
":filetype on" or ":source vimrc_example.vim" statement.
Note: ftplugin/c.vim is sourced for cpp files but in the case of autocommands
you might need them both:
autocomd FileType c,cpp setlocal sw=4 ts=4 expandtab
autocomd Filetype make noexpandtab
Notes:
1) Regardless of whether you use an after-plugin or an autocommand, use
":setlocal" rather than ":set", to avoid clobbering the settings for other
open files (hidden, or in split-windows).
2) The default ftplugin for the "make" filetype already sets 'noexpandtab' so
you don't need to set it again.
Best regards,
Tony.