Whenever possible, I code in TextMate. TextMate is good at a lot of things; one of them is remembering that in Ruby I want a tab stop of two, not four like in most other languages.
But frequently, it’s easier for me to code in Vim on my servers, over an
SSH connection. Here’s where I run into trouble. My .vimrc file has the
general coding defaults I prefer:
1 2 3 4 5 | |
I have mappings to easily swap syntaxes in-buffer, such as these.
1 2 3 4 | |
I have autocommands based on filenames (for cases where the syntax isn’t autodetected).
1 2 3 4 | |
Yet I still have the issue where I edit a Ruby file, the syntax is
autodetected, and I end up with shiftwidth=4 because that’s the default.
I can tap ,sr but that’s inconvenient to do every time. I can’t use an
autocommand because these files don’t end in a predictable extension.
It turns out that Vim has a way to let you define settings per syntax at buffer
load time, using Vim scripts inside the ~/.vim directory. I added this to the
global .bash_profile that I use everywhere. This Vim script is automatically
created whenever I log in.
1 2 3 | |
Now, these settings are applied whenever Vim detects I’ve entered a file with Ruby syntax, regardless of filename. Works like a charm.
You can use this trick to do anything you want using any supported Vim syntax; just name a file after the syntax in question (perl, php, m4, etc).