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}.vim
or 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:
augroup lang_matlab
autocmd!
autocmd FileType matlab setlocal ts=4 sw=4 et
augroup END
. Note two things: FileType event (it is there and it is not BufRead,BufNewFile) and setlocal
instead of plain set
. 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
augroup lang_perl
autocmd!
augroup END
augroup lang_ruby
autocmd!
augroup END
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
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
So, you effectively set ts=2 sw=2 et
. But the $VIMRUNTIME/ftplugin/perl.vim
contains the following code:
setlocal tabstop=4
setlocal shiftwidth=4
so, ts=4 sw=4
for perl is set into ftplugin/perl.vim
, not in your vimrc (if you have installed perl-support plugin). You can check it by replacing tabstop=4
with tabstop=8
in vimrc.
I cannot tell you how it's determined that lang_perl
or lang_ruby
shall be read. But looking at the autocommand documentation brings up an example for gzipped files (:he gzip-example
):
augroup gzip
autocmd!
autocmd BufReadPre,FileReadPre *.gz set bin
autocmd BufReadPost,FileReadPost *.gz '[,']!gunzip
autocmd BufReadPost,FileReadPost *.gz set nobin
autocmd BufReadPost,FileReadPost *.gz execute ":doautocmd BufReadPost " . expand("%:r")
autocmd BufWritePost,FileWritePost *.gz !mv <afile> <afile>:r
autocmd BufWritePost,FileWritePost *.gz !gzip <afile>:r
autocmd FileAppendPre *.gz !gunzip <afile>
autocmd FileAppendPre *.gz !mv <afile>:r <afile>
autocmd FileAppendPost *.gz !mv <afile> <afile>:r
autocmd FileAppendPost *.gz !gzip <afile>:r
augroup END
See it? Every autocmd
that gets specified carries an Event (BufReadPre
, FileReadPre
) and the extension for which it shall be executed (*.gz
). This is backing up my hitherto opinion that you can use any name for your augroup
but have to specify an extension that the augroup
shall be responsible for in order to get it working correctly.
Another quote of :he autocmd-groups
:
Normally, when executing autocommands automatically, Vim uses the autocommands for all groups. The group only matters when executing autocommands with ":doautocmd" or ":doautoall", or when defining or deleting autocommands.
So, in order to make an augroup
for matlab files, this one should do the trick:
augroup lang_matlab
autocmd!
autocmd BufRead *.m set ts=4 sw=4 expandtab
augroup END
And make sure that these settings don't get overwritten by any augroup
s that do something unconditional (i.e. modify the other augroup
definitions too).
Use ftplugins, configuring vim will be much more easier to achieve. See gVim and multiple programming languages
精彩评论