I use several different programming languages every day, and I’d like to have different tab widths (in spaces) for each. For example: I use the “standard” 2 spaces for Ruby, but all our existing Matlab code uses 4 spaces.
I have this from my personal ~/.vimrc:
augroup lang_perl
au!
set tabstop=4 " tabstop length N in spaces
set shiftwidth=4 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
set expandtab " Use spaces instead of tabs
augroup END
augroup lang_ruby
au!
set tabstop=2 " tabstop length N in spaces
set shiftwidth=2 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
set expandtab " Use spaces instead of tabs
augroup END
Those work, but the following doesn’t:
augroup lang_matlab
au!
set tabstop=4 " tabstop length N in spaces
set shiftwidth=4 " make >> and friends (<<, ^T, ^D) shift N, not the default 8
set expandtab " Use spaces instead of tabs
augroup END
I really don’t understand how augroup lang_ruby figures out that I’m editing a Ruby file. (My searches brought up ftdetect, but the solution wasn’t obvious.) It doesn’t seem like vim knows that I’m editing Matlab using augroup lang_matlab. What do I change to make this work?
If you want to have a large number of settings for {filetype}, you should put them into
~/.vim/ftplugin/{filetype}.vimor into file that matches~/.vim/ftplugin/{filetype}/**/*.vim(examples:~/.vim/ftplugin/ruby/foo.vim,~/.vim/ftplugin/ruby/foo/bar.vim). In this case you don’t need any autocommands at all. If you still want to use autocommands, use the following:. Note two things: FileType event (it is there and it is not BufRead,BufNewFile) and
setlocalinstead of plainset. First is intended to be used for filetype settings, second is how buffer-specific options must be set.About why perl and ruby settings work and why matlab settings does not: your example code is just the same as
So, you effectively set
ts=2 sw=2 et. But the$VIMRUNTIME/ftplugin/perl.vimcontains the following code:so,
ts=4 sw=4for perl is set intoftplugin/perl.vim, not in your vimrc (if you have installed perl-support plugin). You can check it by replacingtabstop=4withtabstop=8in vimrc.