{"id":42,"date":"2012-10-27T22:41:06","date_gmt":"2012-10-27T20:41:06","guid":{"rendered":"http:\/\/www.hacksrus.net\/blog\/?p=42"},"modified":"2012-10-29T01:58:50","modified_gmt":"2012-10-28T23:58:50","slug":"using-vims-ctrlp-plugin-with-rails-and-guard","status":"publish","type":"post","link":"https:\/\/www.hacksrus.net\/blog\/2012\/10\/using-vims-ctrlp-plugin-with-rails-and-guard\/","title":{"rendered":"Speed up CtrlP vim plugin and automatically clear it&#8217;s cache using guard"},"content":{"rendered":"<p>I just changed from the famous Command-T plugin to <a title=\"CtrlP\" href=\"https:\/\/github.com\/kien\/ctrlp.vim\">CtrlP<\/a>.<\/p>\n<p>Command-T provides a file navigator for vim, such as the modal that popped up in Textmate when you hit \u2318-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.<\/p>\n<p>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).<\/p>\n<p>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&#8217;t rebuilt automatically when you changed a file.<\/p>\n<p>I couldn&#8217;t really accept this and investigated a bit where it stored it&#8217;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:<\/p>\n<pre>tmp\/cache\/sass\/9e16d302b803e71649858656bd7da287cac6ab9b\/_alternating-rows-and-columns.scssc\r\ntmp\/cache\/assets\/D3B\/DD0\/sprockets%2Fba7a4ad6548c886b0d4054293aec5e07<\/pre>\n<p>As I will never need to open these anyway, I would very much prefer to skip them in the indexing process. CtrlP&#8217;s <a title=\"README\" href=\"https:\/\/github.com\/kien\/ctrlp.vim\/blob\/master\/readme.md\">README<\/a> gave an excellent hint:<\/p>\n<pre lang=\"vim\">set wildignore+=*\/tmp\/*,*.so,*.swp,*.zip     \" MacOSX\/Linux\r\nset wildignore+=*\\\\tmp\\\\*,*.swp,*.zip,*.exe  \" Windows<\/pre>\n<p>Restarting vim and trying to bring up CtrlP immediately showed the effect. The startup time was reduced significantly.<\/p>\n<p>This has only solved one of the issues, however. The reloading issue still existed.<br \/>\nHere 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&#8217;s &#8211;remote-send option and others. All I was looking for was a quite and dirty solution and here it is:<\/p>\n<p>I immediately thought of <a title=\"guard\" href=\"https:\/\/github.com\/guard\/guard\">guard<\/a> 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 <a title=\"guard-addremove\" href=\"https:\/\/github.com\/pehlert\/guard-addremove\">guard-addremove<\/a>.<\/p>\n<p>To use it to clean your CtrlP cache, make sure that you add guard and guard-addremove to your project&#8217;s Gemfile:<\/p>\n<pre lang=\"ruby\">gem 'guard'\r\ngem 'guard-addremove'<\/pre>\n<p>Now edit your Guardfile so that it has a statement like this in it:<\/p>\n<pre lang=\"ruby\">guard 'addremove' do\r\n  # Ignore Vim swap files\r\n  ignore \/~$\/\r\n  ignore \/^(?:.*[\\\\\\\/])?\\.[^\\\\\\\/]+\\.sw[p-z]$\/\r\n\r\n  watch(\/.*\/) { `rm ~\/.cache\/ctrlp\/%Users%pascal%Projects%example.txt &amp;&gt; \/dev\/null` }\r\nend<\/pre>\n<p>You will have to take care to get the path to your cache file right. To do this run vim in your project&#8217;s directory, open CtrlP and and check out the contents of $HOME\/.cache\/ctrlp in another window.<\/p>\n<p>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.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 \u2318-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 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-42","post","type-post","status-publish","format-standard","hentry","category-programming"],"_links":{"self":[{"href":"https:\/\/www.hacksrus.net\/blog\/wp-json\/wp\/v2\/posts\/42","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.hacksrus.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.hacksrus.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.hacksrus.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.hacksrus.net\/blog\/wp-json\/wp\/v2\/comments?post=42"}],"version-history":[{"count":12,"href":"https:\/\/www.hacksrus.net\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions"}],"predecessor-version":[{"id":62,"href":"https:\/\/www.hacksrus.net\/blog\/wp-json\/wp\/v2\/posts\/42\/revisions\/62"}],"wp:attachment":[{"href":"https:\/\/www.hacksrus.net\/blog\/wp-json\/wp\/v2\/media?parent=42"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.hacksrus.net\/blog\/wp-json\/wp\/v2\/categories?post=42"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.hacksrus.net\/blog\/wp-json\/wp\/v2\/tags?post=42"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}