Several auto teams in vim

I have some sections in my .vimrc that look like this:

 autocmd Filetype ruby setlocal ts=2 autocmd Filetype ruby setlocal sts=2 autocmd Filetype ruby setlocal sw=2 

now it seems i can convert them to this:

 autocmd Filetype ruby setlocal ts=2 sts=2 sw=2 

but here my question is: is there any way vim has such a structure?

 <something mentioning Filetype ruby> setlocal ts=2 setlocal sts=2 ... <end> 

those. can the autocmd Filetype bit autocmd Filetype somehow addressed to an action group? (this is a simple example, I really ask for more complex situations.)

+42
vim
Sep 11 '09 at 21:07
source share
3 answers

You can call the function if you want:

 autocmd Filetype ruby call SetRubyOptions() function SetRubyOptions() setlocal ts=2 ... endfunction 
+54
Sep 11 '09 at 21:23
source share

You can combine most teams with | :

 au Filetype ruby \ setlocal ts=2 | \ setlocal sts=2 | \ ... 

Not sure if this syntax is better or worse than writing a function. Some commands may not be related this way, but you can use execute to get around this; see :h :bar .

Also see :h line-continuation for an explanation of the strange syntax with \ at the beginning of lines.

+37
Sep 11 '09 at 21:57
source share

ftplugins is a neat answer to your question.

  • Make sure your .vimrc has a line like :filetype plugin on
  • Define a file named {rtp}/ftplugin/{thefiletype}.vim or {rtp}/ftplugin/{thefiletype}/whatever.vim (for more details see :h rtp ).
  • Edit this new file and place your VIM commands there. It is probably recommended that you use the :setlocal command to ensure that the file-specific options are for that file only (for example, do not include all the purple tiles in all file types).

See the examples in the vim distribution if you plan to override the default settings; or to many ftplugins that I wrote , otherwise) just write your definitions :setlocal :*map <buffer> , etc.

It is another line for input, but at least it scales.

+20
Sep 12 '09 at 10:44
source share



All Articles