Improving Vim Workflow With fzf
Tags: vim
Published: 2020-11-18 15:47:00

---
title: Improving Vim Workflow With fzf
published: true
date: 2020-11-17 00:00:00 UTC
tags: vim
canonical_url: https://pragmaticpineapple.com/improving-vim-workflow-with-fzf/
---

![Pineapple on a beach](https://pragmaticpineapple.com/static/d844e61d88396079b127a2cb42ebe966/1c72d/cover.jpg "Pineapple on a beach")

Photo by [Pineapple Supply Co.](https://unsplash.com/@pineapple?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/s/photos/improve-pineapple?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText)

If you never heard of [fzf](https://github.com/junegunn/fzf), it is a very handy general-purpose command-line fuzzy finder. Besides command-line, it is also a popular Vim plugin. If you’re wondering, a fuzzy finder is a tool that helps you find what you’re looking for without needing to write the full name.

As a Vim user, I am always obsessed with doing a thing in the fewer keystrokes as possible. Having the ability to open a file in Vim quickly is super useful to me, and you will see why soon.

But, did you know that this fuzzy finder - fzf, can do a lot more than you thought? Oh yeah, the fuzzy search is just the tip of the iceberg here. It is like wine; the more you leave it on your computer, the more flavor and sweetness it accumulates from that command-line. Let’s dive in and find out how you can increase your productivity with fzf inside Vim.

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#starting-off)Starting off

To be able to do the things I am doing in this blog post, you will need a couple of plugins. If you’re not using [vim-plug](https://github.com/junegunn/vim-plug) for installing other Vim plugins, then you are missing out. Go ahead and set that up, and you can add the following:

```
Plug 'junegunn/fzf', { 'do': { -> fzf#install() } }
Plug 'junegunn/fzf.vim'
```

Then, install these plugins with `:PlugInstall` or use this shortcut I use:

```
" Source Vim configuration file and install plugins
nnoremap <silent><leader>1 :source ~/.vimrc \| :PlugInstall<CR>
```

You can then press your leader key and number 1 to install and apply all the changes in your `.vimrc`.

Now, to the coolest part!

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#the-magic-finder)The magic finder

Everything we need is installed, and we can get to the practical part. As we said before, fzf is a fuzzy finder, a file selector if you want. Let’s try out that feature right off. To get the file picker opened, type `:Files` in a Vim session. You should get something like this:

![Open :Files](https://pragmaticpineapple.com/static/47be299b0a37790ebdd8396d20b872fa/fcda8/open-files.png "Open :Files")

What happened is fzf opened a small window at the bottom showing files in our directory. If you look at the newly opened window, you will see the list of files, and the **preview** of the currently selected file on the right of the window. So you got your files on the left and their preview on the right. Are you already impressed as I was? Cool, let’s move on.

If you don’t like the window, you can fine-tune easily with fzf customization options. But if you want any fzf command in fullscreen, you can append `!` at the end of the command. For example, let’s do `:Files!` and you should see the following:

![Open :Files! in fullscreen](https://pragmaticpineapple.com/static/71e5fe325ab9a32de02f08763196d067/fcda8/open-files-fullscreen.png "Open :Files! in fullscreen")

But, I don’t use `:Files` that often. As a matter of fact, I only use`:GFiles` command by fzf. `:GFiles` will open a file picker for your Git files, ignoring ones in the `.gitignore`. Using it is pretty neat in JavaScript projects where `node_modules` files tend to kill the mood when running`:Files`. Let us compare the same project with `:Files` and `:GFiles` commands:

![:Files in a JavaScript project](https://pragmaticpineapple.com/static/7b9132a9a4e78a2fc2087e4059a22cbf/fcda8/files-js-project.png ":Files in a JavaScript project")

 Yuck! `node_modules` everywhere.

![:GFiles in a JavaScript project](https://pragmaticpineapple.com/static/6cd545cf7ed03bb38decb5c1ae2d9949/fcda8/git-files-js-project.png ":GFiles in a JavaScript project")

 Ah, that's better.

Notice the difference? It feels like I just dropped a heavy backpack off my back by not having those `node_modules/**` files popping up. Anyway, let’s see this bad boy in action when searching for files to edit.

![:GFiles in action GIF](https://pragmaticpineapple.com/5a84a887e91f3aea28f8692bcc518fc0/gfiles-in-action.gif)

That’s it, and it is looking pretty good. Only thing I dislike about using `:GFiles` is that it won’t include your new files unless you add them to the Git index with `git add`. Also, I’d like some syntax highlighting to be there out of the box when previewing files, but we will cover that in another blog post. In the meantime, consider subscribing to the [newsletter](./newsletter) to get similar posts like this.

To sum up, fzf is useful and quick as a flash when searching for files. And, as Drake below says, resort to using `:GFiles` or try to configure `:Files` to ignore certain files and paths.

![Drake fzf choices - go with :GFiles instead of :Files](https://pragmaticpineapple.com/static/643d1c18c9fd8e42484fcaffaa4d99eb/41099/drake-no-files-yes-gfiles.jpg "Drake fzf choices - go with :GFiles instead of :Files")

To make this super fast for you, you can create a shortcut. I open `:GFiles`with CTRL + p. I got left with this in my muscle memory from the legendary[ctrlp](https://github.com/kien/ctrlp.vim) plugin. To have `:GFiles` wired up, so it opens with CTRL + p, you can add the following to your `.vimrc`:

```
nnoremap <C-p> :GFiles<Cr>
```

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#speed-search-your-project)Speed search your project

What blows my mind from time to time is other things you can do with fzf in Vim. For example, you can use[The Silver Searcher](https://github.com/ggreer/the_silver_searcher)or[ripgrep](https://github.com/BurntSushi/ripgrep) with fzf. To search with The Silver Searcher, type `:Ag` and a term you want to search. And, to search with ripgrep, type `:Rg` and the term. Of course, to have these commands work, you need the respective libraries installed in your environment.

I use `:Ag`, and it performs wonderfully. It is wired to CTRL + g for me, so I access it quickly. To have this or a similar setup, add the following to your `.vimrc`:

```
nnoremap <C-g> :Ag<Cr>
```

The shortcut above will open up the `:Ag` search windows at the bottom with the preview of the file. I find it super helpful and quick when I need to search for a word in a project. Let’s see `:Ag` in action.

![:Ag in action](https://pragmaticpineapple.com/0c1dc6a8efd411223955dd76a20783a6/ag-in-action.gif)

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#buffed-up)Buffed up

You can search all your open buffers with fzf by typing `:Buffers`. I keep a shortcut at my leader key (the Space key, BTW) + b. Like so:

```
nnoremap <silent><leader>l :Buffers<CR>
```

With that command, you will get a buffers explorer where you can quickly switch between open files. I hope that helps. Let’s see how it looks:

![:Buffers in action](https://pragmaticpineapple.com/static/cb72ecbb277eb7bb278051d84c0b5f96/fcda8/buffers.png ":Buffers in action")

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#ascii-art-kind-of)ASCII art (kind of)

If you are a fan of drawing inside the terminal, then you are going to love this one. Try typing `:Commands` if you are using the [vim-fugitive](https://github.com/tpope/vim-fugitive) plugin. The plugin by itself is a pretty awesome wrapper around Git, just if you never want to leave the warmth of your Vim session. Anyways, if you type`:Commands` you should get a tree of your project commits like so:

![Commit art](https://pragmaticpineapple.com/static/4cef7957e6047955944c695930e80804/fcda8/commits-art.png "Commit art")

Pretty cool, huh? You can scroll up and down and checkout changes at each commit. You can even enter the commit you are interested in and check out changes made there. All of this is possible thanks to the `vim-fugitive`, so check it out. A blog post about using Git inside Vim is coming, so be sure to subscribe to the [newsletter](/newsletter).

## [<svg aria-hidden="true" focusable="false" height="16" version="1.1" viewbox="0 0 16 16" width="16"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>](#a-quick-summary)A quick summary

Vim ecosystem has a lot of plugins, and fzf is one great plugin. You can search for files with `:GFiles` and `:Files`. If you want to do a text search, try using`:Ag` or `:Rg`, which use The Silver Searcher and ripgrep, respectively. Tired of slow switching between open buffers - try out `:Buffers`. Or, if you want some nice commit information, do `:Commits`.

These are just a couple of commands and tricks fzf has, be sure to check out their README for more information. Also, stay tuned for more posts like these from me and consider subscribing to the [newsletter](/newsletter). If you found the blog post interesting, make sure to spread the word and share it with your friends and coworkers:

{% twitter 1328670506803924992 %}

Until the next one, cheers!