36

It’s dangerous to Vim alone! Take Fzf. - Jesse Leite - Medium

 4 years ago
source link: https://medium.com/@jesseleite/its-dangerous-to-vim-alone-take-fzf-283bcff74d21
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

It’s dangerous to Vim alone! Take Fzf.

Image for post
Image for post

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 by the legendary Junegunn, which is a wrapper for the command line fuzzy finder with the same name, fzf. On the surface it seems like a just another fuzzy finder, 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*.

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, 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 to show everything including hidden files.

Image for post
Image for post

Buffer Finder

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

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.

Image for post
Image for post

Tag Finder

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

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 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, where searching for indexpostcon will jump you to the indexmethod of your PostController.

Image for post
Image for post

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.

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).

Image for post
Image for post

Project Finder

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 or rg, or they look to search wrappers like 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 syntaxavailable 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 🚜 If you are interested in learning more about my project searching workflow, I wrote about it here!

Image for post
Image for post

Help Finder

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).

Image for post
Image for post

But wait, there’s more!

Fuzzy search defined commands, whether they be user defined, plugin defined, or native commands:

nmap <Leader>C :Commands<CR>

Fuzzy search through :command history:

nmap <Leader>: :History:<CR>

Fuzzy search key mappings, which is great for checking against current mappings before defining a new one:

nmap <Leader>M :Maps<CR>

Fuzzy search filetype syntaxes, and hit Enter on a result to set that syntax on the current buffer:

nmap <Leader>s :Filetypes<CR>

So many possibilities, 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 ❤️


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK