Implementing File Fuzzy Search in Vim with CtrlP Plugin
Learn how to integrate the Ctrl P plugin in order to utilize Fuzzy Search inside of Vim, this is especially helpful when working on Ruby on Rails applications.
Guide Tasks
  • Read Tutorial
  • Watch Guide Video
Video locked
This video is viewable to users with a Bottega Bootcamp license

When it comes to using plug-ins in Vim, one of my very favorite ones in called control P, and what this is gonna allow us to do is have what's called fuzzy search inside of our application, so, let's see what this looks like in a different kind of app. So, I'm going to open up our rails application with Sublime, and dragging this over, you can see, one very cool thing about Sublime is if you run command T, you can run what's called a fuzzy search.

So, I could type in gem and instantly I have access to the gem file, or I could type in application controller and I have access to the application controller. Whenever I'm using Sublime, that is what I do and it makes life much easier.

large

You may have noticed that Vim doesn't quite have that functionality, if I am inside of our file and I go to gem file right here in order to navigate to say the application controller I have to do command E, followed by app controllers application controller in order to switch over. That's not horrible, however, like many other things that we've talked about there is a better way of doing it.

And so that's exactly what we're going to do right now with the control P integration, I'm going to close out of Sublime and open this up. In your show notes you're going to have a gethub.io link for a control P Vim and if you come down to the bottom it says, it walks through how you can clone this.

So you want to first CD into Vim, just like this. If you do not have a dot Vim directory then you'll have to make directory followed by tilde dot Vim.

mkdir ~/.vim

I already have one so I can CD into it. And then type in LS, and so you have a few items. Auto load, back ups, bundle, vundles and if you type LS dash A, it's going to show you also any hidden files, so that is what you have there.

Now what you need to do is be able to do is hit the getclone command. So this is the getclone command.

git clone https://github.com/ctrlpvim/ctrlp.vim.git bundle/ctrlp.vim

I'm not going to do this one because if you take a look at my bundle, so ls bundle, you can see I already have control P installed. If you just, while in the Vim directory, if you run getclone and put it inside of bundle, this control P dot Vim then it's going to install it and then if you look inside of your bundle directory you should have control P. Make sure that you're passing in the right path right here and if you do that, everything should be fine.

So, I'm going to switch back now into the code directory, my devcamp tutorials, followed by Vim guides, perfect okay. So, and I'm also going to go into Vim examples and we have, inside of that, Vim demos. Obviously your system is going to be a little bit different if you've been creating different files and I just wanted to get into our rails application.

So now that I'm back, and you if you were following along you have control P installed. The last thing we need to do is actually pretty basic, we just need to open up our Vim RC file and go into the bottom, add another rule. The rule's right here, I also have included it inside of this, in my own Vim RC file here.

Plugin call to ctrl p for fuzzy file search

Set runtimepath^=~/.vim/bundle/ctrlp.vim

So I'm going to copy this, paste it in, and remove the comment at the beginning. So what this is doing is it's setting the runtime path here for Vim bundle control P dot Vim. So all this is doing is setting the rule so that when Vim loads, it's going to have access to this, so save and close, and now let's open up our Gem file. Now that we're in the Gem file, all you have to do is type in control P and down below it's going to load all of your files in the application.

So here, if I want to type in application controller right here, it's given me access to application controller. Control P type in models dot application record, and I didn't even have to type all of it, I just started typing the first three characters and that popped up. Want to return back to the gem file? Control P, followed by, gem file. And also if you notice one thing, it's trying to, the control P system does it's best to try and find all of the files that match.

So if I just type in gem right here it assumes this first one is the one I want, but I also, as you can see, have a number of other applications that has access to so it may be pulling in a different one so make sure you're using the right one, and you can use your path in order to do that.

large

In order to navigate through these, don't use your home keys like J and K to go up and down, this is where you would use your arrows. And as you can see I can come back, pick any of these, so even switching to different projects, if I hit return, it opens up my gem file for a completely different project if you want to have access to something else.

This is honestly one of the most handy things from a Vim perspective that I've ever had and in fact for this week, when I've been filming this course I removed it from my system, so that I could be showing you the exact system that you were going to be using and I didn't want to have things installed that you weren't going to have access to and this week has been miserable for my rails development. I've had to go back and not use control P when I've been building on a day in day out basis and so that's been, it's made me miss it even more.

So that is how you can implement fuzzy search inside of Vim while using control P as a plug in.