tags:

views:

189

answers:

3

I've got 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's my question: is there a vim way to have a structure like this?

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

ie, can the autocmd Filetype bit somehow be made to address a group of actions? (this is a simple example, I'm really asking for more complicated situations.)

+4  A: 

You can call a function, if you like:

autocmd Filetype ruby call SetRubyOptions()
function SetRubyOptions()
    setlocal ts=2
    ...
endfunction
Lucas Oman
hmmm, this is pretty good - thanks - but any way to tighten it up a little more (ie, remove the need to name and refer to the function)?
Peter
As nice as it would be, vim script doesn't have lambdas :P
Lucas Oman
@Peter - Nope. This is as tight as it can get.
Thomas Geritzma
+2  A: 

You can chain most commands 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 can't be chained like this, but you can use execute to get around that; see :h :bar.

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

Brian Carper
+5  A: 

ftplugins are the neat answer to your question.

Define a file named {rtp}/ftplugin/{thefiletype}.vim or {rtp}/ftplugin/{thefiletype}/whatever.vim. (:h rtp)

Then in a buffer local section of the ftplugin file (see examples in vim distribution if you plan to override default settings ; or among the many ftplugins I wrote otherwise), just write down your :setlocal, :*map <buffer>, etc. definitions.

It represents some more line to type, but at least, it does scale.

NB: don't forget to add a :filetype plugin on in your .vimrc.

Luc Hermitte
The only bad thing about ftplugins is that your vimrc ends up being split into dozens of files, and it's less convenient to copy around between computers.
Brian Carper
ftplugins can be distributed and shared more easily than a single monolithic .vimrc. When the configuration becomes rich and complex we can't maintain every thing into one file (see my C++ suite -- and I have similar and yet simpler configurations for other languages). BTW, I don't have a dozen of (non standard) ftplugins, but 78. And there is only 9 of them I'm not maintaining. I'm glad to not have their definition in my .vimrc.Moreover, copying one's configuration between various computers is as simple as a call to find + xarg + tar.
Luc Hermitte