It's dangerous to Vim alone! Take Fzf.
Tags: vim, fzf, programming, productivity
Published: 2019-08-15 15:48:08

I've been using Vim emulators for a few years, but apparently that wasn't nerdy enough, so I recently made the switch to terminal Vim.  Jumping into the world of Vim plugins was daunting, but one specifically has made it a lot easier.  Enter [fzf.vim](https://github.com/junegunn/fzf.vim) by the _legendary_ [Junegunn](https://github.com/junegunn), which is a wrapper for the command line fuzzy finder with the same name, [fzf](https://github.com/junegunn/fzf).  On the surface it seems like a just [another fuzzy finder](https://github.com/kien/ctrlp.vim), but it's much more than that!  Let me show you some of my mappings and how I have been using it...

## File Finder

The quintessential fuzzy file finder 👌  Here I've mapped `<Leader>f` to search for git tracked files, and `<Leader>F` to search all files*.

```vim
nmap <Leader>f :GFiles<CR>
nmap <Leader>F :Files<CR>
```

I've never used a faster fuzzy finder, and it seems to sort results more intelligently than most.  It's also worth noting that every fzf command displays results in what is called [extended search mode](https://github.com/junegunn/fzf#search-syntax), which allows you to narrow results in realtime using an intuitive regex-like syntax.

*You can customize `:Files` output by setting `FZF_DEFAULT_COMMAND` in your shell config.  [I have mine set](https://github.com/jesseleite/dotfiles/blob/c5fc4a2569f505960eaa542ce6ec0750a801896e/zsh/zshrc#L39) to show everything including hidden files.

![](https://thepracticaldev.s3.amazonaws.com/i/5rr3yxrdmd5tgjalor1q.gif)

## Buffer Finder

Here I've mapped `<Leader>b` to search for open buffers, and `<Leader>h` to search buffer history.

```vim
nmap <Leader>b :Buffers<CR>
nmap <Leader>h :History<CR>
```

These two make it easy to switch between buffers, and order their results by most recent so you can easily switch between your two most recent files without being required to enter a search query.

![](https://thepracticaldev.s3.amazonaws.com/i/46d9nyacax62exoi7i2x.gif)

## Tag Finder

Here I've mapped `<Leader>t` to search for tags in current buffer, and `<Leader>T` to search for tags across project.

```vim
nmap <Leader>t :BTags<CR>
nmap <Leader>T :Tags<CR>
```

The former doesn't require a tags file, and is a great alternative to packages like [tagbar](https://github.com/majutsushi/tagbar) because it's often quicker to fuzzy search for a method name, than it is to navigate your way through a second window.  The latter is extremely powerful in combination with a package like [gutentags](https://github.com/ludovicchabant/vim-gutentags), where searching for `indexpostcon` will jump you to the `index` method of your `PostController`.

![](https://thepracticaldev.s3.amazonaws.com/i/sce6jwcopq0rioyi9ld7.gif)

## Line Finder

Here I've mapped `<Leader>l` to search for lines in current buffer, `<Leader>L` to search for lines in loaded buffers, and `<Leader>'` to search for marked lines.

```vim
nmap <Leader>l :BLines<CR>
nmap <Leader>L :Lines<CR>
nmap <Leader>' :Marks<CR>
```

Honestly, you will get more power out of `/` and `ag`, but they require more thought because they aren't fuzzy searches.  Being able to fuzzy search through both marked and unmarked lines is fast and forgiving.  These can also prove useful when tags aren't available (eg. in .vue components or plain .txt files).

![](https://thepracticaldev.s3.amazonaws.com/i/vs45aigu6j2p8idte4tg.gif)

## Project Finder

```vim
nmap <Leader>/ :Ag<Space>
nmap <Leader>/ :Rg<Space>
```

When you need project searching power, most people either bounce back out to the terminal to run [ag](https://github.com/ggreer/the_silver_searcher) or [rg](https://github.com/BurntSushi/ripgrep), or they look to search wrappers like [ack.vim](https://github.com/mileszs/ack.vim).  Wrappers are nice because they allow you to stay in Vim, but they often expect you to specify your searchable path _before_ seeing the results.

Fzf's `:Ag` and `:Rg` wrapper commands allow you to focus on your search query first, then narrow down results in realtime using the same [extended search mode syntax](https://github.com/junegunn/fzf#search-syntax) available to all of fzf's fuzzy finders.  Hitting `Enter` on a single result will open that file, skipping the quickfix window altogether.  If you are doing a larger refactor, you can `Tab` to select multiple results, `ALT-A` to select all results, then `Enter` to populate the quickfix window.

It's an incredibly powerful workflow, and my only beef is that it doesn't allow you to pass command line options to `ag` or `rg` out-of-the-box  ...but it's okay, because [I wrote a plugin for that](https://github.com/jesseleite/vim-agriculture) 🚜   If you are interested in learning more about my project searching workflow, [I wrote about it here!](/posts/4/project-search-your-feelings)

![](https://thepracticaldev.s3.amazonaws.com/i/to3904mrhce6737unp9y.gif)

## Help Finder

```vim
nmap <Leader>H :Helptags!<CR>
```

Sometimes navigating Vim's `:help` documentation can be painful.  Finding the right article can be difficult.  Fzf's `:Helptags` improves this experience, especially when you run it with a bang `!` to view results in fullscreen (which by the way, also works on fzf's other commands).

![](https://thepracticaldev.s3.amazonaws.com/i/dmtpzxwm6c1e8x39pk4l.gif)

## But wait, there's more!

Fuzzy search defined commands, whether they be user defined, plugin defined, or native commands:
```vim
nmap <Leader>C :Commands<CR>
```

Fuzzy search through `:command` history:
```vim
nmap <Leader>: :History:<CR>
```

Fuzzy search key mappings, which is great for checking against current mappings before defining a new one:
```vim
nmap <Leader>M :Maps<CR>
```

Fuzzy search filetype syntaxes, and hit `Enter` on a result to set that syntax on the current buffer:
```vim
nmap <Leader>s :Filetypes<CR>
```

[So many possibilities](https://github.com/junegunn/fzf.vim#commands), and it's highly extendable if you want to create your own fuzzy finders!

## In a deku nutshell...

Fzf's biggest strength is not just it's blazing speed, but also it's consistent interface and extendability for fuzzy finding all the things.  It's made me faster at nearly everything I do, and quickly replaced packages like ctrlp, tagbar, ack.vim, etc.  If anything, I hope you fuzzy find this enlightening!

## Like this article?

Please [give me a follow](https://twitter.com/jesseleite85) ❤️