66

I'm looking for a way to make Vim have the ability to open a file by fuzzy-searching its name.

Basically, I want to be able to define a project once, and then have a shortcut which will give me a place to type a file name, and will match if any letters match up.

This kind of functionality exists in most editors I've seen, but for the life of me I can't understand how to get Vim to do this.

Note that I'm looking for something that won't require me to have any idea where in my directory tree a file is. I just want to be able to open it by the filename, regardless of what directory it's in.

Thanks

5 Answers 5

73

There are two great vim plugins for this.

ctrlp:

  • Written in pure VimL
  • Works pretty much everywhere
  • Supports custom finders for improved performance
  • Most popular fuzzy search plugin for Vim

Command-T:

  • Written in C, VimL and Ruby
  • Fast out of the box
  • Requires +ruby support in Vim
  • Recommends Vim version >= 7.3

EDIT:

I use CtrlP with ag as my custom finder and it's incredibly quick (even on massive projects) and very portable.

An example of using ag with CtrlP:

if executable('ag')
  " Use Ag over Grep
  set grepprg=ag\ --nogroup\ --nocolor

  " Use ag in CtrlP for listing files. Lightning fast and respects .gitignore
  let g:ctrlp_user_command = 'ag %s -l --nocolor -g ""'
endif
3
  • 3
    Since posting this question, I've switched to using ctrlp, as have many other Vim'ers. I now consider it the de-factor file-switcher-plugin (with Command-t close behind).
    – Edan Maor
    Commented Jun 27, 2013 at 6:34
  • 3
    Use ctrlp with a custom finder command, e.g. ack or even better ag (the silver searcher). This will really speed up your search compared to command-t even if you enable the c find command in it. My experience with watchman was kind of slow
    – Jan Weitz
    Commented May 24, 2014 at 0:39
  • @hasen using ag is much faster than the default, grep. This just calls ag instead of calling grep when searching for files. Commented Oct 12, 2015 at 14:14
6

Basic solution

Simply add this to your .vimrc

nnoremap <C-p> :find ./**/*

Pressing Ctrl+p will now allow you to fuzzyfind files in your current working directory and sub-directories thereof. Use the tab key to cycle through options.

Related solution

For those who want to keep it basic i.e. no plugins, this entertaining video shows another way to achieve fuzzy file find in vim.

They actually use

set path+=**
set wildmenu

in their .vimrc to find files in current sub-directories.

For example, with :find *Murph followd by tab, I would find the files KilianMurphy2012Why.R and KilianMurphy2014ROLE.R in subdir code which I can cycle through with the tab key. The first solution above has the advantage that the relative path is also shown.

Note that your current working directory will matter and that other files on your path (:set path?) will also be found with the this type of solution. The wildmenu option adds visual information and is not essential.

For a keyboard shortcut, add

nnoremap <C-p> :find *

to your .vimrc. Now you will be able to quickly search for files inside your project/current dir with Ctrl+p in normal mode.

4

CommandT for Vim is very much the comparable feature as in TextMate. My work flow is now

1) open up MacVim

2) :cd ~/my_project

3) (I have this mapped as described in the installation help)

4) C-v the file to open the file in a vertical split, or CR to open a new horizontal split.

5) to close the split, use :bd (buffer delete)

6) to switch to another buffer, I have BufferExplorer installed, so just \be and select

This workflow is comparable to TextMate, it takes a while to get used to, and I'm still learning.

1
  • This is awesome. Are there any plugins like this that do not require Ruby and compiling? Being on Windows a lot, that would be just amazing. Commented Jan 20, 2012 at 18:31
2

What about http://www.vim.org/scripts/script.php?script_id=1984 Then there is http://github.com/jamis/fuzzy_file_finder .

Also see these blog posts: http://weblog.jamisbuck.org/2008/10/10/coming-home-to-vim and http://weblog.jamisbuck.org/2009/1/28/the-future-of-fuzzyfinder-textmate

HTH

3
  • I looked at that, but it didn't seem to do what I wanted (it looks like it doesn't search for all files in the directory). Am I just setting it up incorrectly?
    – Edan Maor
    Commented Mar 3, 2010 at 15:36
  • 1
    You probably are. Normally, all files in a directory and all its subdirectories are included. Commented Mar 3, 2010 at 18:06
  • Also: stackoverflow.com/questions/1894614/… Commented Mar 3, 2010 at 18:06
0

fzf is a powerful tool for fuzzy file finding, and there is a vim integration. Check out the official installation instructions or keep reading for an abbreviated installation guide:

curl -fLo ~/.vim/autoload/plug.vim --create-dirs \
    https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
  • add the following to your vimrc
call plug#begin()
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
call plug#end()
  • install plugins (like so, or by entering :PlugInstall in vim)
$ vim +:PlugInstall

Now you can run :Files in vim for a fuzzy file search.

You can add your own shortcut keys to your vimrc such as the following for control+p:

nnoremap <C-p> :Files<cr>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.