Vim Ctrl-P Plug-in

Ctrl-P plug-in allows us to open files with fuzzy names or regular expression matches. It boosts my productivity at lot! I am glad to know about this plug-in and I would like to share my experience with you.

Installation

If you are managing Vim plug-ins with Vundle [1], you can install Ctrl-P by adding the following line to .vimrc:

Plugin 'ctrlpvim/ctrlp.vim'

And then, run the following command:

$ vim +PluginInstall +qall

Usage

After installing Ctrl-P plug-in, you will be able to open files with following steps:

  1. Press Ctrl-P. In a second, a file list will prompt up. It will list the files under the current working directory (recursively.)
  2. Type the file name. Ctrl-P will list the matching files. Select the file with Up or Down button on the keyboard.
  3. Press Enter to open the file in the current buffer. Or, press Ctrl-T to open the file in a new tab.

To search files with regular expression, press Ctrl-P again when the file list are prompted, i.e. press Ctrl-P twice if you are in the normal mode.

File Name Cache

By default, Ctrl-P will walk through directories and build a file name cache to speed up the pattern matching. As a side-effect, we won't be able to find the newly created files with Ctrl-P. To workaround the problem, we have to rebuild the file name cache.

To rebuild the cache, press Ctrl-P and then F5. Ctrl-P will scan the directories again. You will be able to see the newly created files in a moment.

Alternatively, you can run :CtrlPClearCache in Vim command mode to clear the cache. Ctrl-P will rebuild the cache if you press Ctrl-P later.

Open Files in New Tabs

I personally don't like the default behavior of Enter. I wish I could open the file in a new tab no matter I pressed Enter or Ctrl-T. Fortunately, we can change the default behavior by adding following lines to .vimrc:

let g:ctrlp_prompt_mappings = {
  \ 'AcceptSelection("e")': [],
  \ 'AcceptSelection("t")': ['<cr>', '<c-m>'],
  \ }
[1]If you are not aware of Vundle, it is highly recommended to give it a trial. I wrote an article several years ago.