Wednesday, May 23, 2012

QuickLook from vim

Here's another nifty trick I came up with.  I'm probably not the first to think of it but I'll put it out there anyway.

Quicklook is a neat feature of OSX (versions since Leopard) that allows you to preview the contents of a file without having to open it.  The way I've typically used it is to select a file in finder and hit the spacebar.  OSX then opens a nicely formated window with the contents of my selected file displayed.

I keep a lot of my documentation, notes, etc in markdown format.  Back in January, I attended CodeMash in Sandusky, Ohio and someone turned me onto a markdown rendering Quicklook plugin.  It's a great little tool.  I can keep writing my docs and notes in markdown, but when I Quicklook them, they are rendered for display nicely.  If you have interest in this free plugin, you can download it here.

Anyway, I settled into this habit of editing a file in vim, saving it, then pressing spacebar on it from finder to preview it.  It got me thinking, wouldn't it be great if I could just open Quicklook directly from vim?

After some quick googling around, I found a command-line tool in OSX that does just that.  It's called qlmanage.  From there it was easy enough to come up with a vim key mapping.

Put this (all one line) in your .vimrc file and you should be able to do Quicklooks of your files right from vim simply by hitting \v:

map <Leader>v :write<cr>:sil !/usr/bin/qlmanage -p % > /dev/null &<cr>:redraw!<cr>

It works from both MacVim as well as the terminal version.  You don't have to use it solely with markdown files.  In fact you can use it with any file that Quicklook understands (html for example).