Tuesday, August 7, 2012

Synching Your Vim Configuration Files to Dropbox on Change OS X

If you're a user of Vim of any kind of experience level, you've probably come across vimrc and gvimrc. Your experience with vim is heavily ties to these 2 files, hence the need to have them backed up in a safe place in case anything catastrophic manages to occur on your system. Specifically I've implemented a tiny but useful script to push my most precious vim assets to my Dropbox account in the ever so beloved and mysterious....."cloud".

I haven't tested this on Windows as I don't make many changes to my vim configuration on my win32 computer. I find it more effective to manually synch those changes not to mention I don't use vim nearly as much on win32 as I do on darwin (OS X).

That was kind of a joke btw. If you look closely enough you'll see why this won't work on win32 out of the box without a bit of magic or aliasing. Maybe powershell exposes an alias for the command I'm using in the same way that it maps "ls" to "dir", but anyway.

So here goes...

" Synch vim files to dropbox on change
:augroup VimFiles

autocmd! VimFiles BufWritePost ~/.vimrc :!cp ~/.vimrc $HOME/Dropbox/vimrc
autocmd! VimFiles BufWritePost ~/.vimrc.local :!cp ~/.vimrc $HOME/Dropbox/vimrclocal
I've place the snippet above in my .vimrc.local config. What's vimrc.local? Well, there are some "vim bootstraps" out on the market and Steve Francia's is a pretty good option. His vim bootraps process will source the vimrc.local dotfile from vimrc. This allows you to override or customize the bootstappers default configuration without directly changing its contents (vimrc). This is important because the bootstrapper takes the form of a git repo which allows you to pull in updates. You can imagine your changes conflicting with the author's and all other kinds of hairy predicaments version control systems lend themselves susceptible to.

You'll also notice the usage of the augroup vim command. This is just a neat way to organize your vent subscriptions. Vim can keep track of these and manage them as a whole. For example, you could delete all event subscriptions under a particular group if you wanted to. Just handy namespacing. These groups are optional so you should use them at your convenience.

If the :! syntax is news to you then it's just a way to spin up your local shell (cmd, bash, zsh, etc.) and execute an arbitrary command. The same as backticks/sh/system in Ruby, Exec task in msbuild, etc.

As far as gvimrc goes, I don't have much use for one since I use MacVim. I'm perfectly content with my vim GUI and to be honest my system doesn't even contain a gvimrc dotfile. The two files I care most about are covered.

Now I know this script is way simple, it's only 2 lines for crying out loud, but exactly what is it doing?
The vim runtime publishes events that developers can conveniently subscribe to. These events range anywhere from when vim first loads to when a file changes on your file system outside of the context of vim. There are a multitude of these events wich a varying degree of scope and granularity. To see a full list, just invoke the vim help with "au" as an argument.
:h au
au is an ancillary/alias for autocmd so of course either will suffice.

The event I'm subscribing to is the BufWritePost event. And quote...

"after writing the whole buffer to a file"

Most all the events take a regex pattern as input so you can handles multiple files, check based on extension, etc. For example, the vim runtime doesn't know about well known file names idiomatic to Ruby such as

Rakefile - searched for and loaded by rake by default
config.ru - rack configuration for rack based apps like Rails and sinatra
Capfile - capistrano configuration for deployments

etc...

So I took time out to tell vim that it should treat these files as Ruby code so I could get syntax highlighting. Otherwise they just read as plain text and that's quite the inconvenience for today's developer. We Mac users love our aesthetically pleasing things so the more color the better *wink*.
:augroup UnrecognizedRubyFiles

" Set file type to Ruby for unrecognized ruby files
autocmd! UnrecognizedRubyFiles BufReadPost,FileReadPost Gemfile,Rakefile,config.ru,Thorfile,Capfile,Vagrantfile set ft=ruby

The BufReadPost event is published once vim finishes reading your file. Not a bad time to tell it what we're working with. You could probably even get away with subscribing to an event published earlier in the runtime but this works for me.

Well, that's pretty much it folks. We took a look at subscribing to vim event via the au/autocmd command. Again, this command is nice because it can accept patterns as arguments. We can also namespace our event subscriptions with the augroup command. You'll see this technique employed by many plugin developers. Take rails.vim for example.

























Thanks guys...enjoy.

No comments:

Post a Comment