I usually work on js files and php files. My js files require 2 spaces tab, whereas my php requires 4. How can I make vim dynamically change that depending on the file type I'm working on?
Asked
Active
Viewed 2,181 times
1 Answers
4
Putting these in your .vimrc should do the trick:
augroup FileTypeSpecificAutocommands
autocmd FileType javascript setlocal tabstop=2 softtabstop=2 shiftwidth=2
autocmd FileType php setlocal tabstop=2 softtabstop=2 shiftwidth=2
augroup END
You should also use expandtab to convert tabs into spaces, I think. Also, you might wanna check out this blogpost.
Also, if you don't mind splitting up your vimrc in modular files, you can put the following commands in ~/.vim/after/ftplugin/javascript.vim and ~/.vim/after/ftplugin/php.vim
setlocal tabstop = softtabstop = shiftwidth = 2
3N4N
- 5,684
- 18
- 45
filetypeis tested for each autocommand which is not efficient and this is also replicating something vim already does automatically. Also, if you stick with using autocommands it is usually a good idea to put them in anaugroup. – statox Mar 04 '18 at 17:55