tags:

views:

101

answers:

2

I have some character maps for typing braces that I'd like to behave differently based on the file's extension - I imagine that would be pretty useful ability to have in general.

Any ideas on how to make this happen from my .vimrc or a plugin?

Thank you!

+10  A: 

There are basically two ways. Use the filetype plugin, or use filetype or extension autocommands.

The autocommands (placed in your .vimrc/_vimrc) take the form of either

autocmd Filetype cpp set textwidth=100

or

autocmd BufRead *.cpp,*.h,*.c set textwidth=100

(Obviously set textwidth=100 can be replaced with any other command)

The better solution, particularly if you have alot of custom commands for a filetype, is to use the files ~/.vim/after/ftplugin/<filetype>.vim for each filetype. Commands in those files will be executed after loading a file of the given type.

jkerian
I suggest you use setlocal so the settings are local only to the buffer. (e.g. setlocal textwidth=100) Use <buffer> for mappings to make them local to the buffer. (e.g. imap <buffer> } }<Esc>=%``a )
Peter Rincker
Luc Hermitte
Thank you! Using ftplugin worked beautifully.
Nicky Hajal
@Luc Thank you for questioning my mapping, I have forgotten why I had set the map. I use cindent and it works great, but cindent does not handle the case of wrapping a chunk of code with conditional or loop. The mapping I gave will re-indent the newly wrapped code block upon the inserting of the closing brace. Without the mapping the just wrapped code block will stay at the same indention level. If you can accomplish this effect with just cindent I would greatly appreciate it if you would share your indention settings. (:set ai? cin? cink? cino? si? inde? indk?)
Peter Rincker
@Peter. OK I see. Actually I have surrounding (/-der?) mappings (for all brackets-like characters, control statements) that automatically reindent the surrounded code. Everything is actually available here: http://code.google.com/p/lh-vim/wiki/lhCpp#Brackets (provided you are working in C-like languages)
Luc Hermitte
There's also the [surround plugin](http://www.vim.org/scripts/script.php?script_id=1697), which is pretty good imo.
dash-tom-bang
+1  A: 

In complement to jkerian's answer, here are a few links to other questions related to ftplugins (as script-local variables, mappings, command, settings, abbreviations, etc shall be used when writing ft specific configuration):

Luc Hermitte