Getting Fuzzy with CtrlP and Vim
Ever since I started using Vim, I've been looking for ways to improve my workflow. It wasn't until recently that I've felt my use of it had stagnated. I knew there were better ways in doing a lot of things but I never took the time to look into it.
After creating related.vim and writing my previous post, I realized that I didn't know too much about some of the plugins I used. I knew that they were powerful but I had yet to take advantage of that power.
One of the plugins I mentioned was: CtrlP
I was having issues using it because the project I was working in had 15,000+ files but I found a way around it.
First of all, why would I want to use CtrlP? Here's a screenshot of its use in the site's project directory.
As you can see, if I press
ctrl + p and start typing
gru, I can start looking for any files in my project, fuzzily. I dunno about you, but I tend to move between files constantly. CtrlP makes it that much easier. Finding a file by typing a few characters saves an immense amount of time. And as they say, time is money.
What about command-t?
There are a few reasons I chose ctrlp over command-t:
- ctrlp is faster
- ctrlp seems like the more active project
- ctrlp is written in vimscript, while command-t requires and invokes ruby (see reason #1)
- ctrlp is more easily managed using vundle or pathogen
My original workflow consisted of using
grep to look for a file and then copy/paste it into my terminal to edit.
Now it's just a matter of pressing
ctrl + p and typing a few characters.
Not only is there a lot less typing, I don't have to reach for my mouse. No mouse. Mouse is bad. And! I'm still in vim. Ready to edit the next piece of text. Or whatever.
About that issue...
I mentioned that issue of ctrlp being kind of hard to use when the project was huge. You know: 15,000+ files, nested in a folder structure over 9 folders deep... This caused the fuzzy matching to hit the wrong files even when I was typing the entire filename.
Ironically, I saw a Destroy All Software screencast where Gary Bernhardt was using command-t to navigate a file system and I noticed he was searching only in the directories he wanted (ie. the controllers directory in a Rails app) by passing in the directory he wanted into the command.
A few other useful things
Here are some relevant settings from my
.vimrc and what they do:
" Set no max file limit let g:ctrlp_max_files = 0 " Search from current directory instead of project root let g:ctrlp_working_path_mode = 0 " Ignore these directories set wildignore+=*/out/** set wildignore+=*/vendor/** " Search in certain directories a large project (hardcoded for now) cnoremap %proj <c-r>=expand('~/Projects/some-project')<cr> " ga = go api map <Leader>ga :CtrlP %proj/api/<cr> " gf = go frontend map <Leader>gf :CtrlP %proj/some/long/path/to/frontend/code/<cr> ..
When CtrlP is open, you have several options to deal with the file:
f5will clear the CtrlP cache (useful if you add new files during the session)
ctrl + vwill open the file in a vsplit
ctrl + xwill open the file in a split
I've barely touched any of the more advanced features of CtrlP and I'm already seeing significant gains. Who needs a TextMate or Sublime Text license just so they can use
cmd + t for a fast fuzzy search? Not me!
My thanks to Kien Nguyen for creating this plugin and saving the time of many developers all over the world!
- ctrlp.vim Homepage
:h ctrlpfrom within Vim