DEV Community

0xkoji
0xkoji

Posted on

Get along with fzf for Terminal

This is for my next Git post.
I started playing with .gitconfig and got known a great tool, fzf.
fzf can make things easy!!!

fzf
fzf is a general-purpose command-line fuzzy finder.
https://github.com/junegunn/fzf

In this post, I will show what we can do, how to install and a couple of usages.

What We Can Do with fzf

In the following gif, I tried history first then I used h | fzf (h is an alias of history). As you can see, you can find out the command you want to see easily with fzf.
fzf allows us to use git checkout interactively like below.
In addition, you can customize commands with fzf and many people write scripts. In the bottom of this post, I put the link which is very useful for people who want to try fzf. You can also set shortcut keys.

fzf

git_with_fzf

Install

This post supports MacOS and Ubuntu since I have used both of them.

# MacOS
$ brew install fzf

# Ubuntu
$ sudo apt-get install fzf
Enter fullscreen mode Exit fullscreen mode

If you are using Windows, I highly recommend you to start using chocolatey(https://chocolatey.org/) which is a package manager software.

Simple Usage

File search

I did this command in my Gatsby site folder and searched my components.

$ fzf
$ fzf --reverse
Enter fullscreen mode Exit fullscreen mode

file_search

History search

This is very useful to me since I usualy do h | grep 'something' for checking commands I typed before.

$ history | fzf
$ history | fzf --reverse
Enter fullscreen mode Exit fullscreen mode

history_search

If you add --reverse you can change the layout like this(maybe better)
history_reverse

Open file with fzf

$ open $(fzf)
Enter fullscreen mode Exit fullscreen mode

open_file

Update package with fzf

Sometimes I do typo then get an install error lol

$ brew upgrade $(brew outdated | fzf)
# Then choose a package. Probably user --multi.
Enter fullscreen mode Exit fullscreen mode

update_package

jq with API

This is not helpful, but if we need to check a big json return from API with curl command, this might be good ???
By the way, for jq you need to instal jq.

$ curl https://samples.openweathermap.org/data/2.5/weather\?lat\=35\&lon\=139\&appid\=b6907d289e10d714a6e88b30761fae22 | jq | fzf --reverse
Enter fullscreen mode Exit fullscreen mode

with_jq

More Information

Examples

You can see more information here (https://github.com/junegunn/fzf/wiki/examples).

Key bindings for command-line

Suggestion from @vonheikemen
https://github.com/junegunn/fzf#key-bindings-for-command-line

Top comments (1)

Collapse
 
vonheikemen profile image
Heiker

It is worth mention that fzf comes with a few handy functions to search the command history and also directories. You just need find and source the fzf/key-bindings.bash file (there is also one for zsh and fish). More info about it here.