5

How to configure on the .vimrc file to use a particular set options for a particular filetype, but others for a different filetype?

For example, I want to enable expand tab (set tabs to be spaces) for Python files, but disable it for C++ files.

5

I put this sort of thing in $HOME/.vim/ftdetect/{FILETYPE}.vim. To accomplish what you want I would have the following files.

For python...

"$HOME/.vim/ftdetect/python.vim
autocmd BufRead,BufNewFile *.py set noexpandtab

And for C++...

"$HOME/.vim/ftdetect/cpp.vim
autocmd BufRead,BufNewFile *.cpp set expandtab

What these commands do is set the expandtab or noexpandtab options for a specific buffer which gets created for, or reads in, a file that matches one of the patterns *.py or *.cpp.

| improve this answer | |
  • 7
    ftdetect is meant for detecting filetypes, use an ftplugin instead, there you don't need an autocmd, just use setlocal instead though. – Dhruva Sagar Feb 9 '15 at 15:00

Not the answer you're looking for? Browse other questions tagged or ask your own question.