Browsing changed files in Vim with FZF

I use junegunn/fzf to browse files in Vim. It’s an extremely fast fuzzy finder written in Go. If you compare it to something like [CtrlP](kien/ctrlp.vim) you’ll find it blazingly fast. And what’s cool, it doesn’t need to cache files because it’s THAT fast (no need to hit F5 every time you add a new file into the project). If you didn’t hear about it go try it out!

My most used command is obviously:

noremap <Leader><Leader> :Files<cr>

, which opens the dialog with all files in the working tree to fuzzy find from. I find <Leader><Leader> combination very handy (it’s similar to Shift-Shift in Intellij-based products).

Sometimes (quite often actually) in the middle of development cycle I don’t immediately remember what file I need. In this case I don’t want to see all the files in the working tree, I just want to browse the files I’m working on right now i.e. changed files and newly added ones.

How do we do that?

Well, with FZF ability to take anything as an input, it’s just a matter of figuring out a proper git command. Here it is:

command! Fzfc call fzf#run(fzf#wrap(
  \ {'source': 'git ls-files --exclude-standard --others --modified'}))
noremap <Leader>] :Fzfc<cr>

As you can see there’s a special command called fzf#run which can accept an arbitrary command line command, in this case

git ls-files --exclude-standard --others --modified

You can try to run it in command line to see how it works, but it’s really simple. From git help ls-files:

git-ls-files — Show information about files in the index and the working tree
--exclude-standard — Add the standard Git exclusions: .git/info/exclude, .gitignore in each directory, and the user’s global exclusion file.
--others — Show other (i.e. untracked) files in the output
--modified — Show modified files in the output

So no magic here.

Now that you know how to use FZF with custom command go try some other ideas which would work for your workflow. The options are limitless.

Have fun!


Now read this

11 JavaScript Blogs to Follow in 2017

There’s a huge benefit in running a JavaScript newsletter. You get to know about many smart people from whom you can learn a lot. Here are the most interesting blogs I stumbled upon. Feel free to add them directly to your preferred RSS... Continue →