Thursday 26 April 2012

Compiling VIM on OS X Lion with Python and Ruby support

Why?

I like VIM and I’ve been trying in earnest to learn it since 2011. I’ve had spates where I’ve tried to use it as my main editor of choice, but I always return to TextMate because I’m not as fast in VIM as I am in TextMate. Not necessarily because I get to use a mouse, but because I’ve been using TextMate solid for since 2006.

The two main reasons that I want to use VIM as main editor is that it’s portable and powerful. I can use it when I’m ssh’d into a linux box, at my desk or ssh’d into my laptop. Being able to have a powerful editor and have all three editing environments be exactly the same is a huge plus. There is also the smaller stuff, like no waiting for version two releases. After seven versions VIM is arguably feature rich and ready for anything I can throw at it.

To get to the point where I can use VIM full time, I need to be able to –

  1. Navigate file structures quickly
  2. Navigate within a file quickly
  3. Be unaware of the controls and commands, being only aware of the work
  4. Be able to extend and customise VIM to my liking - fonts, colour schemes, plugins etc etc.

For the next 30 days I’ll be posting my escapades in this journey.

Now for the juicy stuff.

Getting, compiling and install VIM.

I’ve used MacVim and I know that OSX ships with a VIM, but I don’t like that MacVim runs in a separate window and the version of VIM that ships with OSX doesn’t have ruby or python support compiled in. For this reason I want to compile my own version so that I know how to do it and more importantly, I can recompile it if I need to alter the binary later on.

Getting the source

It’s recommended to install from the source and I’m going to get the source from the VIM mercurial repository so I can update later on. To do this you’ll need to have mercurial installed –

 hg clone https://vim.googlecode.com/hg/ vim 

Before we can start compiling we need to know what options are available to us. So cd into the vim src folder and ask configure for help. It’ll spit back a lot –

 ./configure --help 

That’s a lot, so if you get lost you can always use grep to hunt out the bits you want –

 ./configure --help | grep python 

So after a bit of poking around I’ve decided that I’m going to compile VIM with both Ruby and Python interpreters enabled, with all features enabled and I’m going to install it into my /usr/local/ directory. Here’s the configure flags –

 ./configure --prefix=/usr/local/ \             --enable-rubyinterp \             --enable-pythoninterp \             --with-features=huge 

Now that it’s compiled we can make it and install it, that’s super simple. Just make sure you’ve got write permissions into /usr/local

 make && make install 

You’re now installed, but if you have an OS that ships with a precompiled version you’ll want to make sure that when you type vim that your version is used. To do this make sure that /usr/local/bin is on the path before the system wide bin directories. To do this you need to edit your .bashrc or .bash_login file –

 vim $HOME/.bash_login 

Add something like the below. Here you can see that the first port of call is ~/bin then /usr/local/bin and /usr/local/sbin and finally whatever the system wide $PATH is set to.

 export PATH="~/bin:/usr/local/bin:/usr/local/sbin:$PATH" 

You can now test it and if all goes according to plan you should see something like this

Your freshly compiled installation of VIM

Before I wrap up for my first day of my VIM Quest there’s two more things I want to do. Firstly, I often type vi to get into VIM because before VIM there was VI. Technically I know that by typing vi I’m actually calling a different binary but I do it a lot because it’s one less keystroke. Secondly I want to set VIM as my editor of choice. I can do both of these things in my .bash_login/.bashrc files –

 export EDITOR="vim" alias vi="/usr/local/bin/vim" 

Now just reload your .bash_login/.bashrc file and you’re good to go

 source $HOME/.bash_login 

Summary

In day one of my VIM Quest I compiled a custom binary of ViM and enabled everything that I want. I then installed it into my /usr/local/ folder and set up my environment by editing my .bash_login/.bashrc file.

The scene is set for day two.