Baptiste Fontaine’s Blog  (back to the website)

How to speed-up Vim’s Command-T plugin

Command-T is a wonderful Vim plugin which allows you to open files with a minimal number of keystrokes. It’s really handy in a large codebase where you only have to type <leader>t, then a couple letters and press enter to open your file. It’s based on a fuzzy matching, which let you skip letters without worrying.

I recently installed the plugin on another machine and noticed it was really low: I had to wait a couple seconds to get the files list everytime. My computer has 8GB RAM so the problem wasn’t there.

The solution is pretty simple: the plugin relies on a C extension which I forgot to compile after the installation.

From the docs:

cd ~/.vim/bundle/command-t/ruby/command-t
ruby extconf.rb
make

It’ll make the plugin incredibly faster. This step is easy to miss if you read the docs too quickly. I wrote this blog post to remember this, I hope it might help a couple others since I didn’t find anything on Google about this issue.

How to remember the difference between conj and cons in Clojure

When I started writing Clojure, I couldn’t memorize the difference between conj and cons and always used one instead of the another. Their name are similar, but cons is used to add an element at the beginning of a vector, while conj is used to add an element at the end of it. How can one memorize this? I found a mnemonic trick over the time that helps me remember this. Here is it:

The trick is to look at the last letter of each function, s and j. As shown in the image below, the s of cons shows the right, while the j of conj shows the left.

This means that cons pushes elements from the left to the right, that is, at the beginning of a vector. conj, on the other hand, pushes elements from the right to the left, which is at the end of a vector. That’s it. Once you see this in your head, you’ll never forget the difference between cons and conj on a vector.

Fixing gnuplot-py’s “unknown aqua terminal” warning on OSX

When plotting with gnuplot-py on OSX, I got an annoying warning saying that terminal aqua is unknown or ambiguous, even when I use a different terminal (e.g. postscript). This terminal doesn’t exist on my Gnuplot installation (4.6.3). In fact, gnuplot-py uses slightly different files depending on your platform. OSX’s one is exactly the same as other UNIX-flavored OSes but its default terminal is aqua. There are two ways to fix the warning, a hacky one I used before this blog post, and a clean one I discovered while writting this post. Hope this help!

The hacky way

Since OSX’s Gnuplot file is the same as Linux’s one apart from its default term, we just need to tell gnuplot-py we’re on Linux:

import sys
_platform = sys.platform # save the normal sys.platform value

if _platform == 'darwin':
    sys.platform = 'linux' # replace it with 'linux'

import Gnuplot, Gnuplot.funcutils # import Gnuplot
sys.platform = _platform # restore it

It temporarily changes sys.platform value, which gnuplot-py is relying on, to "linux", import Gnuplot and then restore it back to its previous value ("darwin").

The clean way

In fact there’s a much cleaner way to fix the warning. The default terminal is stored in GnuplotOpts.default_term, so we just need to change it to another value to fix the warning:

import Gnuplot, Gnuplot.funcutils
Gnuplot.GnuplotOpts.default_term = 'x11'

It’s cleaner than the hacky way because:

  1. It’s clear: the second line is pretty explicit: we change default_term to 'x11'. We don’t even need to add a comment.
  2. It’s more maintainable: if OSX’s gnuplot-py interface change in the future, this piece of code won’t break compatibility while the hacky way will prevent our code to use the new interface.
  3. It’s shorter: one line versus five ones. It’s really easier to introduce bugs when using five lines instead of just one.