DEV Community

Serhat Teker
Serhat Teker

Posted on • Originally published at tech.serhatteker.com on

Vim fzf - Project Root

What is fzf?

fzf is a general-purpose command-line fuzzy finder written in Go. fzf is a fuzzy finder for your terminal, it is a command line application that filters each line from given input with a query that the user types. When the query changes, the results update in realtime.

Vim

Since I spend a lot of my time in Vim trying to find a file either by name or by some code inside a certain file. Streamlining that process is very important.

Every context switch you have to make adds overhead and the possibility of losing focus of what you are trying to find. Therefore it should be as easy as possible, e.g: press a key, type query, press enter to go to matching file.

Finding files wasn’t too much of an issue here. There is a long list of Vim plugins that offer file searching using fuzzy matching or MRU algorithms. Two examples of this are CtrlP and Command-T. I used to be a fan of CtrlP which always managed to do the job.

fzf.vim

fzf has a small builtin Vim interface that already works, but it comes without any existing functionality. The author of fzf also wrote this fzf.vim plugin. It is a small wrapper that provides common functionality. This includes listing files, buffers, tags, git logs and much more!

Fuzzy searching in directories

Coming from CtrlP the first thing I needed was a replacement for fuzzy-finding files. The solution was to use the :Files command provided by fzf.vim. This lists files using your $FZF_DEFAULT_COMMAND environment variable. It opens the currently highlighted file on enter/return

The problem is that when you return a new file in current buffer it changes :Files directory. I am not sure about the background so maybe :cd or :lcdbut I don't like it.

I would like to use some "IDE-like" feature: it finds the project root —if it has .git, .svn, etc, and yet act on current directory if it's not under a project.

function! s:find_git_root()
  return system('git rev-parse --show-toplevel 2> /dev/null')[:-2]
endfunction

command! ProjectFiles execute 'Files' s:find_git_root()
Enter fullscreen mode Exit fullscreen mode

So this solves our issue. Since I am familiar with <C-p> keybing, I would like to use this binding:

map <C-p> :ProjectFiles<CR>
Enter fullscreen mode Exit fullscreen mode

Vim is again "the most efficient" editor in a developer's toolbox.

Links:

Top comments (4)

Collapse
 
jesseleite profile image
Jesse Leite

The problem is that when you return a new file in current buffer it changes :Files directory.

It doesn't change current working directory for me (here's a demo)? It did for you?

PS. Great post though! Have you checked out the other cool things fzf.vim can do?

Collapse
 
serhatteker profile image
Serhat Teker

Actually I figured out long before that the problem is not :Files, it is my other conf in my vimrc:

" Enter automatically into the files directory
autocmd BufEnter * silent! lcd %:p:h

I should update this old (cross) post accordingly :)

Thanks for your comment. I look at your post and it is so awesome. I almost use them all as well.

Btw. which tool do you use to record your screen?

Collapse
 
pernambucano profile image
Paulo Fernandes

Hi, Serhat. Awesome post indeed. I had the same problem as you. I used "set autochdir" because of Nerdtree (to always open the nerdtree window to the right directory), however, when I look for any file using fzf it only looks for the current directory (not the root path if I open a file in a subfolder). How did you solve it? Thanks in advance!

Thread Thread
 
serhatteker profile image
Serhat Teker • Edited

Hi Paulo. Thanks a lot. My ideal solution suggestion would be don't use autochdir at all. I don't use it anymore. I cd into project root then make the search, edit etc.

However if you like to use it though, there are 3 solutions for it:

1 - Using above solution of mine. Add them in your .vimrc
2 - Using :Gfiles command of fzf.vim plugin
3 - Add 'dbakker/vim-projectroot' plugin and map necessary command like below. My mapping is "Ctrl+P" but you can modify according to your favorites.

map <C-p> :<C-u>ProjectRootExe Files<CR>

Tell me these fixes solve your issue.