pithy, poignant, insightful - or not

RSpec Syntax Highlighting In Vim

Posted: December 29th, 2008 | Author: Adam | Filed under: Uncategorized | Tags: , , | 2 Comments »

Recently I began the migration back to Vim for my day-to-day text crunching needs.  Textmate is good but it’s starting to feel like abandonware; who knows when Textmate 2 is going to be released or if it even exists. It’s future feels uncertain. I need certainty. Anyway. Not what I want to talk about.

I want to talk about RSpec syntax highlighing in Vim or my apparent lack thereof.  Check it out:

Missing RSpec keyword syntax highlighting

Missing RSpec keyword syntax highlighting

I’m using MacVim with the vim-ruby gem installed. There has to be an easy way to get RSpec syntax highlighting for all the RSpec keywords although I couldn’t fine one after spending time scouring the Google. Time to break out the ‘Hard Way’.

That SEXY theme you see is a modified port of the TextMate theme called Monokai.  It’s was originally ported to Vim by Thomas Restrepo. Monokai forms a solid basis for some supurb eye candy but my taste requires a bit more flare.

BEHOLD: Monokai Modified for Vim and Monokai Modified for TextMate. It’s rumored that more than a few retinas have been burned by the Monokai - shhh, don’t listen to them, trust me, it’s great!

Anyway, we’re drifting again, let’s continue.

It was a pain to figure out how to tag things with the proper colors when working on a new vim color file. After quite a bit of initial pain I found SyntaxAttr.vim and it made my Vim themeing life quite a bit more enjoyable. SyntaxAttr will tell you the exact syntax highlighting attribute Vim is using to attach a color to an element. In the case of RSpec ‘description’ and ‘it’ keywords Vim was telling me: “<no syntax group here>”. Bummer.

Now we have to go in and force Vim to recognize these keywords as syntax so we can hang colors off of them. You can do this using ‘after’ syntax files. This will allow you to add to an existing syntax file without modifying the original. You can see a more detailed explanation of this in the Vim documentation for syntax files.

I chose to override the ruby.vim syntax file. I did this by creating a blank ruby.vim syntax file in ~/.vim/after/syntax/. You may need to make the after and syntax directories if they don’t already exist. This is what I put into my ruby.vim override file:

UPDATE: This code from Tim Pope (via Josh Nichols in the comment) is better as it will only apply this formatting for ‘_spec.rb’ files. Thanks Josh!

This is just standard syntax file stuff: the first two lines are searching for matches and the next two lines are assigning them to an existing syntax structure.

(If you don’t want to piggy-back on an existing syntax definition [as I've done here] you can take a look at your theme in ~/.vim/colors for the proper way to add a new definition.  Instead of specifying “Define” you would specify your new definition.)

The override ruby.vim finds lines starting with “describe” and “it” and assigns them to the “Define” syntax definition. In Monokai Modified this is HOT PINK (as it should be):

Correct RSpec Syntax Highlighting in Vim

Correct RSpec Syntax Highlighting in Vim

Sweet. We’re in business.


Sometimes Tumblr isn’t enough

Posted: November 24th, 2008 | Author: Adam | Filed under: Uncategorized | Comments Off

Tumblr is great for quick bits of info: quotes, pictures, images sent from my iPhone, etc.  But I’ve started to feel compelled to post information that wants to go long-form.  This is where that will happen.