Speed up CtrlP vim plugin and automatically clear it’s cache using guard

I just changed from the famous Command-T plugin to CtrlP.

Command-T provides a file navigator for vim, such as the modal that popped up in Textmate when you hit ⌘-t. While it worked well for a while, it relied on an external Ruby library, causing issues with rvm as outlined in my previous post. Then recently it refused to work with MacVim entirely, due to a rendering issue that made it unusable.

While I looked for a way to fix the issue, I stumbled upon CtrlP. CtrlP is a native vim replacement which not only makes it cleaner by design but also a bit faster (and most important for me, it actually worked with my setup).

One issue I never really got around however was the fact that a) building the cache took quite a while, causing quite a delay when you bring it up for the first time and b) the cache wasn’t rebuilt automatically when you changed a file.

I couldn’t really accept this and investigated a bit where it stored it’s cache file. When I found it in $HOME/.cache/ctrlp/$ESCAPED_PROJECT_PATH.txt, I realized that it was full of entries like this:

tmp/cache/sass/9e16d302b803e71649858656bd7da287cac6ab9b/_alternating-rows-and-columns.scssc
tmp/cache/assets/D3B/DD0/sprockets%2Fba7a4ad6548c886b0d4054293aec5e07

As I will never need to open these anyway, I would very much prefer to skip them in the indexing process. CtrlP’s README gave an excellent hint:

set wildignore+=*/tmp/*,*.so,*.swp,*.zip     " MacOSX/Linux
set wildignore+=*\\tmp\\*,*.swp,*.zip,*.exe  " Windows

Restarting vim and trying to bring up CtrlP immediately showed the effect. The startup time was reduced significantly.

This has only solved one of the issues, however. The reloading issue still existed.
Here is what I came up with to solve it, and it may not be the cleanest or best solution. Just for the record, there likely are better ways which involve using vim’s –remote-send option and others. All I was looking for was a quite and dirty solution and here it is:

I immediately thought of guard which I use to automatically run my tests when files change. In order to be able to execute a command only when a file has been added or removed, I cloned guard-shell to make guard-addremove.

To use it to clean your CtrlP cache, make sure that you add guard and guard-addremove to your project’s Gemfile:

gem 'guard'
gem 'guard-addremove'

Now edit your Guardfile so that it has a statement like this in it:

guard 'addremove' do
  # Ignore Vim swap files
  ignore /~$/
  ignore /^(?:.*[\\\/])?\.[^\\\/]+\.sw[p-z]$/
 
  watch(/.*/) { `rm ~/.cache/ctrlp/%Users%pascal%Projects%example.txt &> /dev/null` }
end

You will have to take care to get the path to your cache file right. To do this run vim in your project’s directory, open CtrlP and and check out the contents of $HOME/.cache/ctrlp in another window.

I am aware that this can be improved. Someone who is more experienced with vim than I am might also be able to adjust it so that it can be used with NERDTree and others. I would love to see your examples in the comments.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.