tags:

views:

638

answers:

3

Hi,

In my vimrc I've got a generic tab setting of 2 spaces, and I'd like to override that on a per language basic (ie 4 for Python etc, otherwise use the default), but I'm having trouble finding any good example of this.

+4  A: 

Typically what you do is set up a special vimrc-type file with the settings for a particular language, and then use autocommands in your main .vimrc to execute the special vimrc when necessary. Here's my configuration for Haskell (.hs, etc.) files:

autocmd! BufNewFile,BufReadPre,FileReadPre  *.hs    so ~/.vim/haskell.vim
autocmd! BufNewFile,BufReadPre,FileReadPre  *.hsc   so ~/.vim/haskell.vim
autocmd! BufNewFile,BufReadPre,FileReadPre  *.lhs   so ~/.vim/haskell.vim
autocmd! BufNewFile,BufReadPre,FileReadPre  *.cabal so ~/.vim/haskell.vim

My ~/.vim/haskell.vim does stuff like "set expandtab" to use spaces instead of tabs, and all sorts of other magic for formatting and things like this. You can often download good versions of these for various languages from http://vim.org and other sites.

Note that you can do a lot more than just change vim settings. For example, you can run the file through a filter before and after editing:

" Edit gpg-encrypted ascii-armoured files
autocmd! BufReadPre,FileReadPre      *.asc  set bin
autocmd  BufReadPost,FileReadPost    *.asc  '[,']!gpg -q -d
autocmd  BufReadPost,FileReadPost    *.asc  set nobin
autocmd! BufWritePre,FileWritePre    *.asc  set bin
autocmd  BufWritePre,FileWritePre    *.asc  '[,']!gpg -e
autocmd  BufWritePost,FileWritePost  *.asc  undo
autocmd  BufWritePost,FileWritePost  *.asc  set nobin
Curt Sampson
Great, thanks. I'm going to go with this solution instead of the ftplugin based one, because this seems cleaner in the case where I have multiple extensions for files using the same language (like you have in your Haskell example).
Magnus Österlind
There is a little price to pay. The source commands (e.g. so ~/.vim/haskell.vim) will be executed every time you switch to such a buffer.
fgm
This is not the correct solution. The correct solution consists in using ftplugins (no need to write and maintain the autocommands ourselves), and (very important) in using local settings. -> s#:set tabstop=#:setlocal tabstop=#. See fgm's answer. If your extension is not recognized as haskell, then update your filetypes base. Don't you want vim to apply the correct syntax highlighting to your files?
Luc Hermitte
Personally, no, I don't; I rather dislike syntax highlighting. (And it looks pretty silly in a language like Haskell that has so little syntax.) But I understand that this doesn't apply to most users.
Curt Sampson
+4  A: 

Just put the settings into the filetype plugin file ~/.vim/ftplugin/LANGUAGE.vim . My ~/.vim/ftplugin/perl.vim contains the lines:

"
" ---------- tabulator / shiftwidth --------------------
"  Set tabulator and shift width to 4 (Perl Style Guide)
"
setlocal  tabstop=4
setlocal  shiftwidth=4
"

These settings will automatically be in effect for each file with file type 'perl' (new or existing).

fgm
A: 

See this tip on the VIM Wiki. Scroll down to the section titled "Different settings for different file types."

This answer uses the "after" directory so you won't have to muck with the supplied plugin files for different filetypes.

Brian Neal