Skip to content

Change Directory Faster With Fzf

Published: at 12:17 PM

Problem: you are working on some new mono repo, and you are trying to reach the desired project directory or file directly when you edit, copy, or delete etc.

With that usage pattern, one thing I notice is that fzf can significantly reduce the time it takes to cd/cp/mv/rm.

If you install your fzf with the shell integration (I use bash here), one thing you can do is to do the following:

# Shows the autocompletion prompt
$ cd some/dir/**<tab>

This is awesome! You get a very fast autocompletion that is very deep. You can get to the file directly.

If you need the **<tab> to work with other commands (like nvim etc.), you can do the following in your ~/.bashrc (from the fzf installation instructions):

# This should be added for you by the installation script
[ -f ~/.fzf.bash ] && source ~/.fzf.bash

# Basically adding commands that are not in the default
# I set my nv to be an alias to nvim
_fzf_setup_completion path ag git kubectl nvim nv vim vi
_fzf_setup_completion dir tree
$

In order to go back up a directory, you can define the “up” command in your ~/.bashrc as:

up() {
    local path i
    for (( i=0; i < $1; i++ )); do
        path+=../
    done
    cd "$path"
}

You then can do:

# Go to a directory and do things...
$ cd **<tab>

# To go 3 directories up:
$ up 3

Then of course you can do pushd and popd as well for this purpose, but sometimes you don’t know how many levels you want to go up/down.

Hope you can use this to stop wasting time typing the tab key!