Install CtrlP to save time hunting for files in Vim
Vim is my favorite text editor. I've used it exclusively since 2004, having fallen in love with its near-infinite customizability and "one tool, one job" philosophy.
But if there's one feature that's always felt missing, it's a great fuzzy file
search. Other text editors like Atom, TextMate, and Sublime offer the user a
convenient way to search files by typing partial substrings of the full
filename. So if you have a file in lib/foobar/baz.rb
, typing foobaz
into the
fuzzy finder would find the file.
This becomes especially useful in the context of modern JavaScript, where you'll often have file trees that look like this:
reducers/todos.js
actions/todos.js
components/TodoList.js
Using tab completion to resolve these paths works, but it's a lot of keyboard crunching. Not the smoothest approach.
Luckily, CtrlP offers a turnkey solution.
Installation
To install CtrlP, clone it into your ~/.vim/bundle
directory:
git clone https://github.com/ctrlpvim/ctrlp.vim.git ~/.vim/bundle/ctrlp.vim
Then, add it to your Vim's runtime path in your ~/.vimrc
:
set runtimepath^=~/.vim/bundle/ctrlp.vim
You'll probably also want to tell CtrlP to ignore files matching some paths by
setting the wildignore
option in your ~/.vimrc
:
set wildignore+=*/.git/*,*/.hg/*,*/.svn/*,*/build/*,*/node_modules/*
This tells CtrlP to ignore version control meta files (Git/Mercurial/SVN), files
inside build
directories (I use Middleman
frequently and it dumps its output files here), and your NPM node_modules
directory. If you have other project-specific paths you don't want to show up in
your fuzzy search results, add them here.
Usage
To use CtrlP, open Vim in the root directory of the codebase of your choice and
press, well, Ctrl+P
. A buffer will appear at the bottom of your Vim. Type some
characters that are a part of the file you want to find, and you'll see the list
of files reduce to those matching your query. Press Return and the selected file
will open!
Hopefully CtrlP will improve your workflow like it has improved mine. Reducing the friction between your brain and your fingers is paramount in creating a work environment that enables great work instead of getting the way. Cheers!