Make fzf your best friend inside terminal
Tags: linux, terminal, shell, productivity
Published: 2021-06-05 11:56:28

[Fuzzy finder](https://github.com/junegunn/fzf) is one of the best tool which you can use for speeding up your daily terminal work flow. In my daily workflow, I have integrated `fzf` in most of the tasks where I have many choices and have to choose one among them, whether be it changing a git branch, searching and opening a file, changing directory or changing a project.

Let's get started to exploit some features of `fzf` 😈

## Install

Probably your [package manager already has it](https://github.com/junegunn/fzf#using-linux-package-managers). If it doesn't, don't worry, just clone the git repo and run the `install` script as directed [here](https://github.com/junegunn/fzf#using-git).

You can check if the installation has been successful by running the command `whereis fzf`. You should see the location of binary.

Make sure to read the `README.md` from the project's Github. It explains `fzf` and some of it's usage quite well. Specially the [key-binding](https://github.com/junegunn/fzf#key-bindings-for-command-line) part, where you can map `Ctrl-t` to paste the selected files and directories onto the command-line, `Alt-c` for changing directories and `Ctrl-r` for fuzzy reverse searching. Also if you happen to use `vim`, you may check the [vim extension](https://github.com/junegunn/fzf#vim-plugin) for same.

## Some other usage

### Changing git projects

Add the following alias in your bashrc:

```sh
# Find and save location of all git repositories present in $HOME.
# Try using `fd', if not installed then use default `find'.
alias repoup="fd -HI ^\.git$ $HOME 2>/dev/null > $HOME/.tmp/gitfiles || find $HOME/ -regex .*/\.git$ 2>/dev/null > $HOME/.tmp/gitfiles"
```
> If you happen to use `fd` ([which definitely you should](https://dev.to/chhajedji/why-to-find-files-when-you-can-fd-them-26ej)), this command will be completed in a wink.

Executing this alias will use search path of all git repositories stored inside `HOME` and list them in file at `~/.tmp/gitfiles`. This file will be further used with `fzf`.

Make sure to execute this alias whenever you add a new git repository in your system. This will update the paths of all git repositories, specially the newly added one.

Next, add this function in your bashrc file:

```sh
gotoprj() {
    REPOS="$(cat $HOME/.tmp/gitfiles | xargs dirname  | sed s:/home/$USER:~: | fzf)"

# Cut the '~/' part from the `REPOS'.
        REPOS="$(echo $REPOS | cut -d '/' -f2-)"

# If user selected any repo then open `$TERMINAL' in that repo.
        [ "$REPOS" = "" ] || cd $HOME/$REPOS
}
```

This will list all the git repositories present in our database created in last step (`~/.tmp/gitfiles`) , and change current directory to that repository.

### Switch to any directory

As simple as said in title, from any current directory, switch to any directory in you system by just typing the keywords you remember specific to target directory. Have this function in your bashrc:

```sh
sd() {
    TARGET="$({ fd . $HOME --type directory -HI 2>/dev/null || find $HOME -type d 2>/dev/null; } | fzf)"
        [ -n "$TARGET" ] && cd "$TARGET" && ls
}
```
Now whenever you want to go to any directory, instead of giving it's full path, just hit `sd` and give the directory name and few unique words in it's path, and Tada 🎉! No need to enter that ugly full path.

### Change git branch

Many times it happens that I forget the branch name I want to switch to. Again, doing it with `fzf` saves the pain for searching the branch name. What can be done is add following section in your gitconfig file which should be present at `$HOME/.gitconfig`.

```config
[alias]
    ; switch git local branches using fzf
    sb="!switch_branch_fzf() { git branch | fzf | xargs git checkout; }; switch_branch_fzf"
```
> Note that since we are using `git branch`, it will only list the branches you have previously checked out. New branches will not appear in the finder.

#### Usage

```git sb```

This will list all the git branches in `fzf` and now we can search for the target branch with ease.

Once you get the idea of using the fuzzy finder, you can modify it to find anything which you need to search on regular basis. For example, if you have a location where you store all the scripts which you edit frequently, you can have a function to list all of them into `fzf` and then open the selected one in your editor. These were just some of the exploits I use. If you have anything more to suggest, add it in the comments.